27
27
*/
28
28
29
29
30
- /**
31
- * The default duration in seconds of the access token lifetime.
32
- */
33
- define ("OAUTH2_DEFAULT_ACCESS_TOKEN_LIFETIME " , 3600 );
34
-
35
- /**
36
- * The default duration in seconds of the authorization code lifetime.
37
- */
38
- define ("OAUTH2_DEFAULT_AUTH_CODE_LIFETIME " , 30 );
39
-
40
- /**
41
- * The default duration in seconds of the refresh token lifetime.
42
- */
43
- define ("OAUTH2_DEFAULT_REFRESH_TOKEN_LIFETIME " , 1209600 );
44
-
45
-
46
30
/**
47
31
* @defgroup oauth2_section_2 Client Credentials
48
32
* @{
85
69
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-10#section-3
86
70
*/
87
71
88
- /**
89
- * Denotes "token" authorization response type.
90
- */
91
- define ("OAUTH2_AUTH_RESPONSE_TYPE_ACCESS_TOKEN " , "token " );
92
-
93
- /**
94
- * Denotes "code" authorization response type.
95
- */
96
- define ("OAUTH2_AUTH_RESPONSE_TYPE_AUTH_CODE " , "code " );
97
-
98
- /**
99
- * Denotes "code-and-token" authorization response type.
100
- */
101
- define ("OAUTH2_AUTH_RESPONSE_TYPE_CODE_AND_TOKEN " , "code-and-token " );
102
-
103
72
/**
104
73
* Regex to filter out the authorization response type.
105
74
*/
@@ -359,145 +328,89 @@ class OAuth2 {
359
328
protected $ conf = array ();
360
329
361
330
/**
362
- *
331
+ * Storage engine for authentication server
363
332
*
364
333
* @var IOAuth2Storage
365
334
*/
366
335
protected $ storage ;
367
-
368
- // Stuff that can get overridden by subclasses.
369
- // Override the ones you need.
370
-
371
- /**
372
- * Return supported authorization response types.
373
- *
374
- * You should override this function with your supported response types.
375
- *
376
- * @return
377
- * A list as below. If you support all authorization response types,
378
- * then you'd do:
379
- * @code
380
- * return array(
381
- * OAUTH2_AUTH_RESPONSE_TYPE_AUTH_CODE,
382
- * OAUTH2_AUTH_RESPONSE_TYPE_ACCESS_TOKEN,
383
- * OAUTH2_AUTH_RESPONSE_TYPE_CODE_AND_TOKEN,
384
- * );
385
- * @endcode
386
- *
387
- * @ingroup oauth2_section_3
388
- */
389
- protected function getSupportedAuthResponseTypes () {
390
- return array (
391
- OAUTH2_AUTH_RESPONSE_TYPE_AUTH_CODE ,
392
- OAUTH2_AUTH_RESPONSE_TYPE_ACCESS_TOKEN ,
393
- OAUTH2_AUTH_RESPONSE_TYPE_CODE_AND_TOKEN
394
- );
395
- }
396
-
336
+
397
337
/**
398
- * Return supported scopes.
399
- *
400
- * If you want to support scope use, then have this function return a list
401
- * of all acceptable scopes (used to throw the invalid-scope error).
402
- *
403
- * @return
404
- * A list as below, for example:
405
- * @code
406
- * return array(
407
- * 'my-friends',
408
- * 'photos',
409
- * 'whatever-else',
410
- * );
411
- * @endcode
412
- *
413
- * @ingroup oauth2_section_3
338
+ * Keep track of the old refresh token. So we can unset
339
+ * the old refresh tokens when a new one is issued.
340
+ *
341
+ * @var string
414
342
*/
415
- protected function getSupportedScopes () {
416
- return array ();
417
- }
343
+ protected $ oldRefreshToken ;
418
344
419
345
/**
420
- * Check restricted authorization response types of corresponding Client
421
- * identifier.
422
- *
423
- * If you want to restrict clients to certain authorization response types,
424
- * override this function.
425
- *
426
- * @param $client_id
427
- * Client identifier to be check with.
428
- * @param $response_type
429
- * Authorization response type to be check with, would be one of the
430
- * values contained in OAUTH2_AUTH_RESPONSE_TYPE_REGEXP.
431
- *
432
- * @return
433
- * TRUE if the authorization response type is supported by this
434
- * client identifier, and FALSE if it isn't.
435
- *
436
- * @ingroup oauth2_section_3
346
+ * Default values for configuration options.
347
+ *
348
+ * @var int
349
+ * @see OAuth2::setDefaultOptions()
437
350
*/
438
- protected function checkRestrictedAuthResponseType ($ client_id , $ response_type ) {
439
- return TRUE ;
440
- }
441
-
351
+ const DEFAULT_ACCESS_TOKEN_LIFETIME = 3600 ;
352
+ const DEFAULT_REFRESH_TOKEN_LIFETIME = 30 ;
353
+ const DEFAULT_AUTH_CODE_LIFETIME = 1209600 ;
354
+ const DEFAULT_WWW_REALM = 'Service ' ;
355
+
442
356
/**
443
- * Check restricted grant types of corresponding client identifier.
444
- *
445
- * If you want to restrict clients to certain grant types, override this
446
- * function.
447
- *
448
- * @param $client_id
449
- * Client identifier to be check with.
450
- * @param $grant_type
451
- * Grant type to be check with, would be one of the values contained in
452
- * OAUTH2_GRANT_TYPE_REGEXP.
453
- *
454
- * @return
455
- * TRUE if the grant type is supported by this client identifier, and
456
- * FALSE if it isn't.
457
- *
458
- * @ingroup oauth2_section_4
357
+ * Configurable options.
358
+ *
359
+ * @var string
459
360
*/
460
- protected function checkRestrictedGrantType ($ client_id , $ grant_type ) {
461
- return TRUE ;
462
- }
463
-
361
+ const CONFIG_ACCESS_LIFETIME = 'access_token_lifetime ' ; // The lifetime of access token in seconds.
362
+ const CONFIG_REFRESH_LIFETIME = 'refresh_token_lifetime ' ; // The lifetime of refresh token in seconds.
363
+ const CONFIG_AUTH_LIFETIME = 'auth_code_lifetime ' ; // The lifetime of auth code in seconds.
364
+ const CONFIG_DISPLAY_ERROR = 'display_error ' ; // Whether to show verbose error messages in the response.
365
+ const CONFIG_SUPPORTED_AUTH = 'supported_auth_types ' ; // Array of supported auth types
366
+ const CONFIG_SUPPORTED_SCOPES = 'supported_scopes ' ; // Array of scopes you want to support
367
+ const CONFIG_DEFAULT_REALM = 'default_auth_realm ' ; // Realm you want to send in a WWW-Authenticate header
368
+
464
369
/**
465
- * Get default authentication realm for WWW-Authenticate header.
466
- *
467
- * Change this to whatever authentication realm you want to send in a
468
- * WWW-Authenticate header.
469
- *
470
- * @return
471
- * A string that you want to send in a WWW-Authenticate header.
472
- *
473
- * @ingroup oauth2_error
370
+ * List of possible authentication response types.
371
+ * You can specify the CONFIG_SUPPORTED_AUTH array with one or
372
+ * more the below options.
373
+ *
374
+ * @var string
375
+ * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-10#section-3
474
376
*/
475
- protected function getDefaultAuthenticationRealm () {
476
- return "Service " ;
477
- }
478
-
479
- // End stuff that should get overridden.
480
-
377
+ const AUTH_RESPONSE_TYPE_AUTH_CODE = 'code ' ;
378
+ const AUTH_RESPONSE_TYPE_ACCESS_TOKEN = 'token ' ;
379
+ const AUTH_RESPONSE_TYPE_CODE_AND_TOKEN = 'code-and-token ' ;
380
+
481
381
/**
482
382
* Creates an OAuth2.0 server-side instance.
483
383
*
484
- * @param $config
485
- * An associative array as below:
486
- * - access_token_lifetime: (optional) The lifetime of access token in
487
- * seconds.
488
- * - auth_code_lifetime: (optional) The lifetime of authorization code in
489
- * seconds.
490
- * - refresh_token_lifetime: (optional) The lifetime of refresh token in
491
- * seconds.
492
- * - display_error: (optional) Whether to show verbose error messages in
493
- * the response.
384
+ * @param $config - An associative array as below of config options. See CONFIG_* constants.
494
385
*/
495
386
public function __construct (IOAuth2Storage $ storage , $ config = array ()) {
496
387
$ this ->storage = $ storage ;
388
+
389
+ // Configuration options
390
+ $ this ->setDefaultOptions ();
497
391
foreach ($ config as $ name => $ value ) {
498
392
$ this ->setVariable ($ name , $ value );
499
393
}
500
394
}
395
+
396
+ /**
397
+ * Default configuration options are specified here.
398
+ */
399
+ protected function setDefaultOptions () {
400
+ $ this ->conf = array (
401
+ self ::CONFIG_ACCESS_LIFETIME => self ::DEFAULT_ACCESS_TOKEN_LIFETIME ,
402
+ self ::CONFIG_REFRESH_LIFETIME => self ::DEFAULT_REFRESH_TOKEN_LIFETIME ,
403
+ self ::CONFIG_AUTH_LIFETIME => self ::DEFAULT_AUTH_CODE_LIFETIME ,
404
+ self ::CONFIG_DEFAULT_REALM => self ::DEFAULT_REALM ,
405
+ self ::CONFIG_SUPPORTED_AUTH => array (
406
+ self ::AUTH_RESPONSE_TYPE_AUTH_CODE ,
407
+ self ::AUTH_RESPONSE_TYPE_ACCESS_TOKEN ,
408
+ self ::AUTH_RESPONSE_TYPE_CODE_AND_TOKEN
409
+ ),
410
+ // This is expected to be passed in on construction. Scopes can be an aribitrary string.
411
+ self ::CONFIG_SUPPORTED_SCOPES => array ()
412
+ );
413
+ }
501
414
502
415
/**
503
416
* Returns a persistent variable.
@@ -764,7 +677,7 @@ public function grantAccessToken(array $inputData = NULL) {
764
677
$ this ->errorJsonResponse (OAUTH2_HTTP_BAD_REQUEST , OAUTH2_ERROR_EXPIRED_TOKEN );
765
678
766
679
// store the refresh token locally so we can delete it when a new refresh token is generated
767
- $ this ->setVariable ( ' _old_refresh_token ' , $ stored ["refresh_token " ]) ;
680
+ $ this ->oldRefreshToken = $ stored ["refresh_token " ];
768
681
769
682
break ;
770
683
case OAUTH2_GRANT_TYPE_NONE :
@@ -946,10 +859,10 @@ public function finishClientAuthorization($is_authorized, $user_id = NULL, $para
946
859
$ result ["query " ]["error " ] = OAUTH2_ERROR_USER_DENIED ;
947
860
}
948
861
else {
949
- if ($ response_type == OAUTH2_AUTH_RESPONSE_TYPE_AUTH_CODE || $ response_type == OAUTH2_AUTH_RESPONSE_TYPE_CODE_AND_TOKEN )
862
+ if ($ response_type == self :: AUTH_RESPONSE_TYPE_AUTH_CODE || $ response_type == self :: AUTH_RESPONSE_TYPE_CODE_AND_TOKEN )
950
863
$ result ["query " ]["code " ] = $ this ->createAuthCode ($ client_id , $ user_id , $ redirect_uri , $ scope );
951
864
952
- if ($ response_type == OAUTH2_AUTH_RESPONSE_TYPE_ACCESS_TOKEN || $ response_type == OAUTH2_AUTH_RESPONSE_TYPE_CODE_AND_TOKEN )
865
+ if ($ response_type == self :: AUTH_RESPONSE_TYPE_ACCESS_TOKEN || $ response_type == self :: AUTH_RESPONSE_TYPE_CODE_AND_TOKEN )
953
866
$ result ["fragment " ] = $ this ->createAccessToken ($ client_id , $ user_id , $ scope );
954
867
}
955
868
@@ -1029,19 +942,20 @@ protected function createAccessToken($client_id, $user_id, $scope=NULL) {
1029
942
1030
943
$ token = array (
1031
944
"access_token " => $ this ->genAccessToken (),
1032
- "expires_in " => $ this ->getVariable (' access_token_lifetime ' , OAUTH2_DEFAULT_ACCESS_TOKEN_LIFETIME ),
945
+ "expires_in " => $ this ->getVariable (self :: CONFIG_ACCESS_LIFETIME ),
1033
946
"scope " => $ scope
1034
947
);
1035
948
1036
- $ this ->storage ->setAccessToken ($ token ["access_token " ], $ client_id , $ user_id , time () + $ this ->getVariable (' access_token_lifetime ' , OAUTH2_DEFAULT_ACCESS_TOKEN_LIFETIME ), $ scope );
949
+ $ this ->storage ->setAccessToken ($ token ["access_token " ], $ client_id , $ user_id , time () + $ this ->getVariable (self :: CONFIG_ACCESS_LIFETIME ), $ scope );
1037
950
1038
951
// Issue a refresh token also, if we support them
1039
952
if (in_array (OAUTH2_GRANT_TYPE_REFRESH_TOKEN , $ this ->storage ->getSupportedGrantTypes ())) {
1040
953
$ token ["refresh_token " ] = $ this ->genAccessToken ();
1041
- $ this ->storage ->setRefreshToken ($ token ["refresh_token " ], $ client_id , $ user_id , time () + $ this ->getVariable (' refresh_token_lifetime ' , OAUTH2_DEFAULT_REFRESH_TOKEN_LIFETIME ), $ scope );
954
+ $ this ->storage ->setRefreshToken ($ token ["refresh_token " ], $ client_id , $ user_id , time () + $ this ->getVariable (self :: CONFIG_REFRESH_LIFETIME ), $ scope );
1042
955
// If we've granted a new refresh token, expire the old one
1043
- if ($ this ->getVariable ('_old_refresh_token ' ))
1044
- $ this ->storage ->unsetRefreshToken ($ this ->getVariable ('_old_refresh_token ' ));
956
+ if ($ this ->oldRefreshToken )
957
+ $ this ->storage ->unsetRefreshToken ($ this ->oldRefreshToken );
958
+ unset($ this ->oldRefreshToken );
1045
959
}
1046
960
1047
961
return $ token ;
@@ -1065,7 +979,7 @@ protected function createAccessToken($client_id, $user_id, $scope=NULL) {
1065
979
*/
1066
980
private function createAuthCode ($ client_id , $ user_id , $ redirect_uri , $ scope = NULL ) {
1067
981
$ code = $ this ->genAuthCode ();
1068
- $ this ->storage ->setAuthCode ($ code , $ client_id , $ user_id , $ redirect_uri , time () + $ this ->getVariable (' auth_code_lifetime ' , OAUTH2_DEFAULT_AUTH_CODE_LIFETIME ), $ scope );
982
+ $ this ->storage ->setAuthCode ($ code , $ client_id , $ user_id , $ redirect_uri , time () + $ this ->getVariable (self :: CONFIG_AUTH_LIFETIME ), $ scope );
1069
983
return $ code ;
1070
984
}
1071
985
@@ -1169,10 +1083,10 @@ private function errorDoRedirectUriCallback($redirect_uri, $error, $error_descri
1169
1083
if ($ state )
1170
1084
$ result ["query " ]["state " ] = $ state ;
1171
1085
1172
- if ($ this ->getVariable (' display_error ' ) && $ error_description )
1086
+ if ($ this ->getVariable (self :: CONFIG_DISPLAY_ERROR ) && $ error_description )
1173
1087
$ result ["query " ]["error_description " ] = $ error_description ;
1174
1088
1175
- if ($ this ->getVariable (' display_error ' ) && $ error_uri )
1089
+ if ($ this ->getVariable (self :: CONFIG_DISPLAY_ERROR ) && $ error_uri )
1176
1090
$ result ["query " ]["error_uri " ] = $ error_uri ;
1177
1091
1178
1092
$ this ->doRedirectUriCallback ($ redirect_uri , $ result );
@@ -1201,10 +1115,10 @@ private function errorDoRedirectUriCallback($redirect_uri, $error, $error_descri
1201
1115
private function errorJsonResponse ($ http_status_code , $ error , $ error_description = NULL , $ error_uri = NULL ) {
1202
1116
$ result ['error ' ] = $ error ;
1203
1117
1204
- if ($ this ->getVariable (' display_error ' ) && $ error_description )
1118
+ if ($ this ->getVariable (self :: CONFIG_DISPLAY_ERROR ) && $ error_description )
1205
1119
$ result ["error_description " ] = $ error_description ;
1206
1120
1207
- if ($ this ->getVariable (' display_error ' ) && $ error_uri )
1121
+ if ($ this ->getVariable (self :: CONFIG_DISPLAY_ERROR ) && $ error_uri )
1208
1122
$ result ["error_uri " ] = $ error_uri ;
1209
1123
1210
1124
header ("HTTP/1.1 " . $ http_status_code );
@@ -1252,10 +1166,10 @@ private function errorWWWAuthenticateResponseHeader($http_status_code, $realm, $
1252
1166
if ($ error )
1253
1167
$ result .= ", error=' " . $ error . "' " ;
1254
1168
1255
- if ($ this ->getVariable (' display_error ' ) && $ error_description )
1169
+ if ($ this ->getVariable (self :: CONFIG_DISPLAY_ERROR ) && $ error_description )
1256
1170
$ result .= ", error_description=' " . $ error_description . "' " ;
1257
1171
1258
- if ($ this ->getVariable (' display_error ' ) && $ error_uri )
1172
+ if ($ this ->getVariable (self :: CONFIG_DISPLAY_ERROR ) && $ error_uri )
1259
1173
$ result .= ", error_uri=' " . $ error_uri . "' " ;
1260
1174
1261
1175
if ($ scope )
0 commit comments