|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Toyokumo\JWTBundle; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use InvalidArgumentException; |
| 7 | +use Toyokumo\JWTBundle\Exception\InvalidJWTException; |
| 8 | +use Toyokumo\JWTBundle\Exception\NotVerifiedJWTException; |
| 9 | +use Jose\Component\Checker\InvalidClaimException; |
| 10 | +use Jose\Component\Checker\InvalidHeaderException; |
| 11 | +use Jose\Component\Core\JWKSet; |
| 12 | +use Jose\Component\KeyManagement\JWKFactory; |
| 13 | +use Jose\Component\Signature\Serializer\CompactSerializer; |
| 14 | +use Jose\Easy\Build; |
| 15 | +use Jose\Easy\Load; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class JWTService |
| 19 | + * @package Toyokumo\JWTBundle |
| 20 | + */ |
| 21 | +class JWTService |
| 22 | +{ |
| 23 | + private JWKSet $jwkSet; |
| 24 | + |
| 25 | + /** |
| 26 | + * JWTService constructor. |
| 27 | + * @param string $keyDirPath |
| 28 | + * @param array $jwkInfos |
| 29 | + */ |
| 30 | + public function __construct(string $keyDirPath, array $jwkInfos) |
| 31 | + { |
| 32 | + if ('/' !== substr($keyDirPath, -1)) { |
| 33 | + $keyDirPath .= '/'; |
| 34 | + } |
| 35 | + $jwks = []; |
| 36 | + foreach ($jwkInfos as $jwkInfo) { |
| 37 | + $kid = $jwkInfo['kid']; |
| 38 | + $alg = $jwkInfo['alg']; |
| 39 | + if ($alg === 'HS256') { |
| 40 | + $secret = $jwkInfo['secret']; |
| 41 | + $jwks[] = JWKFactory::createFromSecret($secret, [ |
| 42 | + 'use' => 'sig', |
| 43 | + 'alg' => $alg, |
| 44 | + 'kid' => $kid, |
| 45 | + ]); |
| 46 | + } else { |
| 47 | + $filename = $jwkInfo['filename']; |
| 48 | + $passphrase = $jwkInfo['passphrase']; |
| 49 | + $jwks[] = JWKFactory::createFromKeyFile( |
| 50 | + $keyDirPath . $filename, |
| 51 | + $passphrase, |
| 52 | + [ |
| 53 | + 'use' => 'sig', |
| 54 | + 'alg' => $alg, |
| 55 | + 'kid' => $kid, |
| 56 | + ] |
| 57 | + ); |
| 58 | + } |
| 59 | + } |
| 60 | + $this->jwkSet = new JWKSet($jwks); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param array $claims |
| 65 | + * @param string $kid |
| 66 | + * @param int $exp |
| 67 | + * @return string |
| 68 | + */ |
| 69 | + public function generateJWSToken( |
| 70 | + array $claims, |
| 71 | + string $kid, |
| 72 | + int $exp |
| 73 | + ): string { |
| 74 | + $now = time(); |
| 75 | + |
| 76 | + $jwk = $this->jwkSet->get($kid); |
| 77 | + $jws = Build::jws() |
| 78 | + ->alg($jwk->get('alg')) |
| 79 | + ->header('kid', $kid) |
| 80 | + ->exp($now + $exp) |
| 81 | + ->iat($now) |
| 82 | + ->nbf($now); |
| 83 | + foreach ($claims as $key => $value) { |
| 84 | + $jws->claim($key, $value); |
| 85 | + } |
| 86 | + return $jws->sign($jwk); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param string $token |
| 91 | + * @param string $claimKey |
| 92 | + * @return mixed |
| 93 | + * @throws NotVerifiedJWTException |
| 94 | + * @throws InvalidJWTException |
| 95 | + * @throws Exception |
| 96 | + */ |
| 97 | + public function extractValueFromToken(string $token, string $claimKey) |
| 98 | + { |
| 99 | + try { |
| 100 | + // Get kid for identifying jwk |
| 101 | + $signatures = (new CompactSerializer()) |
| 102 | + ->unserialize($token) |
| 103 | + ->getSignatures(); |
| 104 | + $signature = $signatures[0]; |
| 105 | + if (!$signature->hasProtectedHeaderParameter('kid')) { |
| 106 | + throw new NotVerifiedJWTException('Token is not verified.'); |
| 107 | + } |
| 108 | + $kid = $signature->getProtectedHeaderParameter('kid'); |
| 109 | + if (!$this->jwkSet->has($kid)) { |
| 110 | + throw new NotVerifiedJWTException('Token is not verified.'); |
| 111 | + } |
| 112 | + $jwk = $this->jwkSet->get($kid); |
| 113 | + |
| 114 | + $jwt = Load::jws($token) |
| 115 | + ->alg($jwk->get('alg')) |
| 116 | + ->exp() |
| 117 | + ->nbf() |
| 118 | + ->key($jwk) |
| 119 | + ->run(); |
| 120 | + } catch (InvalidClaimException $e) { |
| 121 | + // token expiration etc.. |
| 122 | + throw new InvalidJWTException('Token is invalid.'); |
| 123 | + } catch (InvalidHeaderException $e) { |
| 124 | + // alg=none tampering etc.. |
| 125 | + throw new NotVerifiedJWTException('Token is not verified.'); |
| 126 | + } catch (InvalidArgumentException $e) { |
| 127 | + if ($e->getMessage() === 'Unsupported input') { |
| 128 | + // failed to decode token |
| 129 | + throw new NotVerifiedJWTException('Token is not verified.'); |
| 130 | + } |
| 131 | + if ($e->getMessage() === 'Undefined index') { |
| 132 | + // there is no JWK corresponding to kid |
| 133 | + throw new NotVerifiedJWTException('Token is not verified.'); |
| 134 | + } |
| 135 | + throw $e; |
| 136 | + } catch (Exception $e) { |
| 137 | + if ($e->getMessage() === 'Invalid signature') { |
| 138 | + throw new NotVerifiedJWTException('Token is not verified.'); |
| 139 | + } |
| 140 | + throw $e; |
| 141 | + } |
| 142 | + |
| 143 | + return $jwt->claims->get($claimKey); |
| 144 | + } |
| 145 | +} |
0 commit comments