Removed unused exception parameters

This commit is contained in:
Alex Bilbie 2016-02-21 16:40:01 +00:00
parent a4b65241ad
commit 7f539f8736
3 changed files with 45 additions and 79 deletions

View File

@ -33,14 +33,15 @@ class OAuthServerException extends \Exception
* Throw a new exception. * Throw a new exception.
* *
* @param string $message Error message * @param string $message Error message
* @param int $code Error code
* @param string $errorType Error type * @param string $errorType Error type
* @param int $httpStatusCode HTTP status code to send (default = 400) * @param int $httpStatusCode HTTP status code to send (default = 400)
* @param null|string $hint A helper hint * @param null|string $hint A helper hint
* @param null|string $redirectUri A HTTP URI to redirect the user back to * @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->httpStatusCode = $httpStatusCode;
$this->errorType = $errorType; $this->errorType = $errorType;
$this->hint = $hint; $this->hint = $hint;
@ -49,110 +50,73 @@ class OAuthServerException extends \Exception
/** /**
* Invalid grant type error. * Invalid grant type error.
*
* @param null|string $localizedError
* @param null|string $localizedHint
*
* @return static * @return static
*/ */
public static function invalidGrantType( public static function invalidGrantType()
$localizedError = null, {
$localizedHint = null $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.';
$errorMessage = (is_null($localizedError)) $hint = 'Check the `grant_type` parameter';
? '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;
return new static($errorMessage, 'invalid_grant', 400, $hint); return new static($errorMessage, 1, 'invalid_grant', 400, $hint);
} }
/** /**
* Unsupported grant type error. * Unsupported grant type error.
* *
* @param null|string $localizedError
* @param null|string $localizedHint
*
* @return static * @return static
*/ */
public static function unsupportedGrantType( public static function unsupportedGrantType()
$localizedError = null, {
$localizedHint = null $errorMessage = 'The authorization grant type is not supported by the authorization server.';
) { $hint = 'Check the `grant_type` parameter';
$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;
return new static($errorMessage, 'unsupported_grant_type', 400, $hint); return new static($errorMessage, 2, 'unsupported_grant_type', 400, $hint);
} }
/** /**
* Invalid request error. * Invalid request error.
* *
* @param string $parameter The invalid parameter * @param string $parameter The invalid parameter
* @param null|string $localizedError * @param string|null $hint
* @param null|string $localizedHint
* *
* @return static * @return static
*/ */
public static function invalidRequest( public static function invalidRequest($parameter, $hint = null)
$parameter, {
$localizedError = null, $errorMessage = 'The request is missing a required parameter, includes an invalid parameter value, ' .
$localizedHint = null 'includes a parameter more than once, or is otherwise malformed.';
) { $hint = ($hint === null) ? sprintf('Check the `%s` parameter', $parameter) : $hint;
$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);
return new static($errorMessage, 'invalid_request', 400, $hint); return new static($errorMessage, 3, 'invalid_request', 400, $hint);
} }
/** /**
* Invalid client error. * Invalid client error.
* *
* @param null|string $localizedError
*
* @return static * @return static
*/ */
public static function invalidClient($localizedError = null) public static function invalidClient()
{ {
$errorMessage = (is_null($localizedError)) $errorMessage = 'Client authentication failed';
? 'Client authentication failed'
: $localizedError;
return new static($errorMessage, 'invalid_client', 401); return new static($errorMessage, 4, 'invalid_client', 401);
} }
/** /**
* Invalid scope error. * Invalid scope error.
* *
* @param string $scope The bad scope * @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 null|string $redirectUri A HTTP URI to redirect the user back to
* *
* @return static * @return static
*/ */
public static function invalidScope($scope, $localizedError = null, $localizedHint = null, $redirectUri = null) public static function invalidScope($scope, $redirectUri = null)
{ {
$errorMessage = (is_null($localizedError)) $errorMessage = 'The requested scope is invalid, unknown, or malformed';
? 'The requested scope is invalid, unknown, or malformed' $hint = sprintf('Check the `%s` scope', $scope);
: $localizedError;
$hint = (is_null($localizedHint))
? sprintf('Check the `%s` scope', $scope)
: sprintf($localizedHint, $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() 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);
} }
/** /**
@ -177,6 +141,7 @@ class OAuthServerException extends \Exception
return new static( return new static(
'The authorization server encountered an unexpected condition which prevented it from fulfilling' 'The authorization server encountered an unexpected condition which prevented it from fulfilling'
. 'the request.', . 'the request.',
7,
'server_error', 'server_error',
500, 500,
$hint $hint
@ -192,7 +157,7 @@ class OAuthServerException extends \Exception
*/ */
public static function invalidRefreshToken($hint = null) 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( return new static(
'The resource owner or authorization server denied the request.', 'The resource owner or authorization server denied the request.',
'access_denied', 'access_denied',
9,
401, 401,
$hint, $hint,
$redirectUri $redirectUri

View File

@ -186,7 +186,7 @@ abstract class AbstractGrant implements GrantTypeInterface
$this->getServerParameter('PHP_AUTH_USER', $request) $this->getServerParameter('PHP_AUTH_USER', $request)
); );
if (is_null($clientId)) { 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( $client = $this->clientRepository->getClientEntity(
@ -206,7 +206,7 @@ abstract class AbstractGrant implements GrantTypeInterface
); );
if ($client->canKeepASecret() && is_null($clientSecret)) { 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) { if ($client->canKeepASecret() && $client->validateSecret($clientSecret) === false) {
@ -256,7 +256,7 @@ abstract class AbstractGrant implements GrantTypeInterface
); );
if (($scope instanceof ScopeEntity) === false) { if (($scope instanceof ScopeEntity) === false) {
throw OAuthServerException::invalidScope($scopeItem, null, null, $redirectUri); throw OAuthServerException::invalidScope($scopeItem, $redirectUri);
} }
$scopes[] = $scope; $scopes[] = $scope;

View File

@ -86,7 +86,7 @@ class AuthCodeGrant extends AbstractGrant
$this->getServerParameter('PHP_AUTH_USER', $request) $this->getServerParameter('PHP_AUTH_USER', $request)
); );
if (is_null($clientId)) { if (is_null($clientId)) {
throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing'); throw OAuthServerException::invalidRequest('client_id');
} }
$client = $this->clientRepository->getClientEntity( $client = $this->clientRepository->getClientEntity(
@ -252,7 +252,7 @@ class AuthCodeGrant extends AbstractGrant
// The redirect URI is required in this request // The redirect URI is required in this request
$redirectUri = $this->getQueryStringParameter('redirect_uri', $request, null); $redirectUri = $this->getQueryStringParameter('redirect_uri', $request, null);
if (is_null($redirectUri)) { if (is_null($redirectUri)) {
throw OAuthServerException::invalidRequest('redirect_uri', null, '`%s` parameter is missing'); throw OAuthServerException::invalidRequest('redirect_uri');
} }
// Validate request // Validate request
@ -278,7 +278,7 @@ class AuthCodeGrant extends AbstractGrant
throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client'); throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client');
} }
} catch (\LogicException $e) { } 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 // Issue and persist access + refresh tokens