forked from philsturgeon/codeigniter-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAuth2_Provider.php
213 lines (176 loc) · 4.99 KB
/
OAuth2_Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* OAuth Provider
*
* @package CodeIgniter/OAuth
* @category Provider
* @author Phil Sturgeon
* @copyright Phil Sturgeon
* @license http://philsturgeon.co.uk/code/dbad-license
*/
abstract class OAuth2_Provider {
/**
* @var string provider name
*/
public $name;
/**
* @var string uid key name
*/
public $uid_key = 'uid';
/**
* @var string scope separator, most use "," but some like Google are spaces
*/
public $scope_seperator = ',';
/**
* @var string additional request parameters to be used for remote requests
*/
public $callback = null;
/**
* @var string scope
*/
public $scope;
/**
* @var array additional request parameters to be used for remote requests
*/
protected $params = array();
/**
* @var string the method to use when requesting tokens
*/
protected $method = 'GET';
/**
* Overloads default class properties from the options.
*
* Any of the provider options can be set here, such as app_id or secret.
*
* @param array provider options
* @return void
*/
public function __construct(array $options = array())
{
if ( ! $this->name)
{
// Attempt to guess the name from the class name
$this->name = strtolower(substr(get_class($this), strlen('OAuth2_Provider_')));
}
if (empty($options['id']))
{
throw new Exception('Required option not provided: id');
}
$this->client_id = $options['id'];
isset($options['callback']) and $this->callback = $options['callback'];
isset($options['secret']) and $this->client_secret = $options['secret'];
isset($options['scope']) and $this->scope = $options['scope'];
$this->redirect_uri = URL::to(URI::current()); // '/'.ltrim(Laravel\URI::current(), '/');
}
/**
* Return the value of any protected class variable.
*
* // Get the provider signature
* $signature = $provider->signature;
*
* @param string variable name
* @return mixed
*/
public function __get($key)
{
return $this->$key;
}
/**
* Returns the authorization URL for the provider.
*
* $url = $provider->url_authorize();
*
* @return string
*/
abstract public function url_authorize();
/**
* Returns the access token endpoint for the provider.
*
* $url = $provider->url_access_token();
*
* @return string
*/
abstract public function url_access_token();
/*
* Get an authorization code from Facebook. Redirects to Facebook, which this redirects back to the app using the redirect address you've set.
*/
public function authorize($options = array())
{
$state = md5(uniqid(rand(), TRUE));
Laravel\Session::put('state', $state);
$params = array(
'client_id' => $this->client_id,
'redirect_url' => isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri,
'state' => $state,
'scope' => is_array($this->scope) ? implode($this->scope_seperator, $this->scope) : $this->scope,
'response_type' => 'code',
'approval_prompt' => 'force' // - google force-recheck
);
$url = $this->url_authorize().'?'.http_build_query($params);
return Laravel\Redirect::to($url);
}
/*
* Get access to the API
*
* @param string The access code
* @return object Success or failure along with the response details
*/
public function access($code, $options = array())
{
$params = array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code',
);
switch ($params['grant_type'])
{
case 'authorization_code':
$params['code'] = $code;
$params['redirect_url'] = isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri;
break;
case 'refresh_token':
$params['refresh_token'] = $code;
break;
}
$response = null;
$url = $this->url_access_token();
switch ($this->method)
{
case 'GET':
// Need to switch to Request library, but need to test it on one that works
$url .= '?'.http_build_query($params);
$response = file_get_contents($url);
parse_str($response, $return);
break;
case 'POST':
$postdata = http_build_query($params);
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$_default_opts = stream_context_get_params(stream_context_get_default());
$context = stream_context_create(array_merge_recursive($_default_opts['options'], $opts));
$response = file_get_contents($url, false, $context);
$return = json_decode($response, true);
break;
default:
throw new OutOfBoundsException("Method '{$this->method}' must be either GET or POST");
}
if (isset($return['error']))
{
throw new OAuth2_Exception($return);
}
switch ($params['grant_type'])
{
case 'authorization_code':
return OAuth2_Token::factory('access', $return);
break;
case 'refresh_token':
return OAuth2_Token::factory('refresh', $return);
break;
}
}
}