| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\JoseVerifier\Internal; |
| 6 |
|
| 7 |
use Facile\JoseVerifier\Exception\InvalidTokenClaimException; |
| 8 |
use Facile\JoseVerifier\Exception\InvalidTokenException; |
| 9 |
use Jose\Component\Checker; |
| 10 |
use Jose\Component\Core\AlgorithmManager; |
| 11 |
use Jose\Component\Core\JWKSet; |
| 12 |
use Jose\Component\Core\Util\JsonConverter; |
| 13 |
use Jose\Component\Signature\Algorithm; |
| 14 |
use Jose\Component\Signature\JWSTokenSupport; |
| 15 |
use Jose\Component\Signature\JWSVerifier; |
| 16 |
use Jose\Component\Signature\Serializer\CompactSerializer; |
| 17 |
use JsonException; |
| 18 |
use Throwable; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
final class Validate |
| 26 |
{ |
| 27 |
private string $token; |
| 28 |
|
| 29 |
private JWKSet $jwkset; |
| 30 |
|
| 31 |
|
| 32 |
private array $headerCheckers = []; |
| 33 |
|
| 34 |
|
| 35 |
private array $claimCheckers = []; |
| 36 |
|
| 37 |
|
| 38 |
private array $algorithms = []; |
| 39 |
|
| 40 |
|
| 41 |
private array $mandatoryClaims = []; |
| 42 |
|
| 43 |
private function __construct(string $token) |
| 44 |
{ |
| 45 |
$this->token = $token; |
| 46 |
$this->jwkset = new JWKSet([]); |
| 47 |
|
| 48 |
foreach ($this->getAlgorithmMap() as $algorithmClass) { |
| 49 |
if (class_exists($algorithmClass)) { |
| 50 |
try { |
| 51 |
$this->algorithms[] = new $algorithmClass(); |
| 52 |
} catch (Throwable $throwable) { |
| 53 |
|
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public static function withToken(string $token): static |
| 60 |
{ |
| 61 |
return new static($token); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function run(): array |
| 71 |
{ |
| 72 |
$jws = (new CompactSerializer())->unserialize($this->token); |
| 73 |
$headerChecker = new Checker\HeaderCheckerManager($this->headerCheckers, [new JWSTokenSupport()]); |
| 74 |
$headerChecker->check($jws, 0); |
| 75 |
|
| 76 |
$verifier = new JWSVerifier(new AlgorithmManager($this->algorithms)); |
| 77 |
if (! $verifier->verifyWithKeySet($jws, $this->jwkset, 0)) { |
| 78 |
throw new InvalidTokenException('Invalid token signature'); |
| 79 |
} |
| 80 |
|
| 81 |
try { |
| 82 |
|
| 83 |
$claims = JsonConverter::decode($jws->getPayload() ?? '{}'); |
| 84 |
} catch (JsonException $e) { |
| 85 |
throw new InvalidTokenException('Unable to decode JWT payload'); |
| 86 |
} |
| 87 |
|
| 88 |
$claimChecker = new Checker\ClaimCheckerManager($this->claimCheckers); |
| 89 |
try { |
| 90 |
$claimChecker->check($claims, $this->mandatoryClaims); |
| 91 |
} catch (Checker\InvalidHeaderException $e) { |
| 92 |
throw new InvalidTokenException($e->getMessage(), 0, $e); |
| 93 |
} catch (Checker\InvalidClaimException $e) { |
| 94 |
throw new InvalidTokenClaimException($e->getMessage(), $e->getClaim(), $e->getValue(), $e); |
| 95 |
} catch (Checker\MissingMandatoryHeaderParameterException $e) { |
| 96 |
throw new InvalidTokenException($e->getMessage(), 0, $e); |
| 97 |
} catch (Checker\MissingMandatoryClaimException $e) { |
| 98 |
throw new InvalidTokenException($e->getMessage(), 0, $e); |
| 99 |
} catch (Throwable $e) { |
| 100 |
throw new InvalidTokenException('An error occurred validating JWT', 0, $e); |
| 101 |
} |
| 102 |
|
| 103 |
return $claims; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private function getAlgorithmMap(): array |
| 112 |
{ |
| 113 |
return [ |
| 114 |
Algorithm\None::class, |
| 115 |
Algorithm\HS256::class, |
| 116 |
Algorithm\HS384::class, |
| 117 |
Algorithm\HS512::class, |
| 118 |
Algorithm\RS256::class, |
| 119 |
Algorithm\RS384::class, |
| 120 |
Algorithm\RS512::class, |
| 121 |
Algorithm\PS256::class, |
| 122 |
Algorithm\PS384::class, |
| 123 |
Algorithm\PS512::class, |
| 124 |
Algorithm\ES256::class, |
| 125 |
Algorithm\ES384::class, |
| 126 |
Algorithm\ES512::class, |
| 127 |
Algorithm\EdDSA::class, |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function withMandatory(array $mandatoryClaims): static |
| 135 |
{ |
| 136 |
$clone = clone $this; |
| 137 |
$clone->mandatoryClaims = $mandatoryClaims; |
| 138 |
|
| 139 |
return $clone; |
| 140 |
} |
| 141 |
|
| 142 |
public function withClaim(Checker\ClaimChecker $checker): static |
| 143 |
{ |
| 144 |
$clone = clone $this; |
| 145 |
|
| 146 |
$clone->claimCheckers[] = $checker; |
| 147 |
|
| 148 |
return $clone; |
| 149 |
} |
| 150 |
|
| 151 |
public function withHeader(Checker\HeaderChecker $checker): static |
| 152 |
{ |
| 153 |
$clone = clone $this; |
| 154 |
|
| 155 |
$clone->headerCheckers[] = $checker; |
| 156 |
|
| 157 |
return $clone; |
| 158 |
} |
| 159 |
|
| 160 |
public function withJWKSet(JWKSet $jwkset): static |
| 161 |
{ |
| 162 |
$clone = clone $this; |
| 163 |
$clone->jwkset = $jwkset; |
| 164 |
|
| 165 |
return $clone; |
| 166 |
} |
| 167 |
} |