Updates to exceptions

This commit is contained in:
Alex Bilbie 2014-05-01 14:32:54 +01:00
parent 6981ced972
commit 9f1f0cc3bc
8 changed files with 24 additions and 56 deletions

View File

@ -29,7 +29,7 @@ class InvalidClientException extends OAuthException
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function __construct($parameter) public function __construct()
{ {
parent::__construct('Client authentication failed.'); parent::__construct('Client authentication failed.');
} }

View File

@ -29,8 +29,8 @@ class InvalidCredentialsException extends OAuthException
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function __construct($parameter) public function __construct()
{ {
parent::__construct('The user credentials were incorrect..'); parent::__construct('The user credentials were incorrect.');
} }
} }

View File

@ -29,7 +29,7 @@ class InvalidRefreshException extends OAuthException
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function __construct($parameter) public function __construct()
{ {
parent::__construct('The refresh token is invalid.'); parent::__construct('The refresh token is invalid.');
} }

View File

@ -101,7 +101,7 @@ class AuthCode extends AbstractGrant
// Ensure response type is one that is recognised // Ensure response type is one that is recognised
if ( ! in_array($responseType, $this->server->getResponseTypes())) { if ( ! in_array($responseType, $this->server->getResponseTypes())) {
throw new Exception\UnsupportedResponseTypeException(); throw new Exception\UnsupportedResponseTypeException($responseType);
} }
// Validate client ID and redirect URI // Validate client ID and redirect URI

View File

@ -16,7 +16,7 @@ use League\OAuth2\Server\Entity\AccessToken;
use League\OAuth2\Server\Entity\Client; use League\OAuth2\Server\Entity\Client;
use League\OAuth2\Server\Entity\Session; use League\OAuth2\Server\Entity\Session;
use League\OAuth2\Server\Entity\Scope; use League\OAuth2\Server\Entity\Scope;
use League\OAuth2\Server\Exception\ClientException; use League\OAuth2\Server\Exception;
use League\OAuth2\Server\Util\SecureKey; use League\OAuth2\Server\Util\SecureKey;
use League\OAuth2\Server\Storage\SessionInterface; use League\OAuth2\Server\Storage\SessionInterface;
use League\OAuth2\Server\Storage\ClientInterface; use League\OAuth2\Server\Storage\ClientInterface;
@ -61,18 +61,12 @@ class ClientCredentials extends AbstractGrant
// Get the required params // Get the required params
$clientId = $this->server->getRequest()->request->get('client_id', null); $clientId = $this->server->getRequest()->request->get('client_id', null);
if (is_null($clientId)) { if (is_null($clientId)) {
throw new ClientException( throw new Exception\InvalidRequestException('client_id');
sprintf(AuthorizationServer::getExceptionMessage('invalid_request'), 'client_id'),
0
);
} }
$clientSecret = $this->server->getRequest()->request->get('client_secret', null); $clientSecret = $this->server->getRequest()->request->get('client_secret', null);
if (is_null($clientSecret)) { if (is_null($clientSecret)) {
throw new ClientException( throw new Exception\InvalidRequestException('client_secret');
sprintf(AuthorizationServer::getExceptionMessage('invalid_request'), 'client_secret'),
0
);
} }
// Validate client ID and client secret // Validate client ID and client secret
@ -84,7 +78,7 @@ class ClientCredentials extends AbstractGrant
); );
if (($client instanceof Client) === false) { if (($client instanceof Client) === false) {
throw new ClientException(AuthorizationServer::getExceptionMessage('invalid_client'), 8); throw new Exception\InvalidClientException();
} }
// Validate any scopes that are in the request // Validate any scopes that are in the request

View File

@ -17,8 +17,7 @@ use League\OAuth2\Server\Entity\Client;
use League\OAuth2\Server\Entity\RefreshToken as RT; use League\OAuth2\Server\Entity\RefreshToken as RT;
use League\OAuth2\Server\Entity\Session; use League\OAuth2\Server\Entity\Session;
use League\OAuth2\Server\Entity\Scope; use League\OAuth2\Server\Entity\Scope;
use League\OAuth2\Server\Exception\ClientException; use League\OAuth2\Server\Exception;
use League\OAuth2\Server\Exception\InvalidGrantTypeException;
use League\OAuth2\Server\Util\SecureKey; use League\OAuth2\Server\Util\SecureKey;
use League\OAuth2\Server\Storage\SessionInterface; use League\OAuth2\Server\Storage\SessionInterface;
use League\OAuth2\Server\Storage\ClientInterface; use League\OAuth2\Server\Storage\ClientInterface;
@ -70,7 +69,7 @@ class Password extends AbstractGrant
protected function getVerifyCredentialsCallback() protected function getVerifyCredentialsCallback()
{ {
if (is_null($this->callback) || ! is_callable($this->callback)) { if (is_null($this->callback) || ! is_callable($this->callback)) {
throw new InvalidGrantTypeException('Null or non-callable callback set on Password grant'); throw new Exception\ServerErrorException('Null or non-callable callback set on Password grant');
} }
return $this->callback; return $this->callback;
@ -86,18 +85,12 @@ class Password extends AbstractGrant
// Get the required params // Get the required params
$clientId = $this->server->getRequest()->request->get('client_id', null); $clientId = $this->server->getRequest()->request->get('client_id', null);
if (is_null($clientId)) { if (is_null($clientId)) {
throw new ClientException( throw new Exception\InvalidRequestException('client_id');
sprintf(AuthorizationServer::getExceptionMessage('invalid_request'), 'client_id'),
0
);
} }
$clientSecret = $this->server->getRequest()->request->get('client_secret', null); $clientSecret = $this->server->getRequest()->request->get('client_secret', null);
if (is_null($clientSecret)) { if (is_null($clientSecret)) {
throw new ClientException( throw new Exception\InvalidRequestException('client_secret');
sprintf(AuthorizationServer::getExceptionMessage('invalid_request'), 'client_secret'),
0
);
} }
// Validate client ID and client secret // Validate client ID and client secret
@ -109,30 +102,24 @@ class Password extends AbstractGrant
); );
if (($client instanceof Client) === false) { if (($client instanceof Client) === false) {
throw new ClientException(AuthorizationServer::getExceptionMessage('invalid_client'), 8); throw new Exception\InvalidClientException();
} }
$username = $this->server->getRequest()->request->get('username', null); $username = $this->server->getRequest()->request->get('username', null);
if (is_null($username)) { if (is_null($username)) {
throw new ClientException( throw new Exception\InvalidRequestException('username');
sprintf(AuthorizationServer::getExceptionMessage('invalid_request'), 'username'),
0
);
} }
$password = $this->server->getRequest()->request->get('password', null); $password = $this->server->getRequest()->request->get('password', null);
if (is_null($password)) { if (is_null($password)) {
throw new ClientException( throw new Exception\InvalidRequestException('password');
sprintf(AuthorizationServer::getExceptionMessage('invalid_request'), 'password'),
0
);
} }
// Check if user's username and password are correct // Check if user's username and password are correct
$userId = call_user_func($this->getVerifyCredentialsCallback(), $username, $password); $userId = call_user_func($this->getVerifyCredentialsCallback(), $username, $password);
if ($userId === false) { if ($userId === false) {
throw new ClientException($this->server->getExceptionMessage('invalid_credentials'), 0); throw new Exception\InvalidCredentialsException();
} }
// Validate any scopes that are in the request // Validate any scopes that are in the request

View File

@ -21,7 +21,6 @@ use League\OAuth2\Server\Storage\ScopeInterface;
use League\OAuth2\Server\Entity\RefreshToken as RT; use League\OAuth2\Server\Entity\RefreshToken as RT;
use League\OAuth2\Server\Entity\AccessToken; use League\OAuth2\Server\Entity\AccessToken;
use League\OAuth2\Server\Entity\Session; use League\OAuth2\Server\Entity\Session;
use League\OAuth2\Server\Exception\ClientException;
/** /**
* Referesh token grant * Referesh token grant
@ -65,18 +64,12 @@ class RefreshToken extends AbstractGrant
{ {
$clientId = $this->server->getRequest()->request->get('client_id', null); $clientId = $this->server->getRequest()->request->get('client_id', null);
if (is_null($clientId)) { if (is_null($clientId)) {
throw new Exception\ClientException( throw new Exception\InvalidRequestException('client_id');
sprintf($this->server->getExceptionMessage('invalid_request'), 'client_id'),
0
);
} }
$clientSecret = $this->server->getRequest()->request->get('client_secret', null); $clientSecret = $this->server->getRequest()->request->get('client_secret', null);
if (is_null($clientSecret)) { if (is_null($clientSecret)) {
throw new Exception\ClientException( throw new Exception\InvalidRequestException('client_secret');
sprintf($this->server->getExceptionMessage('invalid_request'), 'client_secret'),
0
);
} }
// Validate client ID and client secret // Validate client ID and client secret
@ -88,22 +81,19 @@ class RefreshToken extends AbstractGrant
); );
if ($client === null) { if ($client === null) {
throw new ClientException(AuthorizationServer::getExceptionMessage('invalid_client'), 8); throw new Exception\InvalidClientException();
} }
$oldRefreshTokenParam = $this->server->getRequest()->request->get('refresh_token', null); $oldRefreshTokenParam = $this->server->getRequest()->request->get('refresh_token', null);
if ($oldRefreshTokenParam === null) { if ($oldRefreshTokenParam === null) {
throw new Exception\ClientException( throw new Exception\InvalidRequestException('refresh_token');
sprintf($this->server->getExceptionMessage('invalid_request'), 'refresh_token'),
0
);
} }
// Validate refresh token // Validate refresh token
$oldRefreshToken = $this->server->getStorage('refresh_token')->get($oldRefreshTokenParam); $oldRefreshToken = $this->server->getStorage('refresh_token')->get($oldRefreshTokenParam);
if (($oldRefreshToken instanceof RT) === false) { if (($oldRefreshToken instanceof RT) === false) {
throw new Exception\ClientException($this->server->getExceptionMessage('invalid_refresh'), 0); throw new Exception\InvalidRefreshException();
} }
$oldAccessToken = $oldRefreshToken->getAccessToken(); $oldAccessToken = $oldRefreshToken->getAccessToken();
@ -124,10 +114,7 @@ class RefreshToken extends AbstractGrant
// the request doesn't include any new scopes // the request doesn't include any new scopes
foreach ($requestedScopes as $requestedScope) { foreach ($requestedScopes as $requestedScope) {
if (!isset($scopes[$requestedScope->getId()])) { if (!isset($scopes[$requestedScope->getId()])) {
throw new Exception\ClientException( throw new Exception\InvalidScopeException($requestedScope->getId());
sprintf($this->server->getExceptionMessage('invalid_scope'), $requestedScope->getId()),
0
);
} }
} }

View File

@ -210,7 +210,7 @@ class ResourceServer extends AbstractServer
} }
if (empty($accessToken)) { if (empty($accessToken)) {
throw new Exception\InvalidAccessTokenException('Access token is missing'); throw new Exception\InvalidRequestException('access token');
} }
return $accessToken; return $accessToken;