Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace OAuth2\Encryption; |
| 4 | |
| 5 | /** |
| 6 | * Bridge file to use the firebase/php-jwt package for JWT encoding and decoding. |
| 7 | * @author Francis Chuang <francis.chuang@gmail.com> |
| 8 | */ |
| 9 | class FirebaseJwt implements EncryptionInterface |
| 10 | { |
| 11 | public function __construct() |
| 12 | { |
| 13 | if (!class_exists('\JWT')) { |
| 14 | throw new \ErrorException('firebase/php-jwt must be installed to use this feature. You can do this by running "composer require firebase/php-jwt"'); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | public function encode($payload, $key, $alg = 'HS256', $keyId = null) |
| 19 | { |
| 20 | return \JWT::encode($payload, $key, $alg, $keyId); |
| 21 | } |
| 22 | |
| 23 | public function decode($jwt, $key = null, $allowedAlgorithms = null) |
| 24 | { |
| 25 | try { |
| 26 | |
| 27 | //Maintain BC: Do not verify if no algorithms are passed in. |
| 28 | if (!$allowedAlgorithms) { |
| 29 | $key = null; |
| 30 | } |
| 31 | |
| 32 | return (array)\JWT::decode($jwt, $key, $allowedAlgorithms); |
| 33 | } catch (\Exception $e) { |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public function urlSafeB64Encode($data) |
| 39 | { |
| 40 | return \JWT::urlsafeB64Encode($data); |
| 41 | } |
| 42 | |
| 43 | public function urlSafeB64Decode($b64) |
| 44 | { |
| 45 | return \JWT::urlsafeB64Decode($b64); |
| 46 | } |
| 47 | } |