Repository navigation
Expand file tree
/
Copy pathLightOAuth2.php
More file actions
393 lines (362 loc) · 12.5 KB
/
Copy pathLightOAuth2.php
File metadata and controls
393 lines (362 loc) · 12.5 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/**
This class provides a simple interface for OAuth 2.0.
- OAuth 2.0 draft 10 supported.
- fallback mode for ealier draft version of OAuth 2.0 supported.
(for Facebook)
- MAC, BEARER Authorization header based on OAuth 2.0 draft 12.
Usage:
<code>
// for Facebook OAuth 2.0
$url = "https://graph.facebook.com/me/feed";
$entry = array('callback'=>'http://www.example.com/ws_fb.php',
'authorize'=>'https://graph.facebook.com/oauth/authorize',
'access_token'=>'https://graph.facebook.com/oauth/access_token');
$opts = array('redirect_uri'=>$entry['callback'],
'scope'=>'read_stream,offline_access,publish_stream');
$copts = array('cainfo'=>CAFILE); // can be ignored if cainfo available
$oauth = new LightOAuth2(CLIENT_ID, CLIENT_SECRET, $copts);
session_start();
if (!isset($_SESSION['access_token'])) {
if (!isset($_GET['code'])) {
$url = $oauth->getAuthUrl($entry['authorize'], $entry['callback'], $opts);
header("Location: " . $url);
exit();
}
$obj = $oauth->getToken($entry['access_token'], $entry['callback'],
$_GET['code'], 'json');
$_SESSION['access_token'] = $obj->access_token;
}
// access to proteced resource
$oauth->setToken($_SESSION['access_token']);
$oauth->fetch($url);
</code>
The library requires PHP >= 5.3.
@author Rui Hirokawa <rui_hirokawa at yahoo.co.jp>
@copyright Copyright (c) 2012, Rui Hirokawa
@license http://www.opensource.org/licenses/mit-license.php MIT
*/
class LightOAuth2 {
const AUTH_TYPE_HEADER = 1, AUTH_TYPE_FORM = 2, AUTH_TYPE_URI = 3;
const AUTH_ENGINE_STREAM = 1, AUTH_ENGINE_CURL = 2;
protected $client_id = null, $client_secret = null, $token = null;
protected $copts = array('verify_peer'=>true,
'requestEngine'=>self::AUTH_ENGINE_CURL);
protected $credentials = 'Bearer';
protected $secret = null;
protected $algorithm = 'hmac-sha-1';
protected $auth_type = self::AUTH_TYPE_HEADER;
/**
* Constructor of the class
*
* @param $client_id the client id
* @param $client_secret the client secret
* @param $copts the optional array
* @return an instance of the object
*/
public function __construct($client_id, $client_secret, $copts = array()) {
$this->client_id = $client_id;
$this->client_secret = $client_secret;
$this->copts = array_merge($this->copts, $copts);
}
/**
* issue a request using PHP stream
*
* @param $url the url to access
* @param $params the optional parameters
* @param $method the HTTP method (default: 'GET')
* @param $headers the optional headers
* @param $code the expected HTTP response code
* @returns the response string
*/
protected function requestStream($url, $params = null, $method = 'GET',
$headers = array(), $code = 200) {
$opts = array('http'=> array('method' => $method, 'follow_location' => false),
'ssl' => array('verify_peer' => $this->copts['verify_peer']));
if(isset($this->copts['cainfo'])) {
$opts['ssl']['cafile'] = $this->copts['cainfo'];
}
if (!empty($params)) {
if ($method == 'POST') {
if (is_array($params)) {
$files = array();
foreach($params as $key => $value) {
if ($value[0] == '@') { // file attachment
$files[$key] = substr($value, 1);
unset($params[$key]);
}
}
if (!empty($files)) {
$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);
$headers['Content-Type'] = "multipart/form-data; boundary=$boundary";
$content = $this->buildFormData($params, $boundary);
} else {
$headers['Content-type'] = "application/x-www-form-urlencoded";
$content = http_build_query($params);
}
$opts['http']['content'] = $content;
} else {
$opts['http']['content'] = http_build_query($params);
}
} else if ($method == 'GET') {
$url .= '?' . http_build_query($params,'','&');
}
}
$header = '';
foreach($headers as $key => $value) {
$header .= "$key: $value\r\n";
}
$opts['http']['header'] = $header;
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
if (!$result || !strpos($http_response_header[0], (string)$code)) {
throw new RuntimeException($http_response_header[0]);
}
return $result;
}
/**
* issue a request using curl extension
*
* @param $url the url to access
* @param $params the optional parameters
* @param $method the HTTP method (default: 'GET')
* @param $headers the optional headers
* @param $code the expected HTTP response code
* @returns the response string
*/
protected function requestCurl($url, $params = null, $method = 'GET',
$headers = array(), $code = 200) {
$opts = array(CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true);
if ($method == 'POST' || $method == 'PUT') {
if (!is_null($params)) {
$opts[CURLOPT_POSTFIELDS] = $params;
}
} elseif ($method == 'GET') {
if (!is_null($params)) {
$url .= '?'. http_build_query($params,'','&');
}
}
$opts[CURLOPT_CUSTOMREQUEST] = $method;
$opts[CURLOPT_URL] = $url;
if(isset($this->copts['cainfo'])) {
$opts[CURLOPT_CAINFO] = $this->copts['cainfo'];
}
if (!empty($headers)) {
$header = array();
foreach($headers as $key => $value) {
$header[] = "$key: $value";
}
$opts[CURLOPT_HTTPHEADER] = $header;
}
$ch = curl_init();
curl_setopt_array($ch, $opts);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (intval($http_code) != $code) {
throw new RuntimeException("returned code: $http_code");
}
curl_close($ch);
return $result;
}
/**
* Get token and token secret
*
* @param $url the endpoint url to obtain the token
* @param $callback the callback url (null for 'refresh_token')
* @param $token the current token for 'refresh_token'
* @param $type the type of parameter encoding ('json' or 'url')
* @return an object including the access_token
*/
public function getToken($url, $callback = null, $token = null, $type = 'json') {
$gtype = empty($callback) ? 'refresh_token' : 'authorization_code';
$params = array('grant_type'=>$gtype,
'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret);
if ($gtype == 'authorization_code') {
$params['code'] = $token;
$params['redirect_uri'] = $callback;
} else { // refresh_token
$params['refresh_token'] = $token;
}
if ($this->copts['requestEngine'] == self::AUTH_ENGINE_STREAM) {
$result = $this->requestStream($url, $params, 'POST', null, 200);
} else {
$result = $this->requestCurl($url, $params, 'POST', null, 200);
}
if ($type == 'json') {
$obj = json_decode($result);
} else {
parse_str($result, $r);
$obj = (object)$r;
}
return $obj;
}
/**
* Returns the url for authorization
*
* @param $url the endpoint for authorization
* @param $redirect_uri the uri for redirection
* @param $opts the optional parameters
* @return the url for authorization
*/
public function getAuthUrl($url, $redirect_uri, $opts = array()) {
$params = array('client_id'=>$this->client_id,
'redirect_uri'=>$redirect_uri,
'response_type'=>'code');
$params = array_merge($opts, $params);
$auth_url = $url . '?'. http_build_query($params, null, '&');
return $auth_url;
}
/**
* Set token
*
* @param $token the token
*/
public function setToken($token) {
$this->token = $token;
}
/**
* issue a request for the protected resource
*
* @param $url the url to access
* @param $params the optional parameters
* @param $method the HTTP method (default: HTTP_METH_GET)
* @param $headers the optional headers
* @param $code the expected HTTP response code
* @param $auth_get true if the authorization parameter sends via GET
* @returns the response string
*/
public function fetch($url, $params = null, $method = 'GET',
$headers = array(), $code = 200){
if ($this->auth_type == self::AUTH_TYPE_HEADER) {
$sauth = $this->getAuthHeader($url, $this->token, $method);
$headers['Authorization'] = $sauth;
} else {
$params['access_token'] = $this->token;
}
if ($this->copts['requestEngine'] == self::AUTH_ENGINE_STREAM) {
$result = $this->requestStream($url, $params, $method, $headers, $code);
} else {
$result = $this->requestCurl($url, $params, $method, $headers, $code);
}
return $result;
}
/**
* set method to send the authorization information in the request
*
* @param $auth_type authorization type (default: AUTH_TYPE_HEADER)
*/
public function setAuthType($auth_type) {
$this->auth_type = $auth_type;
}
/**
* build multipart/formdata
*
* @param $params the optional parameters
* @param $boundary the boundary
* @returns the content string
*/
protected function buildFormData($params = array(), $boundary) {
$content = '';
foreach($params as $key => $value) {
$content .= "--$boundary\r\n";
$content .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
$content .= $value . "\r\n";
}
$content .= "--$boundary\r\n";
foreach($files as $key => $value) {
$content .= "Content-Disposition: form-data; name=\"$key\"; filename=\"$value\"\r\n\r\n";
$content .= file_get_contents($value) . "\r\n";
$content .= "--$boundary--\r\n";
}
return $content;
}
/**
* set credentials
*
* @param $credentials credentials: 'OAuth'(draft 10),'MAC' or 'Bearer'
* @param $secret token secret
* @param $algorithm hash-mac algorithm ('hmac-sha-1' or 'hmac-sha-256')
*/
public function setCredentials($credentials = 'Bearer', $secret = null,
$algorithm = 'hmac-sha-1') {
$this->credentials = $credentials;
$this->secret = $secret;
$this->algorithm = $algorithm;
}
/**
* generate MAC signature
*
* @param $url the target url
* @param $params parameters
* @param $secret token secret
* @param $method the HTTP method
* @returns signature string
*/
public function getSignature($url, $params, $secret, $method = 'GET') {
$v = parse_url($url);
if (isset($v['query'])) {
$query = $this->normalized_query($v['query']);
} else {
$query = '';
}
if (!isset($v['port'])) {
$v['port'] = ($v['scheme'] == 'https') ? 443 : 80;
}
$base = "{$params['token']}\n{$params['timestamp']}\n{$params['nonce']}\n";
$base .= "$method\n{$v['host']}\n{$v['port']}\n{$v['path']}\n$query";
$alg = ($this->algorithm == 'hmac-sha-256') ? 'sha256' : 'sha1';
$sign = base64_encode(hash_hmac($alg,$base, $secret, true));
return $sign;
}
/**
* generate Authorization header
*
* @param $url the target url
* @param $token token
* @param $method HTTP method
* @param $timestamp timestamp. automatically generated if it is ignored.
* @param $nonce nonce. automatically generated if it is ignored.
* @returns authorization header string.
*/
public function getAuthHeader($url, $token, $method = HTTP_METH_GET,
$timestamp = null, $nonce = null) {
if (strtolower($this->credentials) == 'mac' && !empty($this->secret)) {
$timestamp = $timestamp ?: time();
$nonce = $nonce ?: md5(microtime(true) . rand(1, 999));
$params = array('token'=>$token,
'timestamp'=>$timestamp,
'nonce'=>$nonce);
$params['signature'] = $this->getSignature($url, $params, $this->secret);
$head = '';
foreach($params as $key => $value) {
$head .= ",$key=\"$value\"";
}
$token = substr($head, 1);
}
return $this->credentials .' '. $token;
}
/**
* build query string for MAC signature
*
* @param $params the optional parameters
* @returns string based on RFC-3986
*/
protected function normalized_query($query) {
$v = explode('&',$query);
$q = array();
foreach($v as $key) {
if (strpos($key,'=')) {
list($key, $value) = explode('=',$key);
$value = rawurlencode(urldecode($value));
} else {
$value = '';
}
$key = rawurlencode(urldecode($key));
$q[] = "$key=$value";
}
sort($q);
return implode("\n",$q)."\n";
}
}
?>