Jeroen Moors
Oauth client library for CodeIgniter

Some time ago I was hired to coach a project where also oAuth was required. Lucky the client was so kind to let us opensource the code.

The resulting, CodeIgniter, library can be found on GitHub. It’s written for CodeIgniter, but with very small modification it can be used with any PHP framework.

Supports

  • Facebook (oAuth v2)
  • Twitter (oAuth v1)
  • LinkedIn (oAuth v1)

Features

  • Let users login into your site using their Facebook, Twitter or LinkedIn login
  • Make API call’s on behalf of them

Example usage




Simple CodeIgniter module for user session management

Recent I got a question how to connect my oAuth module to Tank Auth for user session management. While that might be possible, I think that would be overkill.

When I wrote the oAuth module, I also wrote a very simple module to handle the user session management. The module has only 3 methods:

Set the current user id

After your authentication process, being it oAuth or any thing else, use $this->current_user->setId(10, true); to set the current logged in user id to 10. The $rememberMe parameter defines if the user has to login or not the next time he or she visits the site.

For this functionality a cookie use used on the client side and a table named user_autologin on the server.

Get the current user id

Calling $userId = $this->current_user->getId(); will store the current logged in user in $userId or false if there is currently no active user session.

Log the current user off

To end a session use $this->current_user->logoff();.

For the Remember Me functionality to work you will need to create a table called user_autologin:

The code can be downloaded here. As always all feedback is welcome!

Oauth client for CodeIgniter … and others

I’ve been working on Facebook, Twitter and LinkedIn oAuth authentication for my secret pet project LunchUp.biz.

Recent I was hired to coach a project where also oAuth was required. I decided to clean up the code a bit and publish it.

The resulting, CodeIgniter, library can be found on GitHub. It’s written for CodeIgniter, but with very small modification it can be used with any PHP framework.

Supports:

  • Facebook (oAuth v2)
  • Twitter (oAuth v1)
  • LinkedIn (oAuth v1)
  • FourSquare (oAuth v2) – Work in progress

Features:

  • Let users login into your site using their Facebook, Twitter or LinkedIn login
  • Make API call’s on behalf of them

Files in the package:

  • OAuthclient.php: The library itself, put in application/library
  • demo_twitter.php: Demo code for Twitter, put in application/controllers
  • demo_facebook.php: Demo code for Facebook, put in application/controllers
  • demo_linkedin.php: Demo code for LinkedIn, put in application/controllers
  • OAuthloader.php: A helper library to load the real library with all its configuration parameters. (not required).
  • demo_twitter_oauthloader.php: Demo code for Twitter using the helper library

Requirements

For the oAuth v1 services PHP oAuth is required. In a next version this can be replaced by pure PHP-code as part of the library.

How to use

1. Create a table to store the oAuth data:

Note: If you prefer a different name then “user_oauth” for the table, make sure you use the right name with the setOauthDataStore function.

2. Load the library into your controller code:

3. Set some parameters:

4. Call the connect function, this will redirect the client to the authorization page.

5. Create a controller function that’s used to handle the response

6. Now your user is logged in!

During all sessions $userId will always be the same. Keep in mind that one user can use different oAuth services (Facebook, Twitter, LinkedIn), so one user can hold multiple userId’s.

I guess the best way to handle this, is to use your own separated users table and link the user oAuth user id(s) to this user.

What else can I do?
There’s one build in api-call getProfile:

To call any other function, find its url and call it with the apiCallfunction:

Where to get Consumer Key’s and Consumer Secrets

To use oAuth registration of your application or site is required.

How to use without CodeIgniter

You can instantiate the class into any other PHP framework or even vanilla PHP.

The class uses CodeIgniter ActiveRecord to read and write to the database. Just search for “db->” in the code and you’ll find 4 queries. The code to run these queries has to be rewritten to your favorite query-functions.

It’s not perfect…yet

I’m not the best developer in the world. I’m not even aware of my current position in the world programmer ranking. For me this code is working, that’s about all warranty I can provide.

If you find a bug or have any other comment, all feedback is more then welcome, just leave a comment!