From 031cf3064ae967658441f7e1e909b59994545961 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Mon, 2 Dec 2013 18:42:54 +0000 Subject: [PATCH] Moved some grant related functions into a trait to reduce duplicate code --- src/League/OAuth2/Server/Grant/AuthCode.php | 30 +------------ .../OAuth2/Server/Grant/ClientCredentials.php | 2 + src/League/OAuth2/Server/Grant/GrantTrait.php | 45 +++++++++++++++++++ .../Server/Grant/GrantTypeInterface.php | 12 ----- src/League/OAuth2/Server/Grant/Implicit.php | 30 +------------ src/League/OAuth2/Server/Grant/Password.php | 30 +------------ .../OAuth2/Server/Grant/RefreshToken.php | 30 +------------ 7 files changed, 55 insertions(+), 124 deletions(-) create mode 100644 src/League/OAuth2/Server/Grant/GrantTrait.php diff --git a/src/League/OAuth2/Server/Grant/AuthCode.php b/src/League/OAuth2/Server/Grant/AuthCode.php index 08f1a7c2..79a541af 100644 --- a/src/League/OAuth2/Server/Grant/AuthCode.php +++ b/src/League/OAuth2/Server/Grant/AuthCode.php @@ -24,6 +24,8 @@ use League\OAuth2\Server\Storage\ScopeInterface; */ class AuthCode implements GrantTypeInterface { + use GrantTrait; + /** * Grant identifier * @var string @@ -64,34 +66,6 @@ class AuthCode implements GrantTypeInterface { $this->authServer = $authServer; } - /** - * Return the identifier - * @return string - */ - public function getIdentifier() - { - return $this->identifier; - } - - /** - * Return the response type - * @return string - */ - public function getResponseType() - { - return $this->responseType; - } - - /** - * Override the default access token expire time - * @param int $accessTokenTTL - * @return void - */ - public function setAccessTokenTTL($accessTokenTTL) - { - $this->accessTokenTTL = $accessTokenTTL; - } - /** * Override the default access token expire time * @param int $authTokenTTL diff --git a/src/League/OAuth2/Server/Grant/ClientCredentials.php b/src/League/OAuth2/Server/Grant/ClientCredentials.php index 3d962ee9..4d53bf23 100644 --- a/src/League/OAuth2/Server/Grant/ClientCredentials.php +++ b/src/League/OAuth2/Server/Grant/ClientCredentials.php @@ -24,6 +24,8 @@ use League\OAuth2\Server\Storage\ScopeInterface; */ class ClientCredentials implements GrantTypeInterface { + use GrantTrait; + /** * Grant identifier * @var string diff --git a/src/League/OAuth2/Server/Grant/GrantTrait.php b/src/League/OAuth2/Server/Grant/GrantTrait.php new file mode 100644 index 00000000..e052ce57 --- /dev/null +++ b/src/League/OAuth2/Server/Grant/GrantTrait.php @@ -0,0 +1,45 @@ + + * @copyright Copyright (c) 2013 PHP League of Extraordinary Packages + * @license http://mit-license.org/ + * @link http://github.com/php-loep/oauth2-server + */ + +namespace League\OAuth2\Server\Grant; + +trait GrantTrait { + + /** + * Return the identifier + * @return string + */ + public function getIdentifier() + { + return $this->identifier; + } + + /** + * Return the response type + * @return string + */ + public function getResponseType() + { + return $this->responseType; + } + + /** + * Override the default access token expire time + * @param int $accessTokenTTL + * @return self + */ + public function setAccessTokenTTL($accessTokenTTL) + { + $this->accessTokenTTL = $accessTokenTTL; + return $this; + } + +} \ No newline at end of file diff --git a/src/League/OAuth2/Server/Grant/GrantTypeInterface.php b/src/League/OAuth2/Server/Grant/GrantTypeInterface.php index 2399c521..ec0b906b 100644 --- a/src/League/OAuth2/Server/Grant/GrantTypeInterface.php +++ b/src/League/OAuth2/Server/Grant/GrantTypeInterface.php @@ -28,18 +28,6 @@ interface GrantTypeInterface */ public function __construct(Authorization $authServer); - /** - * Returns the grant identifier (used to validate grant_type in League\OAuth2\Server\Authorization::issueAccessToken()) - * @return string - */ - public function getIdentifier(); - - /** - * Returns the response type (used to validate response_type in League\OAuth2\Server\Grant\AuthCode::checkAuthoriseParams()) - * @return null|string - */ - public function getResponseType(); - /** * Complete the grant flow * diff --git a/src/League/OAuth2/Server/Grant/Implicit.php b/src/League/OAuth2/Server/Grant/Implicit.php index 05809e28..a71afed5 100644 --- a/src/League/OAuth2/Server/Grant/Implicit.php +++ b/src/League/OAuth2/Server/Grant/Implicit.php @@ -24,6 +24,8 @@ use League\OAuth2\Server\Storage\ScopeInterface; */ class Implicit implements GrantTypeInterface { + use GrantTrait; + /** * Grant identifier * @var string @@ -58,34 +60,6 @@ class Implicit implements GrantTypeInterface { $this->authServer = $authServer; } - /** - * Return the identifier - * @return string - */ - public function getIdentifier() - { - return $this->identifier; - } - - /** - * Return the response type - * @return string - */ - public function getResponseType() - { - return $this->responseType; - } - - /** - * Override the default access token expire time - * @param int $accessTokenTTL - * @return void - */ - public function setAccessTokenTTL($accessTokenTTL) - { - $this->accessTokenTTL = $accessTokenTTL; - } - /** * Complete the client credentials grant * @param null|array $inputParams diff --git a/src/League/OAuth2/Server/Grant/Password.php b/src/League/OAuth2/Server/Grant/Password.php index 316f22c5..a81a62c3 100644 --- a/src/League/OAuth2/Server/Grant/Password.php +++ b/src/League/OAuth2/Server/Grant/Password.php @@ -24,6 +24,8 @@ use League\OAuth2\Server\Storage\ScopeInterface; */ class Password implements GrantTypeInterface { + use GrantTrait; + /** * Grant identifier * @var string @@ -64,34 +66,6 @@ class Password implements GrantTypeInterface { $this->authServer = $authServer; } - /** - * Return the identifier - * @return string - */ - public function getIdentifier() - { - return $this->identifier; - } - - /** - * Return the response type - * @return string - */ - public function getResponseType() - { - return $this->responseType; - } - - /** - * Override the default access token expire time - * @param int $accessTokenTTL - * @return void - */ - public function setAccessTokenTTL($accessTokenTTL) - { - $this->accessTokenTTL = $accessTokenTTL; - } - /** * Set the callback to verify a user's username and password * @param callable $callback The callback function diff --git a/src/League/OAuth2/Server/Grant/RefreshToken.php b/src/League/OAuth2/Server/Grant/RefreshToken.php index 99d759b0..4c4664f3 100644 --- a/src/League/OAuth2/Server/Grant/RefreshToken.php +++ b/src/League/OAuth2/Server/Grant/RefreshToken.php @@ -24,6 +24,8 @@ use League\OAuth2\Server\Storage\ScopeInterface; */ class RefreshToken implements GrantTypeInterface { + use GrantTrait; + /** * Grant identifier * @var string @@ -70,34 +72,6 @@ class RefreshToken implements GrantTypeInterface { $this->authServer = $authServer; } - /** - * Return the identifier - * @return string - */ - public function getIdentifier() - { - return $this->identifier; - } - - /** - * Return the response type - * @return string - */ - public function getResponseType() - { - return $this->responseType; - } - - /** - * Override the default access token expire time - * @param int $accessTokenTTL - * @return void - */ - public function setAccessTokenTTL($accessTokenTTL) - { - $this->accessTokenTTL = $accessTokenTTL; - } - /** * Set the TTL of the refresh token * @param int $refreshTokenTTL