| 12694 |
anikendra |
1 |
# Codeigniter OAuth 2.0
|
|
|
2 |
|
|
|
3 |
Authorize users with your application in a driver-base fashion meaning one implementation works for multiple OAuth 2 providers. This is only to authenticate onto OAuth2 providers and not to build an OAuth2 service.
|
|
|
4 |
|
|
|
5 |
Note that this Spark ONLY provides the authorization mechanism. There's an example controller below, however in a later version there will be a full controller.
|
|
|
6 |
|
|
|
7 |
## Examples
|
|
|
8 |
|
|
|
9 |
OAuth 2 is split into two sections, clients and providers. A client is an application - perhaps a basic Twitter feed aggregator - which
|
|
|
10 |
authenticates with an OAuth 2 provider, which in this example would be Twitter itself. You can interact with any provider which is supported in the list below:
|
|
|
11 |
|
|
|
12 |
- Facebook
|
|
|
13 |
- Foursquare
|
|
|
14 |
- GitHub
|
|
|
15 |
- Google
|
|
|
16 |
- Instagram
|
|
|
17 |
- Mailchimp
|
|
|
18 |
- Mail.ru
|
|
|
19 |
- PayPal
|
|
|
20 |
- Soundcloud
|
|
|
21 |
- Vkontakte
|
|
|
22 |
- Windows Live
|
|
|
23 |
- Yandex
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
## TODO
|
|
|
27 |
|
|
|
28 |
- Requests should be done through a more stable system, there however isn't a Request class in CodeIgniter.
|
|
|
29 |
- Add unit tests and get on Travis
|
|
|
30 |
|
|
|
31 |
## Usage Example
|
|
|
32 |
|
|
|
33 |
This example will need the user to go to a certain URL, which will support multiple providers. I like to set a controller to handle it and either have one single "session" method - or have another method for callbacks if you want to separate out the code even more.
|
|
|
34 |
|
|
|
35 |
Here you'll see we have the provider passed in as a URI segment of "facebook" which can be used to find config in a database, or in a config multi-dimensional array. If you want to hard code it all then that is just fine too.
|
|
|
36 |
|
|
|
37 |
Send your user to `http://example.com/auth/session/facebook` where Auth is the name of the controller. This will also be the address of the "Callback URL" which will be required by many OAuth 2 providers such as Facebook.
|
|
|
38 |
|
|
|
39 |
```php
|
|
|
40 |
class Auth extends CI_Controller
|
|
|
41 |
{
|
|
|
42 |
public function session($provider)
|
|
|
43 |
{
|
|
|
44 |
$this->load->helper('url_helper');
|
|
|
45 |
|
|
|
46 |
$this->load->spark('oauth2/0.4.0');
|
|
|
47 |
|
|
|
48 |
$provider = $this->oauth2->provider($provider, array(
|
|
|
49 |
'id' => 'your-client-id',
|
|
|
50 |
'secret' => 'your-client-secret',
|
|
|
51 |
));
|
|
|
52 |
|
|
|
53 |
if ( ! $this->input->get('code'))
|
|
|
54 |
{
|
|
|
55 |
// By sending no options it'll come back here
|
|
|
56 |
$url = $provider->authorize();
|
|
|
57 |
|
|
|
58 |
redirect($url);
|
|
|
59 |
}
|
|
|
60 |
else
|
|
|
61 |
{
|
|
|
62 |
try
|
|
|
63 |
{
|
|
|
64 |
// Have a go at creating an access token from the code
|
|
|
65 |
$token = $provider->access($_GET['code']);
|
|
|
66 |
|
|
|
67 |
// Use this object to try and get some user details (username, full name, etc)
|
|
|
68 |
$user = $provider->get_user_info($token);
|
|
|
69 |
|
|
|
70 |
// Here you should use this information to A) look for a user B) help a new user sign up with existing data.
|
|
|
71 |
// If you store it all in a cookie and redirect to a registration page this is crazy-simple.
|
|
|
72 |
echo "<pre>Tokens: ";
|
|
|
73 |
var_dump($token);
|
|
|
74 |
|
|
|
75 |
echo "\n\nUser Info: ";
|
|
|
76 |
var_dump($user);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
catch (OAuth2_Exception $e)
|
|
|
80 |
{
|
|
|
81 |
show_error('That didnt work: '.$e);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
```
|
|
|
88 |
|
|
|
89 |
If all goes well you should see a dump of user data and have `$token` available. If all does not go well you'll likely have a bunch of errors on your screen.
|
|
|
90 |
|
|
|
91 |
Contribute
|
|
|
92 |
----------
|
|
|
93 |
|
|
|
94 |
1. Check for open issues or open a new issue for a feature request or a bug
|
|
|
95 |
2. Fork [the repository][repo] on Github to start making your changes to the
|
|
|
96 |
`develop` branch (or branch off of it)
|
|
|
97 |
3. Write a test which shows that the bug was fixed or that the feature works as expected
|
|
|
98 |
4. Send a pull request and bug me until I merge it
|
|
|
99 |
|
|
|
100 |
[repo]: https://github.com/philsturgeon/codeigniter-oauth2
|