Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating OAuth 2.0 with CodeIgniter

https://github.com/alexbilbie/CodeIgniter-OAuth-2.0-Server

I have found this on GitHub however the steps to implement don't really help with installing the OAuth code into CodeIgniter and haven't found any really good articles on how to do this

Has anyone already done this that can offer me help setting this up?

like image 848
hoangthienan Avatar asked Jun 06 '12 09:06

hoangthienan


2 Answers

Hé Hoang,

The oAuth library isn't really self explanatory. This is how I got it working:

Basics

  1. Read the oAuth 2.0 draft 23 just enough to get a basic idea of oAuth, the roles and flows.
  2. Then follow the instructions for installing the controller and libraries from alexbilbie in your CodeIgniter install
  3. Install the tables and add an application and some roles (think off a Facebook App and the roles you can request permissions for)
  4. Make sure you made your validate_user function in the oAuth_server.php file, at the bottom somewhere

Do a request

Now you want to perform an Authorization Request as a client. These few easy steps are documented in this section.

Edit: Philsturgeon's oAuth 2.0 authorization library could be used to automate this. Described here is the manual way.

For the library, this means going to:

/index.php/oauth?client_id=IN_YOUR_APPLICATION&redirect_uri=IN_YOUR_APPLICATION&response_type=code&scope=YOUR_ROLE

Fill in the variabels with the data you've putten in the database.

Debug some of the error's it might give..

If all goes well you dit the following:

Sign in -> Authorize application -> See you redirect_uri page with ?code=XXXXXXX

You'll want that XXXXXXX code

Then on the redirect_uri make a post to /index.php/oauth/access_token

With these variabels (you know them all now)

  • client_id (in application table)
  • client_secret (in application table)
  • redirect_uri (in application table: where you want to go to save the access_token)
  • code (the XXXXXX)
  • grant_type (must be 'authorization_code') You know this after reading that section!

That post returns a JSON string containing the access_token (or an error). YEAH!

What's next

Save the access_token in you actual application and use it in requests. On your resource server (probably an API and the same CodeIgniter project as the Authorization server I just explained) you need to validate the access_token before returning results.

This works like this:

$this->load->library('oauth_resource_server');
if (!$this->oauth_resource_server->has_scope(array('account.basic')))
{
    // Error logic here - "access token does not have correct permission"
    show_error('An access token is required to request this resource.');
}
else
{
    //GO RETURN RESULTS
}
 

Hope this gets you up and running!

PS: You need to build some admin area to manage applications, sessions and roles yourself though.

Eric

like image 63
ericbeekman Avatar answered Jan 02 '23 20:01

ericbeekman


I used another spark library that is really good to use with codeigniter. here is the good tutorial on how to install this with spark and use it. Oauth tutorial for codeigniter

like image 27
VSharma Avatar answered Jan 02 '23 21:01

VSharma