Skip to content

Commit

Permalink
Merge pull request #965 from ceeram/add-previous
Browse files Browse the repository at this point in the history
Include previous exception in catch and throw
  • Loading branch information
Sephster authored Nov 13, 2018
2 parents 95a9f46 + 7982275 commit efa8ef6
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- Added a ScopeTrait to provide an implementation for jsonSerialize (PR #952)
- Ability to nest exceptions (PR #965)

### Fixed
- Fix issue where AuthorizationServer is not stateless as ResponseType could store state of a previous request (PR #960)
Expand Down
6 changes: 3 additions & 3 deletions src/AuthorizationValidators/BearerTokenValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function validateAuthorization(ServerRequestInterface $request)
throw OAuthServerException::accessDenied('Access token could not be verified');
}
} catch (BadMethodCallException $exception) {
throw OAuthServerException::accessDenied('Access token is not signed');
throw OAuthServerException::accessDenied('Access token is not signed', null, $exception);
}

// Ensure access token hasn't expired
Expand All @@ -97,10 +97,10 @@ public function validateAuthorization(ServerRequestInterface $request)
->withAttribute('oauth_scopes', $token->getClaim('scopes'));
} catch (InvalidArgumentException $exception) {
// JWT couldn't be parsed so return the request as is
throw OAuthServerException::accessDenied($exception->getMessage());
throw OAuthServerException::accessDenied($exception->getMessage(), null, $exception);
} catch (RuntimeException $exception) {
//JWR couldn't be parsed so return the request as is
throw OAuthServerException::accessDenied('Error while decoding to JSON');
throw OAuthServerException::accessDenied('Error while decoding to JSON', null, $exception);
}
}
}
4 changes: 2 additions & 2 deletions src/CryptTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function encrypt($unencryptedData)

return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
} catch (Exception $e) {
throw new LogicException($e->getMessage());
throw new LogicException($e->getMessage(), null, $e);
}
}

Expand All @@ -63,7 +63,7 @@ protected function decrypt($encryptedData)

return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
} catch (Exception $e) {
throw new LogicException($e->getMessage());
throw new LogicException($e->getMessage(), null, $e);
}
}

Expand Down
30 changes: 19 additions & 11 deletions src/Exception/OAuthServerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Exception;
use Psr\Http\Message\ResponseInterface;
use Throwable;

class OAuthServerException extends Exception
{
Expand Down Expand Up @@ -48,10 +49,11 @@ class OAuthServerException extends Exception
* @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
* @param Throwable $previous Previous exception
*/
public function __construct($message, $code, $errorType, $httpStatusCode = 400, $hint = null, $redirectUri = null)
public function __construct($message, $code, $errorType, $httpStatusCode = 400, $hint = null, $redirectUri = null, Throwable $previous = null)
{
parent::__construct($message, $code);
parent::__construct($message, $code, $previous);
$this->httpStatusCode = $httpStatusCode;
$this->errorType = $errorType;
$this->hint = $hint;
Expand Down Expand Up @@ -103,16 +105,17 @@ public static function unsupportedGrantType()
*
* @param string $parameter The invalid parameter
* @param null|string $hint
* @param Throwable $previous Previous exception
*
* @return static
*/
public static function invalidRequest($parameter, $hint = null)
public static function invalidRequest($parameter, $hint = null, Throwable $previous = 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, 3, 'invalid_request', 400, $hint);
return new static($errorMessage, 3, 'invalid_request', 400, $hint, null, $previous);
}

/**
Expand Down Expand Up @@ -164,52 +167,57 @@ public static function invalidCredentials()
/**
* Server error.
*
* @param string $hint
* @param string $hint
* @param Throwable $previous
*
* @return static
*
* @codeCoverageIgnore
*/
public static function serverError($hint)
public static function serverError($hint, Throwable $previous = null)
{
return new static(
'The authorization server encountered an unexpected condition which prevented it from fulfilling'
. ' the request: ' . $hint,
7,
'server_error',
500
500,
$previous
);
}

/**
* Invalid refresh token.
*
* @param null|string $hint
* @param Throwable $previous
*
* @return static
*/
public static function invalidRefreshToken($hint = null)
public static function invalidRefreshToken($hint = null, Throwable $previous = null)
{
return new static('The refresh token is invalid.', 8, 'invalid_request', 401, $hint);
return new static('The refresh token is invalid.', 8, 'invalid_request', 401, $hint, null, $previous);
}

/**
* Access denied.
*
* @param null|string $hint
* @param null|string $redirectUri
* @param Throwable $previous
*
* @return static
*/
public static function accessDenied($hint = null, $redirectUri = null)
public static function accessDenied($hint = null, $redirectUri = null, Throwable $previous = null)
{
return new static(
'The resource owner or authorization server denied the request.',
9,
'access_denied',
401,
$hint,
$redirectUri
$redirectUri,
$previous
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Grant/AbstractGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,12 @@ protected function generateUniqueIdentifier($length = 40)
return bin2hex(random_bytes($length));
// @codeCoverageIgnoreStart
} catch (TypeError $e) {
throw OAuthServerException::serverError('An unexpected error has occurred');
throw OAuthServerException::serverError('An unexpected error has occurred', $e);
} catch (Error $e) {
throw OAuthServerException::serverError('An unexpected error has occurred');
throw OAuthServerException::serverError('An unexpected error has occurred', $e);
} catch (Exception $e) {
// If you get this message, the CSPRNG failed hard.
throw OAuthServerException::serverError('Could not generate a random string');
throw OAuthServerException::serverError('Could not generate a random string', $e);
}
// @codeCoverageIgnoreEnd
}
Expand Down
2 changes: 1 addition & 1 deletion src/Grant/AuthCodeGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function respondToAccessTokenRequest(
$authCodePayload->user_id
);
} catch (LogicException $e) {
throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code');
throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code', $e);
}

// Validate code challenge
Expand Down
2 changes: 1 addition & 1 deletion src/Grant/RefreshTokenGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function validateOldRefreshToken(ServerRequestInterface $request, $cli
try {
$refreshToken = $this->decrypt($encryptedRefreshToken);
} catch (Exception $e) {
throw OAuthServerException::invalidRefreshToken('Cannot decrypt the refresh token');
throw OAuthServerException::invalidRefreshToken('Cannot decrypt the refresh token', $e);
}

$refreshTokenData = json_decode($refreshToken, true);
Expand Down
16 changes: 16 additions & 0 deletions tests/Exception/OAuthServerExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LeagueTests\Exception;

use Exception;
use League\OAuth2\Server\Exception\OAuthServerException;
use PHPUnit\Framework\TestCase;

Expand All @@ -20,4 +21,19 @@ public function testDoesNotHaveRedirect()

$this->assertFalse($exceptionWithoutRedirect->hasRedirect());
}

public function testHasPrevious()
{
$previous = new Exception('This is the previous');
$exceptionWithPrevious = OAuthServerException::accessDenied(null, null, $previous);

$this->assertSame('This is the previous', $exceptionWithPrevious->getPrevious()->getMessage());
}

public function testDoesNotHavePrevious()
{
$exceptionWithoutPrevious = OAuthServerException::accessDenied();

$this->assertNull($exceptionWithoutPrevious->getPrevious());
}
}

0 comments on commit efa8ef6

Please sign in to comment.