From 7f539f8736ac9bc9c3f05fa40f4865159ae359c7 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Sun, 21 Feb 2016 16:40:01 +0000 Subject: [PATCH] Removed unused exception parameters --- src/Exception/OAuthServerException.php | 108 +++++++++---------------- src/Grant/AbstractGrant.php | 6 +- src/Grant/AuthCodeGrant.php | 10 +-- 3 files changed, 45 insertions(+), 79 deletions(-) diff --git a/src/Exception/OAuthServerException.php b/src/Exception/OAuthServerException.php index 3a8aa469..061722d7 100644 --- a/src/Exception/OAuthServerException.php +++ b/src/Exception/OAuthServerException.php @@ -33,14 +33,15 @@ class OAuthServerException extends \Exception * Throw a new exception. * * @param string $message Error message + * @param int $code Error code * @param string $errorType Error type * @param int $httpStatusCode HTTP status code to send (default = 400) * @param null|string $hint A helper hint * @param null|string $redirectUri A HTTP URI to redirect the user back to */ - public function __construct($message, $errorType, $httpStatusCode = 400, $hint = null, $redirectUri = null) + public function __construct($message, $code, $errorType, $httpStatusCode = 400, $hint = null, $redirectUri = null) { - parent::__construct($message); + parent::__construct($message, $code); $this->httpStatusCode = $httpStatusCode; $this->errorType = $errorType; $this->hint = $hint; @@ -49,110 +50,73 @@ class OAuthServerException extends \Exception /** * Invalid grant type error. - * - * @param null|string $localizedError - * @param null|string $localizedHint - * * @return static */ - public static function invalidGrantType( - $localizedError = null, - $localizedHint = null - ) { - $errorMessage = (is_null($localizedError)) - ? 'The provided authorization grant is invalid, expired, revoked, does not match '. - 'the redirection URI used in the authorization request, or was issued to another client.' - : $localizedError; - $hint = (is_null($localizedHint)) - ? 'Check the `grant_type` parameter' - : $localizedHint; + public static function invalidGrantType() + { + $errorMessage = 'The provided authorization grant is invalid, expired, revoked, does not match ' . + 'the redirection URI used in the authorization request, or was issued to another client.'; + $hint = 'Check the `grant_type` parameter'; - return new static($errorMessage, 'invalid_grant', 400, $hint); + return new static($errorMessage, 1, 'invalid_grant', 400, $hint); } /** * Unsupported grant type error. * - * @param null|string $localizedError - * @param null|string $localizedHint - * * @return static */ - public static function unsupportedGrantType( - $localizedError = null, - $localizedHint = null - ) { - $errorMessage = (is_null($localizedError)) - ? 'The authorization grant type is not supported by the authorization server.' - : $localizedError; - $hint = (is_null($localizedHint)) - ? 'Check the `grant_type` parameter' - : $localizedHint; + public static function unsupportedGrantType() + { + $errorMessage = 'The authorization grant type is not supported by the authorization server.'; + $hint = 'Check the `grant_type` parameter'; - return new static($errorMessage, 'unsupported_grant_type', 400, $hint); + return new static($errorMessage, 2, 'unsupported_grant_type', 400, $hint); } /** * Invalid request error. * - * @param string $parameter The invalid parameter - * @param null|string $localizedError - * @param null|string $localizedHint + * @param string $parameter The invalid parameter + * @param string|null $hint * * @return static */ - public static function invalidRequest( - $parameter, - $localizedError = null, - $localizedHint = null - ) { - $errorMessage = (is_null($localizedError)) - ? 'The request is missing a required parameter, includes an invalid parameter value, '. - 'includes a parameter more than once, or is otherwise malformed.' - : $localizedError; - $hint = (is_null($localizedHint)) - ? sprintf('Check the `%s` parameter', $parameter) - : sprintf($localizedHint, $parameter); + public static function invalidRequest($parameter, $hint = null) + { + $errorMessage = 'The request is missing a required parameter, includes an invalid parameter value, ' . + 'includes a parameter more than once, or is otherwise malformed.'; + $hint = ($hint === null) ? sprintf('Check the `%s` parameter', $parameter) : $hint; - return new static($errorMessage, 'invalid_request', 400, $hint); + return new static($errorMessage, 3, 'invalid_request', 400, $hint); } /** * Invalid client error. * - * @param null|string $localizedError - * * @return static */ - public static function invalidClient($localizedError = null) + public static function invalidClient() { - $errorMessage = (is_null($localizedError)) - ? 'Client authentication failed' - : $localizedError; + $errorMessage = 'Client authentication failed'; - return new static($errorMessage, 'invalid_client', 401); + return new static($errorMessage, 4, 'invalid_client', 401); } /** * Invalid scope error. * - * @param string $scope The bad scope - * @param null|string $localizedError A localized error message - * @param null|string $localizedHint A localized error hint - * @param null|string $redirectUri A HTTP URI to redirect the user back to + * @param string $scope The bad scope + * @param null|string $redirectUri A HTTP URI to redirect the user back to * * @return static */ - public static function invalidScope($scope, $localizedError = null, $localizedHint = null, $redirectUri = null) + public static function invalidScope($scope, $redirectUri = null) { - $errorMessage = (is_null($localizedError)) - ? 'The requested scope is invalid, unknown, or malformed' - : $localizedError; - $hint = (is_null($localizedHint)) - ? sprintf('Check the `%s` scope', $scope) - : sprintf($localizedHint, $scope); + $errorMessage = 'The requested scope is invalid, unknown, or malformed'; + $hint = sprintf('Check the `%s` scope', $scope); - return new static($errorMessage, 'invalid_scope', 400, $hint, $redirectUri); + return new static($errorMessage, 5, 'invalid_scope', 400, $hint, $redirectUri); } /** @@ -162,7 +126,7 @@ class OAuthServerException extends \Exception */ public static function invalidCredentials() { - return new static('The user credentials were incorrect.', 'invalid_credentials', 401); + return new static('The user credentials were incorrect.', 6, 'invalid_credentials', 401); } /** @@ -176,7 +140,8 @@ class OAuthServerException extends \Exception { return new static( 'The authorization server encountered an unexpected condition which prevented it from fulfilling' - .'the request.', + . 'the request.', + 7, 'server_error', 500, $hint @@ -192,7 +157,7 @@ class OAuthServerException extends \Exception */ public static function invalidRefreshToken($hint = null) { - return new static('The refresh token is invalid.', 'invalid_request', 400, $hint); + return new static('The refresh token is invalid.', 8, 'invalid_request', 400, $hint); } /** @@ -208,6 +173,7 @@ class OAuthServerException extends \Exception return new static( 'The resource owner or authorization server denied the request.', 'access_denied', + 9, 401, $hint, $redirectUri @@ -304,7 +270,7 @@ class OAuthServerException extends \Exception } } if ($authScheme !== null) { - $headers[] = 'WWW-Authenticate: '.$authScheme.' realm="OAuth"'; + $headers[] = 'WWW-Authenticate: ' . $authScheme . ' realm="OAuth"'; } } diff --git a/src/Grant/AbstractGrant.php b/src/Grant/AbstractGrant.php index ee4ab684..3823047d 100644 --- a/src/Grant/AbstractGrant.php +++ b/src/Grant/AbstractGrant.php @@ -186,7 +186,7 @@ abstract class AbstractGrant implements GrantTypeInterface $this->getServerParameter('PHP_AUTH_USER', $request) ); if (is_null($clientId)) { - throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing'); + throw OAuthServerException::invalidRequest('client_id', '`%s` parameter is missing'); } $client = $this->clientRepository->getClientEntity( @@ -206,7 +206,7 @@ abstract class AbstractGrant implements GrantTypeInterface ); if ($client->canKeepASecret() && is_null($clientSecret)) { - throw OAuthServerException::invalidRequest('client_secret', null, '`%s` parameter is missing'); + throw OAuthServerException::invalidRequest('client_secret', '`%s` parameter is missing'); } if ($client->canKeepASecret() && $client->validateSecret($clientSecret) === false) { @@ -256,7 +256,7 @@ abstract class AbstractGrant implements GrantTypeInterface ); if (($scope instanceof ScopeEntity) === false) { - throw OAuthServerException::invalidScope($scopeItem, null, null, $redirectUri); + throw OAuthServerException::invalidScope($scopeItem, $redirectUri); } $scopes[] = $scope; diff --git a/src/Grant/AuthCodeGrant.php b/src/Grant/AuthCodeGrant.php index cdd0f1cd..4c808ad9 100644 --- a/src/Grant/AuthCodeGrant.php +++ b/src/Grant/AuthCodeGrant.php @@ -60,10 +60,10 @@ class AuthCodeGrant extends AbstractGrant $this->userRepository = $userRepository; $this->authCodeTTL = $authCodeTTL; $this->pathToLoginTemplate = ($pathToLoginTemplate === null) - ? __DIR__.'/../ResponseTypes/DefaultTemplates/login_user.php' + ? __DIR__ . '/../ResponseTypes/DefaultTemplates/login_user.php' : $this->pathToLoginTemplate; $this->pathToAuthorizeTemplate = ($pathToLoginTemplate === null) - ? __DIR__.'/../ResponseTypes/DefaultTemplates/authorize_client.php' + ? __DIR__ . '/../ResponseTypes/DefaultTemplates/authorize_client.php' : $this->pathToAuthorizeTemplate; $this->refreshTokenTTL = new \DateInterval('P1M'); } @@ -86,7 +86,7 @@ class AuthCodeGrant extends AbstractGrant $this->getServerParameter('PHP_AUTH_USER', $request) ); if (is_null($clientId)) { - throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing'); + throw OAuthServerException::invalidRequest('client_id'); } $client = $this->clientRepository->getClientEntity( @@ -252,7 +252,7 @@ class AuthCodeGrant extends AbstractGrant // The redirect URI is required in this request $redirectUri = $this->getQueryStringParameter('redirect_uri', $request, null); if (is_null($redirectUri)) { - throw OAuthServerException::invalidRequest('redirect_uri', null, '`%s` parameter is missing'); + throw OAuthServerException::invalidRequest('redirect_uri'); } // Validate request @@ -278,7 +278,7 @@ class AuthCodeGrant extends AbstractGrant throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client'); } } catch (\LogicException $e) { - throw OAuthServerException::invalidRequest('code', null, 'Cannot decrypt the authorization code'); + throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code'); } // Issue and persist access + refresh tokens