diff --git a/src/AbstractServer.php b/src/AbstractServer.php index 828bee39..8439fa85 100644 --- a/src/AbstractServer.php +++ b/src/AbstractServer.php @@ -11,6 +11,8 @@ namespace League\OAuth2\Server; +use League\OAuth2\Server\Exception; +use League\OAuth2\Server\TokenType\TokenTypeInterface; use Symfony\Component\HttpFoundation\Request; /** @@ -32,6 +34,12 @@ abstract class AbstractServer */ protected $storages = []; + /** + * Token type + * @var TokenTypeInterface + */ + protected $tokenType; + /** * Sets the Request Object * @param \Symfony\Component\HttpFoundation\Request The Request Object @@ -72,4 +80,23 @@ abstract class AbstractServer return $this->storages[$obj]; } + + /** + * Set the access token type + * @param TokenTypeInterface $tokenType The token type + * @return void + */ + public function setTokenType(TokenTypeInterface $tokenType) + { + $this->tokenType = $tokenType; + } + + /** + * Get the access token type + * @return TokenTypeInterface + */ + public function getTokenType() + { + return $this->tokenType; + } } diff --git a/src/Grant/AuthCodeGrant.php b/src/Grant/AuthCodeGrant.php index 1494defe..e5f31624 100644 --- a/src/Grant/AuthCodeGrant.php +++ b/src/Grant/AuthCodeGrant.php @@ -218,19 +218,16 @@ class AuthCodeGrant extends AbstractGrant $session->associateScope($authCodeScope); } - $response = [ - 'access_token' => $accessToken->getToken(), - 'token_type' => 'Bearer', - 'expires' => $accessToken->getExpireTime(), - 'expires_in' => $this->server->getAccessTokenTTL() - ]; + $this->server->getTokenType()->set('access_token', $accessToken->getToken()); + $this->server->getTokenType()->set('expires', $accessToken->getExpireTime()); + $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL()); // Associate a refresh token if set if ($this->server->hasGrantType('refresh_token')) { $refreshToken = new RefreshTokenEntity($this->server); $refreshToken->setToken(SecureKey::generate()); $refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time()); - $response['refresh_token'] = $refreshToken->getToken(); + $this->server->getTokenType()->set('refresh_token', $refreshToken->getToken()); } // Expire the auth code @@ -246,6 +243,6 @@ class AuthCodeGrant extends AbstractGrant $refreshToken->save(); } - return $response; + return $this->server->getTokenType()->generateResponse(); } } diff --git a/src/Grant/ClientCredentialsGrant.php b/src/Grant/ClientCredentialsGrant.php index cd0a6acf..786d5d2c 100644 --- a/src/Grant/ClientCredentialsGrant.php +++ b/src/Grant/ClientCredentialsGrant.php @@ -101,13 +101,10 @@ class ClientCredentialsGrant extends AbstractGrant $accessToken->setSession($session); $accessToken->save($this->server->getStorage('access_token')); - $response = [ - 'access_token' => $accessToken->getToken(), - 'token_type' => 'Bearer', - 'expires' => $accessToken->getExpireTime(), - 'expires_in' => $this->server->getAccessTokenTTL() - ]; + $this->server->getTokenType()->set('access_token', $accessToken->getToken()); + $this->server->getTokenType()->set('expires', $accessToken->getExpireTime()); + $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL()); - return $response; + return $this->server->getTokenType()->generateResponse(); } } diff --git a/src/Grant/PasswordGrant.php b/src/Grant/PasswordGrant.php index 4a1215df..9c2b27e3 100644 --- a/src/Grant/PasswordGrant.php +++ b/src/Grant/PasswordGrant.php @@ -137,19 +137,16 @@ class PasswordGrant extends AbstractGrant $session->associateScope($scope); } - $response = [ - 'access_token' => $accessToken->getToken(), - 'token_type' => 'Bearer', - 'expires' => $accessToken->getExpireTime(), - 'expires_in' => $this->server->getAccessTokenTTL() - ]; + $this->server->getTokenType()->set('access_token', $accessToken->getToken()); + $this->server->getTokenType()->set('expires', $accessToken->getExpireTime()); + $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL()); // Associate a refresh token if set if ($this->server->hasGrantType('refresh_token')) { $refreshToken = new RefreshTokenEntity($this->server); $refreshToken->setToken(SecureKey::generate()); $refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time()); - $response['refresh_token'] = $refreshToken->getToken(); + $this->server->getTokenType()->set('refresh_token', $refreshToken->getToken()); } // Save everything @@ -162,6 +159,6 @@ class PasswordGrant extends AbstractGrant $refreshToken->save(); } - return $response; + return $this->server->getTokenType()->generateResponse(); } } diff --git a/src/Grant/RefreshTokenGrant.php b/src/Grant/RefreshTokenGrant.php index 1f5ee111..c84503c0 100644 --- a/src/Grant/RefreshTokenGrant.php +++ b/src/Grant/RefreshTokenGrant.php @@ -131,12 +131,9 @@ class RefreshTokenGrant extends AbstractGrant $oldAccessToken->expire($this->server->getStorage('access_token')); $newAccessToken->save($this->server->getStorage('access_token')); - $response = [ - 'access_token' => $newAccessToken->getToken(), - 'token_type' => 'Bearer', - 'expires' => $newAccessToken->getExpireTime(), - 'expires_in' => $this->server->getAccessTokenTTL() - ]; + $this->server->getTokenType()->set('access_token', $accessToken->getToken()); + $this->server->getTokenType()->set('expires', $accessToken->getExpireTime()); + $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL()); // Expire the old refresh token $oldRefreshToken->expire($this->server->getStorage('refresh_token')); @@ -148,8 +145,8 @@ class RefreshTokenGrant extends AbstractGrant $newRefreshToken->setAccessToken($newAccessToken); $newRefreshToken->save($this->server->getStorage('refresh_token')); - $response['refresh_token'] = $newRefreshToken->getToken(); + $this->server->getTokenType()->set('refresh_token', $refreshToken->getToken()); - return $response; + return $this->server->getTokenType()->generateResponse(); } } diff --git a/src/TokenType/AbstractTokenType.php b/src/TokenType/AbstractTokenType.php new file mode 100644 index 00000000..d72181b7 --- /dev/null +++ b/src/TokenType/AbstractTokenType.php @@ -0,0 +1,41 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * @link https://github.com/thephpleague/oauth2-server + */ + +namespace League\OAuth2\Server\TokenType; + +abstract class AbstractBearer +{ + /** + * Response array + * @var array + */ + protected $response = []; + + /** + * Set a key/value response pair + * @param string $key + * @param mixed $value + */ + public function set($key, $value) + { + $this->responsekey[$key] = $value; + } + + /** + * Get a key from the response array + * @param string $key + * @return mixed + */ + private function get($key) + { + return isset($this->response[$key]) ? $this->response[$key] : null; + } +} diff --git a/src/TokenType/Bearer.php b/src/TokenType/Bearer.php new file mode 100644 index 00000000..9b1acfaa --- /dev/null +++ b/src/TokenType/Bearer.php @@ -0,0 +1,49 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * @link https://github.com/thephpleague/oauth2-server + */ + +namespace League\OAuth2\Server\TokenType; + +class Bearer extends AbstractBearer implements TokenTypeInterface +{ + protected $response = []; + + /** + * {@inheritdoc} + */ + public function set($key, $value) + { + $this->responsekey[$key] = $value; + } + + private function get($key) + { + return isset($this->response[$key]) ? $this->response[$key] : null; + } + + /** + * {@inheritdoc} + */ + public function generateResponse() + { + $return = [ + 'access_token' => $this->get('refresh_token'), + 'token_type' => 'Bearer', + 'expires' => $this->get('expires'), + 'expires_in' => $this->get('expires_in') + ]; + + if (!is_null($this->get('refresh_token'))) { + $return['refresh_token'] = $this->get('refresh_token'); + } + + return $return; + } +} diff --git a/src/TokenType/Mac.php b/src/TokenType/Mac.php new file mode 100644 index 00000000..b90d31da --- /dev/null +++ b/src/TokenType/Mac.php @@ -0,0 +1,17 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * @link https://github.com/thephpleague/oauth2-server + */ + +namespace League\OAuth2\Server\TokenType; + +class Mac extends AbstractBearer implements TokenTypeInterface +{ + +} diff --git a/src/TokenType/TokenTypeInterface.php b/src/TokenType/TokenTypeInterface.php new file mode 100644 index 00000000..c0116ba8 --- /dev/null +++ b/src/TokenType/TokenTypeInterface.php @@ -0,0 +1,21 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * @link https://github.com/thephpleague/oauth2-server + */ + +namespace League\OAuth2\Server\TokenType; + +interface TokenTypeInterface +{ + /** + * Generate a response + * @return array + */ + public function generateResponse(); +}