Skip to content

Commit 982e135

Browse files
committed
* introduce Drupal style getVariable() and setVariable, replace legacy variable get/set functions.
* remove hardcode PHP display_error and errror_reporting, as this should be manually implement within 3rd party integration. * make verbose error as configurable and default disable, as this should be manually enable within 3rd party integration. * add lib/OAuth2Client.inc and lib/OAuth2Exception.inc for client-side implementation.
1 parent 9cd57ac commit 982e135

File tree

5 files changed

+887
-117
lines changed

5 files changed

+887
-117
lines changed

CHANGELOG.txt

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
oauth2-php revision xxx, xxxx-xx-xx (development version)
22
----------------------
3+
* introduce Drupal style getVariable() and setVariable, replace legacy
4+
variable get/set functions.
5+
* remove hardcode PHP display_error and errror_reporting, as this should
6+
be manually implement within 3rd party integration.
7+
* make verbose error as configurable and default disable, as this should
8+
be manually enable within 3rd party integration.
9+
* add lib/OAuth2Client.inc and lib/OAuth2Exception.inc for client-side
10+
implementation.
311

412
oauth2-php revision 21, 2010-12-18
513
----------------------

config.doxy

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PROJECT_NAME = oauth2-php
3131
# This could be handy for archiving the generated documentation or
3232
# if some version control system is used.
3333

34-
PROJECT_NUMBER = 4fa75a8c81
34+
PROJECT_NUMBER = draft-ietf-oauth-v2-10
3535

3636
# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute)
3737
# base path where the generated documentation will be put.
@@ -607,7 +607,7 @@ INPUT_ENCODING = UTF-8
607607
# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx
608608
# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.py *.f90
609609

610-
FILE_PATTERNS = *.php \ *.module \ *.inc \ *.install \ *.js \ *.theme
610+
FILE_PATTERNS = *.php \ *.module \ *.inc \ *.install \ *.js \ *.theme \ *.test
611611

612612
# The RECURSIVE tag can be used to turn specify whether or not subdirectories
613613
# should be searched for input files as well. Possible values are YES and NO.

lib/OAuth2.inc

+71-115
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,22 @@
2626
* @see http://code.google.com/p/oauth2-php/
2727
*/
2828

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+
3145

3246
/**
3347
* @defgroup oauth2_section_2 Client Credentials
@@ -326,11 +340,6 @@ define("OAUTH2_ERROR_EXPIRED_TOKEN", "expired_token");
326340
*/
327341
define("OAUTH2_ERROR_INSUFFICIENT_SCOPE", "insufficient_scope");
328342

329-
/**
330-
* Whether to show verbose error messages in the JSON response.
331-
*/
332-
define("OAUTH2_ERROR_VERBOSE", TRUE);
333-
334343
/**
335344
* @}
336345
*/
@@ -344,6 +353,43 @@ define("OAUTH2_ERROR_VERBOSE", TRUE);
344353
*/
345354
abstract class OAuth2 {
346355

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+
347393
// Subclasses must implement the following functions.
348394

349395
/**
@@ -771,98 +817,6 @@ abstract class OAuth2 {
771817

772818
// End stuff that should get overridden.
773819

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-
866820
/**
867821
* Creates an OAuth2.0 server-side instance.
868822
*
@@ -874,11 +828,13 @@ abstract class OAuth2 {
874828
* seconds.
875829
* - refresh_token_lifetime: (optional) The lifetime of refresh token in
876830
* seconds.
831+
* - display_error: (optional) Whether to show verbose error messages in
832+
* the response.
877833
*/
878834
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+
}
882838
}
883839

884840
// Resource protecting (Section 5).
@@ -1108,7 +1064,7 @@ abstract class OAuth2 {
11081064
$this->errorJsonResponse(OAUTH2_HTTP_BAD_REQUEST, OAUTH2_ERROR_EXPIRED_TOKEN);
11091065

11101066
// 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"]);
11121068

11131069
break;
11141070
case OAUTH2_GRANT_TYPE_NONE:
@@ -1364,19 +1320,19 @@ abstract class OAuth2 {
13641320
protected function createAccessToken($client_id, $scope = NULL) {
13651321
$token = array(
13661322
"access_token" => $this->genAccessToken(),
1367-
"expires_in" => $this->getAccessTokenLifetime(),
1323+
"expires_in" => $this->getVariable('access_token_lifetime', OAUTH2_DEFAULT_ACCESS_TOKEN_LIFETIME),
13681324
"scope" => $scope
13691325
);
13701326

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);
13721328

13731329
// Issue a refresh token also, if we support them
13741330
if (in_array(OAUTH2_GRANT_TYPE_REFRESH_TOKEN, $this->getSupportedGrantTypes())) {
13751331
$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);
13771333
// 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'));
13801336
}
13811337

13821338
return $token;
@@ -1400,7 +1356,7 @@ abstract class OAuth2 {
14001356
*/
14011357
private function createAuthCode($client_id, $redirect_uri, $scope = NULL) {
14021358
$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);
14041360
return $code;
14051361
}
14061362

@@ -1504,10 +1460,10 @@ abstract class OAuth2 {
15041460
if ($state)
15051461
$result["query"]["state"] = $state;
15061462

1507-
if (OAUTH2_ERROR_VERBOSE && $error_description)
1463+
if ($this->getVariable('display_error') && $error_description)
15081464
$result["query"]["error_description"] = $error_description;
15091465

1510-
if (OAUTH2_ERROR_VERBOSE && $error_uri)
1466+
if ($this->getVariable('display_error') && $error_uri)
15111467
$result["query"]["error_uri"] = $error_uri;
15121468

15131469
$this->doRedirectUriCallback($redirect_uri, $result);
@@ -1536,10 +1492,10 @@ abstract class OAuth2 {
15361492
private function errorJsonResponse($http_status_code, $error, $error_description = NULL, $error_uri = NULL) {
15371493
$result['error'] = $error;
15381494

1539-
if (OAUTH2_ERROR_VERBOSE && $error_description)
1495+
if ($this->getVariable('display_error') && $error_description)
15401496
$result["error_description"] = $error_description;
15411497

1542-
if (OAUTH2_ERROR_VERBOSE && $error_uri)
1498+
if ($this->getVariable('display_error') && $error_uri)
15431499
$result["error_uri"] = $error_uri;
15441500

15451501
header("HTTP/1.1 " . $http_status_code);
@@ -1587,10 +1543,10 @@ abstract class OAuth2 {
15871543
if ($error)
15881544
$result .= ", error='" . $error . "'";
15891545

1590-
if (OAUTH2_ERROR_VERBOSE && $error_description)
1546+
if ($this->getVariable('display_error') && $error_description)
15911547
$result .= ", error_description='" . $error_description . "'";
15921548

1593-
if (OAUTH2_ERROR_VERBOSE && $error_uri)
1549+
if ($this->getVariable('display_error') && $error_uri)
15941550
$result .= ", error_uri='" . $error_uri . "'";
15951551

15961552
if ($scope)

0 commit comments

Comments
 (0)