Skip to content

Commit

Permalink
Refactor exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
chalasr committed Aug 28, 2016
1 parent b64db93 commit 64eb651
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Encoder/DefaultEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public function decode($token)
throw new JWTDecodeFailureException('Invalid JWT Token', $e);
}

if ($jws->isExpired()) {
throw new JWTDecodeFailureException('Expired JWT Token');
}

if (!$jws->isVerified()) {
throw new JWTDecodeFailureException('Unable to verify the given JWT through the given configuration. If the "lexik_jwt_authentication.encoder" encryption options have been changed since your last authentication, please renew the token. If the problem persists, verify that the configured keys/passphrase are valid.');
}

if ($jws->isExpired()) {
throw new JWTDecodeFailureException('Expired JWT Token');
}

return $jws->getPayload();
}
}
12 changes: 12 additions & 0 deletions Exception/ExpiredTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Lexik\Bundle\JWTAuthenticationBundle\Exception;

/**
* Base class for exceptions thrown during JWT creation/loading.
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
class ExpiredTokenException extends JWTDecodeFailureException
{
}
18 changes: 18 additions & 0 deletions Exception/InvalidTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Lexik\Bundle\JWTAuthenticationBundle\Exception;

use Symfony\Component\Security\Core\Exception\AuthenticationException;

/**
* Exception to be thrown in case of invalid token during an authentication process.
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
class InvalidTokenException extends AuthenticationException
{
public function getMessageKey()
{
return 'Invalid JWT Token';
}
}
39 changes: 39 additions & 0 deletions Exception/InvalidUserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Lexik\Bundle\JWTAuthenticationBundle\Exception;

use Symfony\Component\Security\Core\Exception\AuthenticationException;

/**
* Exception to be thrown in case of invalid user (not found, missing permissions, ...) during
* an authentication process.
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
class UserNotFoundException extends AuthenticationException
{
/**
* @var string
*/
private $userIdentityField;

/**
* @var string
*/
private $identity;

/**
* @param string $userIdentityField
* @param string $identity
*/
public function __construct($userIdentityField, $identity)
{
$this->userIdentityField = $userIdentityField;
$this->identity = $identity;
}

public function getMessageKey()
{
return sprintf('Unable to load an user with property "%s" = "%s". If the user identity has changed, you must renew the token. Otherwise, verify that the "lexik_jwt_authentication.user_identity_field" config option is correctly set.', $this->userIdentity, $this->identity);
}
}
2 changes: 1 addition & 1 deletion Exception/JWTAuthenticationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function tokenNotFound($message = 'JWT Token not found')
*/
public static function invalidUser($identity, $identityField)
{
return new self(sprintf('Unable to load a valid user with property "%s" = "%s". If the user identity has been changed, you must renew the token. Otherwise, verify that the "lexik_jwt_authentication.user_identity_field" config option is correctly set.', $identityField, $identity));
return new self(sprintf('Unable to load a valid user with property "%s" = "%s". If the user identity has been changed, you must renew the token. Otherwise, verify that the configured "user_identity_field" is correct.', $identityField, $identity));
}

/**
Expand Down
4 changes: 3 additions & 1 deletion Exception/JWTDecodeFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Lexik\Bundle\JWTAuthenticationBundle\Exception;

use \Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;

/**
* Base class for exceptions thrown during JWTEncoderInterface::decode().
* JWTDecodeFailureException is thrown if an error occurs in {@link JWTEncoderInterface::decode().
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
Expand Down
4 changes: 3 additions & 1 deletion Exception/JWTEncodeFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Lexik\Bundle\JWTAuthenticationBundle\Exception;

use \Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;

/**
* Base class for exceptions thrown during JWTEncoderInterface::encode().
* JWTEncodeFailureException is thrown if an error occurs in {@link JWTEncoderInterface::encode().
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
Expand Down

0 comments on commit 64eb651

Please sign in to comment.