|
4 | 4 |
|
5 | 5 | namespace CodeIgniter\Shield\Authentication\JWT\Adapters; |
6 | 6 |
|
| 7 | +use CodeIgniter\Shield\Authentication\JWT\Exceptions\InvalidTokenException; |
7 | 8 | use CodeIgniter\Shield\Authentication\JWT\JWSAdapterInterface; |
8 | 9 | use CodeIgniter\Shield\Config\AuthJWT; |
9 | | -use CodeIgniter\Shield\Exceptions\RuntimeException; |
| 10 | +use CodeIgniter\Shield\Exceptions\InvalidArgumentException as ShieldInvalidArgumentException; |
| 11 | +use CodeIgniter\Shield\Exceptions\LogicException; |
10 | 12 | use DomainException; |
11 | 13 | use Firebase\JWT\BeforeValidException; |
12 | 14 | use Firebase\JWT\ExpiredException; |
@@ -48,29 +50,40 @@ public static function decode(string $encodedToken, $keyset): stdClass |
48 | 50 | return JWT::decode($encodedToken, $keys); |
49 | 51 | } catch (InvalidArgumentException $e) { |
50 | 52 | // provided key/key-array is empty or malformed. |
51 | | - throw new RuntimeException('Invalid JWT: ' . $e->getMessage(), 0, $e); |
| 53 | + throw new ShieldInvalidArgumentException( |
| 54 | + 'Invalid Keyset: "' . $keyset . '". ' . $e->getMessage(), |
| 55 | + 0, |
| 56 | + $e |
| 57 | + ); |
52 | 58 | } catch (DomainException $e) { |
53 | 59 | // provided algorithm is unsupported OR |
54 | 60 | // provided key is invalid OR |
55 | 61 | // unknown error thrown in openSSL or libsodium OR |
56 | 62 | // libsodium is required but not available. |
57 | | - throw new RuntimeException('Invalid JWT: ' . $e->getMessage(), 0, $e); |
| 63 | + throw new LogicException('Cannot decode JWT: ' . $e->getMessage(), 0, $e); |
58 | 64 | } catch (SignatureInvalidException $e) { |
59 | 65 | // provided JWT signature verification failed. |
60 | | - throw new RuntimeException('Invalid JWT: ' . $e->getMessage(), 0, $e); |
| 66 | + throw InvalidTokenException::forInvalidToken($e); |
61 | 67 | } catch (BeforeValidException $e) { |
62 | 68 | // provided JWT is trying to be used before "nbf" claim OR |
63 | 69 | // provided JWT is trying to be used before "iat" claim. |
64 | | - throw new RuntimeException('Expired JWT: ' . $e->getMessage(), 0, $e); |
| 70 | + throw InvalidTokenException::forBeforeValidToken($e); |
65 | 71 | } catch (ExpiredException $e) { |
66 | 72 | // provided JWT is trying to be used after "exp" claim. |
67 | | - throw new RuntimeException('Expired JWT: ' . $e->getMessage(), 0, $e); |
| 73 | + throw InvalidTokenException::forExpiredToken($e); |
68 | 74 | } catch (UnexpectedValueException $e) { |
69 | 75 | // provided JWT is malformed OR |
70 | 76 | // provided JWT is missing an algorithm / using an unsupported algorithm OR |
71 | 77 | // provided JWT algorithm does not match provided key OR |
72 | 78 | // provided key ID in key/key-array is empty or invalid. |
73 | | - throw new RuntimeException('Invalid JWT: ' . $e->getMessage(), 0, $e); |
| 79 | + log_message( |
| 80 | + 'error', |
| 81 | + '[Shield] ' . class_basename(self::class) . '::' . __FUNCTION__ |
| 82 | + . '(' . __LINE__ . ') ' |
| 83 | + . get_class($e) . ': ' . $e->getMessage() |
| 84 | + ); |
| 85 | + |
| 86 | + throw InvalidTokenException::forInvalidToken($e); |
74 | 87 | } |
75 | 88 | } |
76 | 89 |
|
|
0 commit comments