Skip to content

Commit f23e833

Browse files
author
Phil Sturgeon
committed
Merge branch 'release/0.3.0'
2 parents 651c2de + 3c4a1f6 commit f23e833

17 files changed

+281
-73
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

+21-5
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@ Authorize users with your application in a driver-base fashion meaning one imple
44

55
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.
66

7-
## Currently Supported
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:
811

912
- Facebook
1013
- Foursquare
1114
- GitHub
1215
- Google
16+
- PayPal
1317
- Instagram
18+
- Soundcloud
1419
- Windows Live
1520
- YouTube
1621

17-
## TODO
1822

19-
This is a developing library and currently only supports a small number of OAuth2 providers - more refactoring of code is to follow with a full implementation of an authentication class to store users details.
23+
## TODO
2024

21-
Requests should be done through a more stable system, there however isn't a request class in CodeIgniter.
25+
- Requests should be done through a more stable system, there however isn't a Request class in CodeIgniter.
26+
- Add unit tests and get on Travis
2227

2328
## Usage Example
2429

@@ -75,4 +80,15 @@ class Auth extends CI_Controller
7580
}
7681
```
7782

78-
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.
83+
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.
84+
85+
Contribute
86+
----------
87+
88+
1. Check for open issues or open a new issue for a feature request or a bug
89+
2. Fork [the repository][] on Github to start making your changes to the
90+
`develop` branch (or branch off of it)
91+
3. Write a test which shows that the bug was fixed or that the feature works as expected
92+
4. Send a pull request and bug me until I merge it
93+
94+
[the repository]: https://github.com/philsturgeon/codeigniter-oauth2

config/autoload.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
22

3-
$autoload['libraries'] = array('OAuth2');
3+
$autoload['libraries'] = array('oauth2');

libraries/Provider.php

+31-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22
/**
3-
* OAuth Provider
3+
* OAuth2 Provider
44
*
5-
* @package CodeIgniter/OAuth
5+
* @package CodeIgniter/OAuth2
66
* @category Provider
77
* @author Phil Sturgeon
8-
* @copyright Phil Sturgeon
8+
* @copyright (c) 2012 HappyNinjas Ltd
99
* @license http://philsturgeon.co.uk/code/dbad-license
1010
*/
1111

12-
abstract class OAuth2_Provider {
13-
12+
abstract class OAuth2_Provider
13+
{
1414
/**
1515
* @var string provider name
1616
*/
@@ -21,15 +21,10 @@ abstract class OAuth2_Provider {
2121
*/
2222
public $uid_key = 'uid';
2323

24-
/**
25-
* @var string scope separator, most use "," but some like Google are spaces
26-
*/
27-
public $scope_seperator = ',';
28-
2924
/**
3025
* @var string additional request parameters to be used for remote requests
3126
*/
32-
public $callback = null;
27+
public $callback;
3328

3429
/**
3530
* @var array additional request parameters to be used for remote requests
@@ -41,6 +36,16 @@ abstract class OAuth2_Provider {
4136
*/
4237
protected $method = 'GET';
4338

39+
/**
40+
* @var string default scope (useful if a scope is required for user info)
41+
*/
42+
protected $scope;
43+
44+
/**
45+
* @var string scope separator, most use "," but some like Google are spaces
46+
*/
47+
protected $scope_seperator = ',';
48+
4449
/**
4550
* Overloads default class properties from the options.
4651
*
@@ -160,20 +165,31 @@ public function access($code, $options = array())
160165
$url .= '?'.http_build_query($params);
161166
$response = file_get_contents($url);
162167

163-
parse_str($response, $return);
168+
parse_str($response, $return);
164169

165170
break;
166171

167172
case 'POST':
168173

169-
$postdata = http_build_query($params);
174+
/* $ci = get_instance();
175+
176+
$ci->load->spark('curl/1.2.1');
177+
178+
$ci->curl
179+
->create($url)
180+
->post($params, array('failonerror' => false));
181+
182+
$response = $ci->curl->execute();
183+
*/
184+
170185
$opts = array(
171186
'http' => array(
172187
'method' => 'POST',
173188
'header' => 'Content-type: application/x-www-form-urlencoded',
174-
'content' => $postdata
189+
'content' => http_build_query($params),
175190
)
176191
);
192+
177193
$_default_opts = stream_context_get_params(stream_context_get_default());
178194
$context = stream_context_create(array_merge_recursive($_default_opts['options'], $opts));
179195
$response = file_get_contents($url, false, $context);
@@ -186,7 +202,7 @@ public function access($code, $options = array())
186202
throw new OutOfBoundsException("Method '{$this->method}' must be either GET or POST");
187203
}
188204

189-
if (isset($return['error']))
205+
if ( ! empty($return['error']))
190206
{
191207
throw new OAuth2_Exception($return);
192208
}

libraries/Provider/Facebook.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
/**
3+
* Facebook OAuth2 Provider
4+
*
5+
* @package CodeIgniter/OAuth2
6+
* @category Provider
7+
* @author Phil Sturgeon
8+
* @copyright (c) 2012 HappyNinjas Ltd
9+
* @license http://philsturgeon.co.uk/code/dbad-license
10+
*/
211

312
class OAuth2_Provider_Facebook extends OAuth2_Provider
4-
{
5-
public $name = 'facebook';
6-
7-
public $uid_key = 'uid';
8-
9-
public $scope = array('offline_access', 'email', 'read_stream');
13+
{
14+
protected $scope = array('offline_access', 'email', 'read_stream');
1015

1116
public function url_authorize()
1217
{

libraries/Provider/Foursquare.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Foursquare OAuth2 Provider
4+
*
5+
* @package CodeIgniter/OAuth2
6+
* @category Provider
7+
* @author Phil Sturgeon
8+
* @copyright (c) 2012 HappyNinjas Ltd
9+
* @license http://philsturgeon.co.uk/code/dbad-license
10+
*/
11+
12+
class OAuth2_Provider_Foursquare extends OAuth2_Provider
13+
{
14+
public $method = 'POST';
15+
16+
public function url_authorize()
17+
{
18+
return 'https://foursquare.com/oauth2/authenticate';
19+
}
20+
21+
public function url_access_token()
22+
{
23+
return 'https://foursquare.com/oauth2/access_token';
24+
}
25+
26+
public function get_user_info(OAuth2_Token_Access $token)
27+
{
28+
$url = 'https://api.foursquare.com/v2/users/self?'.http_build_query(array(
29+
'oauth_token' => $token->access_token,
30+
));
31+
32+
$response = json_decode(file_get_contents($url));
33+
34+
$user = $response->response->user;
35+
36+
// Create a response from the request
37+
return array(
38+
'uid' => $user->id,
39+
//'nickname' => $user->login,
40+
'name' => sprintf('%s %s', $user->firstName, $user->lastName),
41+
'email' => $user->contact->email,
42+
'image' => $user->photo,
43+
'location' => $user->homeCity,
44+
);
45+
}
46+
}

libraries/Provider/Github.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
/**
3+
* GitHub OAuth2 Provider
4+
*
5+
* @package CodeIgniter/OAuth2
6+
* @category Provider
7+
* @author Phil Sturgeon
8+
* @copyright (c) 2012 HappyNinjas Ltd
9+
* @license http://philsturgeon.co.uk/code/dbad-license
10+
*/
211

3-
class OAuth2_Provider_Github extends OAuth2_Provider {
4-
5-
public $name = 'github';
6-
7-
// https://api.github.com
8-
12+
class OAuth2_Provider_Github extends OAuth2_Provider
13+
{
914
public function url_authorize()
1015
{
1116
return 'https://github.com/login/oauth/authorize';

libraries/Provider/Google.php

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
<?php
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
/**
3+
* Google OAuth2 Provider
4+
*
5+
* @package CodeIgniter/OAuth2
6+
* @category Provider
7+
* @author Phil Sturgeon
8+
* @copyright (c) 2012 HappyNinjas Ltd
9+
* @license http://philsturgeon.co.uk/code/dbad-license
10+
*/
211

3-
class OAuth2_Provider_Google extends OAuth2_Provider {
4-
5-
public $name = 'google';
6-
7-
public $human = 'Google';
8-
9-
public $uid_key = 'uid';
10-
12+
class OAuth2_Provider_Google extends OAuth2_Provider
13+
{
14+
/**
15+
* @var string the method to use when requesting tokens
16+
*/
1117
public $method = 'POST';
1218

19+
/**
20+
* @var string scope separator, most use "," but some like Google are spaces
21+
*/
1322
public $scope_seperator = ' ';
1423

1524
public function url_authorize()

libraries/Provider/Instagram.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
/**
3+
* Instagram OAuth2 Provider
4+
*
5+
* @package CodeIgniter/OAuth2
6+
* @category Provider
7+
* @author Phil Sturgeon
8+
* @copyright (c) 2012 HappyNinjas Ltd
9+
* @license http://philsturgeon.co.uk/code/dbad-license
10+
*/
11+
12+
class OAuth_Provider_Instagram extends OAuth2_Provider
13+
{
14+
/**
15+
* @var string scope separator, most use "," but some like Google are spaces
16+
*/
17+
public $scope_seperator = '+';
18+
19+
/**
20+
* @var string the method to use when requesting tokens
21+
*/
22+
public $method = 'POST';
23+
24+
public function url_authorize()
25+
{
26+
return 'https://api.instagram.com/oauth/authorize';
27+
}
28+
29+
public function url_access_token()
30+
{
31+
return 'https://api.instagram.com/oauth/access_token';
32+
}
33+
34+
public function get_user_info(OAuth2_Token_Access $token)
35+
{
36+
$user = $token->user;
37+
38+
return array(
39+
'uid' => $user->id,
40+
'nickname' => $user->username,
41+
'name' => $user->full_name,
42+
'image' => $user->profile_picture,
43+
'urls' => array(
44+
'website' => $user->website,
45+
),
46+
);
47+
}
48+
}

libraries/Provider/Mailchimp.php

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
/**
3+
* Mailchimp OAuth2 Provider
4+
*
5+
* @package CodeIgniter/OAuth2
6+
* @category Provider
7+
* @author Phil Sturgeon
8+
* @copyright (c) 2012 HappyNinjas Ltd
9+
* @license http://philsturgeon.co.uk/code/dbad-license
10+
*/
211

312
class OAuth2_Provider_Mailchimp extends OAuth2_Provider
413
{

0 commit comments

Comments
 (0)