26
26
* @see http://code.google.com/p/oauth2-php/
27
27
*/
28
28
29
- ini_set ('display_errors ' , 1 );
30
- error_reporting (E_ALL | E_STRICT );
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
+
31
45
32
46
/**
33
47
* @defgroup oauth2_section_2 Client Credentials
@@ -326,11 +340,6 @@ define("OAUTH2_ERROR_EXPIRED_TOKEN", "expired_token");
326
340
*/
327
341
define ("OAUTH2_ERROR_INSUFFICIENT_SCOPE " , "insufficient_scope " );
328
342
329
- /**
330
- * Whether to show verbose error messages in the JSON response.
331
- */
332
- define ("OAUTH2_ERROR_VERBOSE " , TRUE );
333
-
334
343
/**
335
344
* @}
336
345
*/
@@ -344,6 +353,43 @@ define("OAUTH2_ERROR_VERBOSE", TRUE);
344
353
*/
345
354
abstract class OAuth2 {
346
355
356
+ /**
357
+ * Array of persistent variables stored.
358
+ */
359
+ protected $ conf = array ();
360
+
361
+ /**
362
+ * Returns a persistent variable.
363
+ *
364
+ * To avoid problems, always use lower case for persistent variable names.
365
+ *
366
+ * @param $name
367
+ * The name of the variable to return.
368
+ * @param $default
369
+ * The default value to use if this variable has never been set.
370
+ *
371
+ * @return
372
+ * The value of the variable.
373
+ */
374
+ public function getVariable ($ name , $ default = NULL ) {
375
+ return isset ($ this ->conf [$ name ]) ? $ this ->conf [$ name ] : $ default ;
376
+ }
377
+
378
+ /**
379
+ * Sets a persistent variable.
380
+ *
381
+ * To avoid problems, always use lower case for persistent variable names.
382
+ *
383
+ * @param $name
384
+ * The name of the variable to set.
385
+ * @param $value
386
+ * The value to set.
387
+ */
388
+ public function setVariable ($ name , $ value ) {
389
+ $ this ->conf [$ name ] = $ value ;
390
+ return $ this ;
391
+ }
392
+
347
393
// Subclasses must implement the following functions.
348
394
349
395
/**
@@ -771,98 +817,6 @@ abstract class OAuth2 {
771
817
772
818
// End stuff that should get overridden.
773
819
774
- /**
775
- * The duration in seconds of the access token lifetime.
776
- */
777
- protected $ access_token_lifetime = 3600 ;
778
-
779
- /**
780
- * The duration in seconds of the authorization code lifetime.
781
- */
782
- protected $ auth_code_lifetime = 30 ;
783
-
784
- /**
785
- * The duration in seconds of the refresh token lifetime.
786
- */
787
- protected $ refresh_token_lifetime = 1209600 ; // Two weeks
788
-
789
- /**
790
- * Legacy refresh token to be expired.
791
- */
792
- private $ _old_refresh_token = '' ;
793
-
794
- /**
795
- * Get the access token lifetime.
796
- *
797
- * @return
798
- * Lifetime of access token in seconds.
799
- */
800
- public function getAccessTokenLifetime () {
801
- return $ this ->access_token_lifetime ;
802
- }
803
-
804
- /**
805
- * Set the access token lifetime.
806
- *
807
- * @param $access_token_lifetime
808
- * Lifetime of access token in seconds.
809
- *
810
- * @return
811
- * The current OAuth2.0 server-side instance.
812
- */
813
- public function setAccessTokenLifetime ($ access_token_lifetime ) {
814
- $ this ->access_token_lifetime = $ access_token_lifetime ;
815
- return $ this ;
816
- }
817
-
818
- /**
819
- * Get the authorization code lifetime.
820
- *
821
- * @return
822
- * Lifetime of authorization code in seconds.
823
- */
824
- public function getAuthCodeLifetime () {
825
- return $ this ->auth_code_lifetime ;
826
- }
827
-
828
- /**
829
- * Set the authorization code lifetime.
830
- *
831
- * @param $auth_code_lifetime
832
- * Lifetime of authorization code in seconds.
833
- *
834
- * @return
835
- * The current OAuth2.0 server-side instance.
836
- */
837
- public function setAuthCodeLifetime ($ auth_code_lifetime ) {
838
- $ this ->auth_code_lifetime = $ auth_code_lifetime ;
839
- return $ this ;
840
- }
841
-
842
- /**
843
- * Get the refresh token lifetime.
844
- *
845
- * @return
846
- * Lifetime of refresh token in seconds.
847
- */
848
- public function getRefreshTokenLifetime () {
849
- return $ this ->refresh_token_lifetime ;
850
- }
851
-
852
- /**
853
- * Set the refresh token lifetime.
854
- *
855
- * @param $refresh_token_lifetime
856
- * Lifetime of refresh token in seconds.
857
- *
858
- * @return
859
- * The current OAuth2.0 server-side instance.
860
- */
861
- public function setRefreshTokenLifetime ($ refresh_token_lifetime ) {
862
- $ this ->refresh_token_lifetime = $ refresh_token_lifetime ;
863
- return $ this ;
864
- }
865
-
866
820
/**
867
821
* Creates an OAuth2.0 server-side instance.
868
822
*
@@ -874,11 +828,13 @@ abstract class OAuth2 {
874
828
* seconds.
875
829
* - refresh_token_lifetime: (optional) The lifetime of refresh token in
876
830
* seconds.
831
+ * - display_error: (optional) Whether to show verbose error messages in
832
+ * the response.
877
833
*/
878
834
public function __construct ($ config = array ()) {
879
- $ this -> setAccessTokenLifetime ( isset ( $ config[ ' access_token_lifetime ' ]) ? $ config [ ' access_token_lifetime ' ] : $ this -> getAccessTokenLifetime ());
880
- $ this ->setAuthCodeLifetime ( isset ( $ config [ ' auth_code_lifetime ' ]) ? $ config [ ' auth_code_lifetime ' ] : $ this -> getAuthCodeLifetime () );
881
- $ this -> setRefreshTokenLifetime ( isset ( $ config [ ' refresh_token_lifetime ' ]) ? $ config [ ' refresh_token_lifetime ' ] : $ this -> getRefreshTokenLifetime ());
835
+ foreach ( $ config as $ name => $ value ) {
836
+ $ this ->setVariable ( $ name , $ value );
837
+ }
882
838
}
883
839
884
840
// Resource protecting (Section 5).
@@ -1108,7 +1064,7 @@ abstract class OAuth2 {
1108
1064
$ this ->errorJsonResponse (OAUTH2_HTTP_BAD_REQUEST , OAUTH2_ERROR_EXPIRED_TOKEN );
1109
1065
1110
1066
// store the refresh token locally so we can delete it when a new refresh token is generated
1111
- $ this ->_old_refresh_token = $ stored ["token " ];
1067
+ $ this ->setVariable ( ' _old_refresh_token ' , $ stored ["token " ]) ;
1112
1068
1113
1069
break ;
1114
1070
case OAUTH2_GRANT_TYPE_NONE :
@@ -1364,19 +1320,19 @@ abstract class OAuth2 {
1364
1320
protected function createAccessToken ($ client_id , $ scope = NULL ) {
1365
1321
$ token = array (
1366
1322
"access_token " => $ this ->genAccessToken (),
1367
- "expires_in " => $ this ->getAccessTokenLifetime ( ),
1323
+ "expires_in " => $ this ->getVariable ( ' access_token_lifetime ' , OAUTH2_DEFAULT_ACCESS_TOKEN_LIFETIME ),
1368
1324
"scope " => $ scope
1369
1325
);
1370
1326
1371
- $ this ->setAccessToken ($ token ["access_token " ], $ client_id , time () + $ this ->getAccessTokenLifetime ( ), $ scope );
1327
+ $ this ->setAccessToken ($ token ["access_token " ], $ client_id , time () + $ this ->getVariable ( ' access_token_lifetime ' , OAUTH2_DEFAULT_ACCESS_TOKEN_LIFETIME ), $ scope );
1372
1328
1373
1329
// Issue a refresh token also, if we support them
1374
1330
if (in_array (OAUTH2_GRANT_TYPE_REFRESH_TOKEN , $ this ->getSupportedGrantTypes ())) {
1375
1331
$ token ["refresh_token " ] = $ this ->genAccessToken ();
1376
- $ this ->setRefreshToken ($ token ["refresh_token " ], $ client_id , time () + $ this ->getRefreshTokenLifetime ( ), $ scope );
1332
+ $ this ->setRefreshToken ($ token ["refresh_token " ], $ client_id , time () + $ this ->getVariable ( ' refresh_token_lifetime ' , OAUTH2_DEFAULT_REFRESH_TOKEN_LIFETIME ), $ scope );
1377
1333
// If we've granted a new refresh token, expire the old one
1378
- if ($ this ->_old_refresh_token )
1379
- $ this ->unsetRefreshToken ($ this ->_old_refresh_token );
1334
+ if ($ this ->getVariable ( ' _old_refresh_token ' ) )
1335
+ $ this ->unsetRefreshToken ($ this ->getVariable ( ' _old_refresh_token ' ) );
1380
1336
}
1381
1337
1382
1338
return $ token ;
@@ -1400,7 +1356,7 @@ abstract class OAuth2 {
1400
1356
*/
1401
1357
private function createAuthCode ($ client_id , $ redirect_uri , $ scope = NULL ) {
1402
1358
$ code = $ this ->genAuthCode ();
1403
- $ this ->setAuthCode ($ code , $ client_id , $ redirect_uri , time () + $ this ->getAuthCodeLifetime ( ), $ scope );
1359
+ $ this ->setAuthCode ($ code , $ client_id , $ redirect_uri , time () + $ this ->getVariable ( ' auth_code_lifetime ' , OAUTH2_DEFAULT_AUTH_CODE_LIFETIME ), $ scope );
1404
1360
return $ code ;
1405
1361
}
1406
1362
@@ -1504,10 +1460,10 @@ abstract class OAuth2 {
1504
1460
if ($ state )
1505
1461
$ result ["query " ]["state " ] = $ state ;
1506
1462
1507
- if (OAUTH2_ERROR_VERBOSE && $ error_description )
1463
+ if ($ this -> getVariable ( ' display_error ' ) && $ error_description )
1508
1464
$ result ["query " ]["error_description " ] = $ error_description ;
1509
1465
1510
- if (OAUTH2_ERROR_VERBOSE && $ error_uri )
1466
+ if ($ this -> getVariable ( ' display_error ' ) && $ error_uri )
1511
1467
$ result ["query " ]["error_uri " ] = $ error_uri ;
1512
1468
1513
1469
$ this ->doRedirectUriCallback ($ redirect_uri , $ result );
@@ -1536,10 +1492,10 @@ abstract class OAuth2 {
1536
1492
private function errorJsonResponse ($ http_status_code , $ error , $ error_description = NULL , $ error_uri = NULL ) {
1537
1493
$ result ['error ' ] = $ error ;
1538
1494
1539
- if (OAUTH2_ERROR_VERBOSE && $ error_description )
1495
+ if ($ this -> getVariable ( ' display_error ' ) && $ error_description )
1540
1496
$ result ["error_description " ] = $ error_description ;
1541
1497
1542
- if (OAUTH2_ERROR_VERBOSE && $ error_uri )
1498
+ if ($ this -> getVariable ( ' display_error ' ) && $ error_uri )
1543
1499
$ result ["error_uri " ] = $ error_uri ;
1544
1500
1545
1501
header ("HTTP/1.1 " . $ http_status_code );
@@ -1587,10 +1543,10 @@ abstract class OAuth2 {
1587
1543
if ($ error )
1588
1544
$ result .= ", error=' " . $ error . "' " ;
1589
1545
1590
- if (OAUTH2_ERROR_VERBOSE && $ error_description )
1546
+ if ($ this -> getVariable ( ' display_error ' ) && $ error_description )
1591
1547
$ result .= ", error_description=' " . $ error_description . "' " ;
1592
1548
1593
- if (OAUTH2_ERROR_VERBOSE && $ error_uri )
1549
+ if ($ this -> getVariable ( ' display_error ' ) && $ error_uri )
1594
1550
$ result .= ", error_uri=' " . $ error_uri . "' " ;
1595
1551
1596
1552
if ($ scope )
0 commit comments