forked from opauth/opauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpauthStrategy.php
543 lines (464 loc) · 15.4 KB
/
OpauthStrategy.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
/**
* Opauth Strategy
* Individual strategies are to be extended from this class
*
* @copyright Copyright © 2012 U-Zyn Chua (http://uzyn.com)
* @link http://opauth.org
* @package Opauth.Strategy
* @license MIT License
*/
/**
* Opauth Strategy
* Individual strategies are to be extended from this class
*
* @package Opauth.Strategy
*/
#[\AllowDynamicProperties]
class OpauthStrategy {
/**
* Compulsory config keys, listed as unassociative arrays
* eg. array('app_id', 'app_secret');
*/
public $expects;
/**
* Optional config keys with respective default values, listed as associative arrays
* eg. array('scope' => 'email');
*/
public $defaults;
/**
* Auth response array, containing results after successful authentication
*/
public $auth;
/**
* Name of strategy
*/
public $name = null;
/**
* Configurations and settings unique to a particular strategy
*/
protected $strategy;
/**
* Safe env values from Opauth, with critical parameters stripped out
*/
protected $env;
/**
* Constructor
*
* @param array $strategy Strategy-specific configuration
* @param array $env Safe env values from Opauth, with critical parameters stripped out
*/
public function __construct($strategy, $env) {
$this->strategy = $strategy;
$this->env = $env;
// Include some useful values from Opauth's env
$this->strategy['strategy_callback_url'] = $this->env['host'].$this->env['callback_url'];
if ($this->name === null) {
$this->name = (isset($name) ? $name : get_class($this));
}
if (is_array($this->expects)) {
foreach ($this->expects as $key){
$this->expects($key);
}
}
if (is_array($this->defaults)) {
foreach ($this->defaults as $key => $value) {
$this->optional($key, $value);
}
}
/**
* Additional helpful values
*/
$this->strategy['path_to_strategy'] = $this->env['path'].$this->strategy['strategy_url_name'].'/';
$this->strategy['complete_url_to_strategy'] = $this->env['host'].$this->strategy['path_to_strategy'];
$dictionary = array_merge($this->env, $this->strategy);
foreach ($this->strategy as $key=>$value) {
$this->strategy[$key] = $this->envReplace($value, $dictionary);
}
}
/**
* Auth request
* aka Log in or Register
*/
public function request() {
}
/**
* Packs $auth nicely and send to callback_url, ships $auth either via GET, POST or session.
* Set shipping transport via callback_transport config, default being session.
*/
public function callback() {
$timestamp = date('c');
// To standardize the way of accessing data, objects are translated to arrays
$this->auth = $this->recursiveGetObjectVars($this->auth);
$this->auth['provider'] = $this->strategy['strategy_name'];
$params = array(
'auth' => $this->auth,
'timestamp' => $timestamp,
'signature' => $this->sign($timestamp)
);
$this->shipToCallback($params);
}
/**
* Error callback
*
* More info: https://github.com/uzyn/opauth/wiki/Auth-response#wiki-error-response
*
* @param array $error Data on error to be sent back along with the callback
* $error = array(
* 'provider' // Provider name
* 'code' // Error code, can be int (HTTP status) or string (eg. access_denied)
* 'message' // User-friendly error message
* 'raw' // Actual detail on the error, as returned by the provider
* )
*
*/
public function errorCallback($error) {
$timestamp = date('c');
$error = $this->recursiveGetObjectVars($error);
$error['provider'] = $this->strategy['strategy_name'];
$params = array(
'error' => $error,
'timestamp' => $timestamp
);
$this->shipToCallback($params);
}
/**
* Send $data to callback_url using specified transport method
*
* @param array $data Data to be sent
* @param string $transport Callback method, either 'get', 'post' or 'session'
* 'session': Default. Works best unless callback_url is on a different domain than Opauth
* 'post': Works cross-domain, but relies on availability of client-side JavaScript.
* 'get': Works cross-domain, but may be limited or corrupted by browser URL length limit
* (eg. IE8/IE9 has 2083-char limit)
*
*/
private function shipToCallback($data, $transport = null) {
if (empty($transport)) {
$transport = $this->env['callback_transport'];
}
switch($transport) {
case 'get':
$this->redirect($this->env['callback_url'].'?'.http_build_query(array('opauth' => base64_encode(json_encode($data))), '', '&'));
break;
case 'post':
$this->clientPost($this->env['callback_url'], array('opauth' => base64_encode(json_encode($data))));
break;
case 'session':
default:
if(!session_id()) {
session_start();
}
$_SESSION['opauth'] = $data;
$this->redirect($this->env['callback_url']);
}
}
/**
* Call an action from a defined strategy
*
* @param string $action Action name to call
* @param string $defaultAction If an action is not defined in a strategy, calls $defaultAction
*/
public function callAction($action, $defaultAction = 'request') {
if (method_exists($this, $action)) return $this->{$action}();
else return $this->{$defaultAction}();
}
/**
* Ensures that a compulsory value is set, throws an error if it's not set
*
* @param string $key Expected configuration key
* @param string $not If value is set as $not, trigger E_USER_ERROR
* @return mixed The loaded value
*/
protected function expects($key, $not = null) {
if (!array_key_exists($key, $this->strategy)) {
trigger_error($this->name." config parameter for \"$key\" expected.", E_USER_ERROR);
exit();
}
$value = $this->strategy[$key];
if (empty($value) || $value == $not) {
trigger_error($this->name." config parameter for \"$key\" expected.", E_USER_ERROR);
exit();
}
return $value;
}
/**
* Loads a default value into $strategy if the associated key is not found
*
* @param string $key Configuration key to be loaded
* @param string $default Default value for the configuration key if none is set by the user
* @return mixed The loaded value
*/
protected function optional($key, $default = null) {
if (!array_key_exists($key, $this->strategy)) {
$this->strategy[$key] = $default;
return $default;
}
else return $this->strategy[$key];
}
/**
* Security: Sign $auth before redirecting to callback_url
*
* @param string $timestamp ISO 8601 formatted date
* @return string Resulting signature
*/
protected function sign($timestamp = null) {
if (is_null($timestamp)) $timestamp = date('c');
$input = sha1(print_r($this->auth, true));
$hash = $this->hash($input, $timestamp, $this->env['security_iteration'], $this->env['security_salt']);
return $hash;
}
/**
* Maps user profile to auth response
*
* @param array $profile User profile obtained from provider
* @param string $profile_path Path to a $profile property. Use dot(.) to separate levels.
* eg. Path to $profile['a']['b']['c'] would be 'a.b.c'
* @param string $auth_path Path to $this->auth that is to be set.
*/
protected function mapProfile($profile, $profile_path, $auth_path) {
$from = explode('.', $profile_path);
$base = $profile;
foreach ($from as $element) {
if (is_array($base) && array_key_exists($element, $base)) {
$base = $base[$element];
} else {
return false;
}
}
$value = $base;
$to = explode('.', $auth_path);
$auth = &$this->auth;
foreach ($to as $element) {
$auth = &$auth[$element];
}
$auth = $value;
return true;
}
/**
* *****************************************************
* Utilities
* A collection of static functions for strategy's use
* *****************************************************
*/
/**
* Static hashing function
*
* @param string $input Input string
* @param string $timestamp ISO 8601 formatted date
* @param int $iteration Number of hash iterations
* @param string $salt
* @return string Resulting hash
*/
public static function hash($input, $timestamp, $iteration, $salt) {
$iteration = intval($iteration);
if ($iteration <= 0) {
return false;
}
for ($i = 0; $i < $iteration; ++$i) {
$input = base_convert(sha1($input.$salt.$timestamp), 16, 36);
}
return $input;
}
/**
* Redirect to $url with HTTP header (Location: )
*
* @param string $url URL to redirect user to
* @param boolean $exit Whether to call exit() right after redirection
*/
public static function redirect($url, $exit = true) {
header("Location: $url");
if ($exit) {
exit();
}
}
/**
* Client-side GET: This function builds the full HTTP URL with parameters and redirects via Location header.
*
* @param string $url Destination URL
* @param array $data Data
* @param boolean $exit Whether to call exit() right after redirection
*/
public static function clientGet($url, $data = array(), $exit = true) {
self::redirect($url.'?'.http_build_query($data, '', '&'), $exit);
}
/**
* Generates a simple HTML form with $data initialized and post results via JavaScript
*
* @param string $url URL to be POSTed
* @param array $data Data to be POSTed
*/
public static function clientPost($url, $data = array()) {
$html = '<html><body onload="postit();"><form name="auth" method="post" action="'.$url.'">';
if (!empty($data) && is_array($data)) {
$flat = self::flattenArray($data);
foreach ($flat as $key => $value) {
$html .= '<input type="hidden" name="'.$key.'" value="'.$value.'">';
}
}
$html .= '</form>';
$html .= '<script type="text/javascript">function postit(){ document.auth.submit(); }</script>';
$html .= '</body></html>';
echo $html;
exit;
}
/**
* Basic server-side HTTP GET request via self::httpRequest(), wrapper of file_get_contents
*
* @param string $url Destination URL
* @param array $data Data to be submitted via GET
* @param array $options Additional stream context options, if any
* @param string $responseHeaders Response headers after HTTP call. Useful for error debugging.
* @return string Content resulted from request, without headers
*/
public static function serverGet($url, $data, $options = null, &$responseHeaders = null) {
return self::httpRequest($url.'?'.http_build_query($data, '', '&'), $options, $responseHeaders);
}
/**
* Basic server-side HTTP POST request via self::httpRequest(), wrapper of file_get_contents
*
* @param string $url Destination URL
* @param array $data Data to be POSTed
* @param array $options Additional stream context options, if any
* @param string $responseHeaders Response headers after HTTP call. Useful for error debugging.
* @return string Content resulted from request, without headers
*/
public static function serverPost($url, $data, $options = array(), &$responseHeaders = null) {
if (!is_array($options)) {
$options = array();
}
$query = http_build_query($data, '', '&');
$stream = array('http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded",
'content' => $query
));
$stream = self::arrayReplaceRecursive($stream, $options);
return self::httpRequest($url, $stream, $responseHeaders);
}
/**
* Simple server-side HTTP request with file_get_contents
* Provides basic HTTP calls.
* See serverGet() and serverPost() for wrapper functions of httpRequest()
*
* Notes:
* Reluctant to use any more advanced transport like cURL for the time being to not
* having to set cURL as being a requirement.
* Strategy is to provide own HTTP transport handler if requiring more advanced support.
*
* @param string $url Full URL to load
* @param array $options Stream context options (http://php.net/stream-context-create)
* @param string $responseHeaders Response headers after HTTP call. Useful for error debugging.
* @return string Content resulted from request, without headers
*/
public static function httpRequest($url, $options = null, &$responseHeaders = null) {
$context = null;
if (!empty($options) && is_array($options)) {
if (empty($options['http']['header'])) {
$options['http']['header'] = "User-Agent: opauth";
} else {
$options['http']['header'] .= "\r\nUser-Agent: opauth";
}
} else {
$options = array('http' => array('header' => 'User-Agent: opauth'));
}
$context = stream_context_create($options);
$content = file_get_contents($url, false, $context);
$responseHeaders = implode("\r\n", $http_response_header);
return $content;
}
/**
* Recursively converts object into array
* Basically get_object_vars, but recursive.
*
* @param mixed $obj Object
* @return array Array of object properties
*/
public static function recursiveGetObjectVars($obj) {
$arr = array();
$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
foreach ($_arr as $key => $val) {
$val = (is_array($val) || is_object($val)) ? self::recursiveGetObjectVars($val) : $val;
// Transform boolean into 1 or 0 to make it safe across all Opauth HTTP transports
if (is_bool($val)) $val = ($val) ? 1 : 0;
$arr[$key] = $val;
}
return $arr;
}
/**
* Recursively converts multidimensional array into POST-friendly single dimensional array
*
* @param array $array Array to be flatten
* @param string $prefix String to be prefixed to flattened variable name
* @param array $results Existing array of flattened inputs to be merged upon
*
* @return array A single dimensional array with POST-friendly name
*/
public static function flattenArray($array, $prefix = null, $results = array()) {
foreach ($array as $key => $val) {
$name = (empty($prefix)) ? $key : $prefix."[$key]";
if (is_array($val)) {
$results = array_merge($results, self::flattenArray($val, $name));
} else {
$results[$name] = $val;
}
}
return $results;
}
/**
* Replace defined env values enclosed in {} with values from $dictionary
*
* @param string $value Input string
* @param array $dictionary Dictionary to lookup values from
* @return string String substituted with value from dictionary, if applicable
*/
public static function envReplace($value, $dictionary) {
if (is_string($value) && preg_match_all('/{([A-Za-z0-9-_]+)}/', $value, $matches)) {
foreach ($matches[1] as $key) {
if (array_key_exists($key, $dictionary)) {
$value = str_replace('{'.$key.'}', $dictionary[$key], $value);
}
}
return $value;
}
return $value;
}
/**
* array_replace_recursive() polyfill for PHP 5.2
* From: http://sg.php.net/manual/en/function.array-replace-recursive.php#92574
*
* @param array $array The array in which elements are replaced.
* @param array $array1 The array from which elements will be extracted
* @return array Returns an array or null if an error occurs.
*/
public static function arrayReplaceRecursive($array, $array1) {
if (!function_exists('array_replace_recursive')) {
function array_replace_recursive($array, $array1) {
function recurse($array, $array1) {
foreach ($array1 as $key => $value) {
if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key]))) {
$array[$key] = array();
}
if (is_array($value)) {
$value = recurse($array[$key], $value);
}
$array[$key] = $value;
}
return $array;
}
$args = func_get_args();
$array = $args[0];
if (!is_array($array)) {
return $array;
}
for ($i = 1; $i < count($args); $i++) {
if (is_array($args[$i])) {
$array = recurse($array, $args[$i]);
}
}
return $array;
}
}
return array_replace_recursive($array, $array1);
}
}