| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\JoseVerifier; |
| 6 |
|
| 7 |
use Facile\JoseVerifier\Internal\InternalClock; |
| 8 |
use Facile\JoseVerifier\Decrypter\NullTokenDecrypter; |
| 9 |
use Facile\JoseVerifier\Decrypter\TokenDecrypterInterface; |
| 10 |
use Facile\JoseVerifier\Exception\InvalidTokenException; |
| 11 |
use Facile\JoseVerifier\Internal\Checker\AuthTimeChecker; |
| 12 |
use Facile\JoseVerifier\Internal\Checker\AzpChecker; |
| 13 |
use Facile\JoseVerifier\Internal\Checker\NonceChecker; |
| 14 |
use Facile\JoseVerifier\Internal\Validate; |
| 15 |
use Facile\JoseVerifier\JWK\JwksProviderInterface; |
| 16 |
use Facile\JoseVerifier\JWK\MemoryJwksProvider; |
| 17 |
use InvalidArgumentException; |
| 18 |
use Jose\Component\Checker\AlgorithmChecker; |
| 19 |
use Jose\Component\Checker\AudienceChecker; |
| 20 |
use Jose\Component\Checker\ExpirationTimeChecker; |
| 21 |
use Jose\Component\Checker\IssuedAtChecker; |
| 22 |
use Jose\Component\Checker\IssuerChecker; |
| 23 |
use Jose\Component\Checker\NotBeforeChecker; |
| 24 |
use Jose\Component\Core\JWK; |
| 25 |
use Jose\Component\Core\JWKSet; |
| 26 |
use Jose\Component\Core\Util\JsonConverter; |
| 27 |
use Jose\Component\Signature\Serializer\CompactSerializer; |
| 28 |
use RuntimeException; |
| 29 |
use Psr\Clock\ClockInterface; |
| 30 |
|
| 31 |
use function is_array; |
| 32 |
use function str_replace; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
abstract class AbstractTokenVerifier implements TokenVerifierInterface |
| 40 |
{ |
| 41 |
protected string $issuer; |
| 42 |
|
| 43 |
protected string $clientId; |
| 44 |
|
| 45 |
protected ?string $clientSecret; |
| 46 |
|
| 47 |
protected ?string $expectedAzp; |
| 48 |
|
| 49 |
protected ?string $expectedAlg; |
| 50 |
|
| 51 |
protected int $clockTolerance; |
| 52 |
|
| 53 |
protected bool $authTimeRequired; |
| 54 |
|
| 55 |
protected bool $aadIssValidation; |
| 56 |
|
| 57 |
protected JwksProviderInterface $jwksProvider; |
| 58 |
|
| 59 |
protected TokenDecrypterInterface $decrypter; |
| 60 |
|
| 61 |
protected ?string $nonce = null; |
| 62 |
|
| 63 |
protected ?int $maxAge = null; |
| 64 |
|
| 65 |
protected ClockInterface $clock; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
final public function __construct( |
| 73 |
string $issuer, |
| 74 |
string $clientId, |
| 75 |
?string $clientSecret = null, |
| 76 |
bool $authTimeRequired = false, |
| 77 |
int $clockTolerance = 0, |
| 78 |
bool $aadIssValidation = false, |
| 79 |
?string $expectedAzp = null, |
| 80 |
?string $expectedAlg = null, |
| 81 |
?JwksProviderInterface $jwksProvider = null, |
| 82 |
?TokenDecrypterInterface $decrypter = null, |
| 83 |
?ClockInterface $clock = null, |
| 84 |
) { |
| 85 |
$this->issuer = $issuer; |
| 86 |
$this->clientId = $clientId; |
| 87 |
$this->clientSecret = $clientSecret; |
| 88 |
$this->authTimeRequired = $authTimeRequired; |
| 89 |
$this->clockTolerance = $clockTolerance; |
| 90 |
$this->aadIssValidation = $aadIssValidation; |
| 91 |
$this->expectedAzp = $expectedAzp; |
| 92 |
$this->expectedAlg = $expectedAlg; |
| 93 |
$this->jwksProvider = $jwksProvider ?? new MemoryJwksProvider(); |
| 94 |
$this->decrypter = $decrypter ?? new NullTokenDecrypter(); |
| 95 |
$this->clock = $clock ?? new InternalClock(); |
| 96 |
} |
| 97 |
|
| 98 |
public function withNonce(?string $nonce): static |
| 99 |
{ |
| 100 |
$new = clone $this; |
| 101 |
$new->nonce = $nonce; |
| 102 |
|
| 103 |
return $new; |
| 104 |
} |
| 105 |
|
| 106 |
public function withMaxAge(?int $maxAge): static |
| 107 |
{ |
| 108 |
$new = clone $this; |
| 109 |
$new->maxAge = $maxAge; |
| 110 |
|
| 111 |
return $new; |
| 112 |
} |
| 113 |
|
| 114 |
protected function decrypt(string $jwt): string |
| 115 |
{ |
| 116 |
return $this->decrypter->decrypt($jwt) ?? '{}'; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
protected function create(string $jwt): Validate |
| 123 |
{ |
| 124 |
$mandatoryClaims = []; |
| 125 |
|
| 126 |
$expectedIssuer = $this->issuer; |
| 127 |
|
| 128 |
if ($this->aadIssValidation) { |
| 129 |
$payload = $this->getPayload($jwt); |
| 130 |
$expectedIssuer = str_replace('{tenantid}', (string) ($payload['tid'] ?? ''), $expectedIssuer); |
| 131 |
} |
| 132 |
|
| 133 |
$validator = Validate::withToken($jwt) |
| 134 |
->withJWKSet($this->buildJwks($jwt)) |
| 135 |
->withClaim(new IssuerChecker([$expectedIssuer], true)) |
| 136 |
->withClaim(new IssuedAtChecker($this->clockTolerance, true, $this->clock)) |
| 137 |
->withClaim(new AudienceChecker($this->clientId, true)) |
| 138 |
->withClaim(new ExpirationTimeChecker($this->clockTolerance, false, $this->clock)) |
| 139 |
->withClaim(new NotBeforeChecker($this->clockTolerance, true, $this->clock)); |
| 140 |
|
| 141 |
if (null !== $this->expectedAzp) { |
| 142 |
$validator = $validator->withClaim(new AzpChecker($this->expectedAzp)); |
| 143 |
} |
| 144 |
|
| 145 |
if (null !== $this->expectedAlg) { |
| 146 |
$validator = $validator->withHeader(new AlgorithmChecker([$this->expectedAlg], true)); |
| 147 |
} |
| 148 |
|
| 149 |
if (null !== $this->nonce) { |
| 150 |
$validator = $validator->withClaim(new NonceChecker($this->nonce)); |
| 151 |
} |
| 152 |
|
| 153 |
if (null !== $this->maxAge) { |
| 154 |
$validator = $validator->withClaim(new AuthTimeChecker($this->maxAge, $this->clockTolerance, $this->clock)); |
| 155 |
} |
| 156 |
|
| 157 |
if ($this->authTimeRequired || (int) $this->maxAge > 0 || null !== $this->maxAge) { |
| 158 |
$mandatoryClaims[] = 'auth_time'; |
| 159 |
} |
| 160 |
|
| 161 |
return $validator->withMandatory($mandatoryClaims); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
protected function getPayload(string $jwt): array |
| 172 |
{ |
| 173 |
try { |
| 174 |
$jws = (new CompactSerializer())->unserialize($jwt); |
| 175 |
} catch (InvalidArgumentException $e) { |
| 176 |
throw new InvalidTokenException('Invalid JWT provided', 0, $e); |
| 177 |
} |
| 178 |
|
| 179 |
try { |
| 180 |
$payload = JsonConverter::decode($jws->getPayload() ?? '{}'); |
| 181 |
} catch (RuntimeException $e) { |
| 182 |
throw new InvalidTokenException('Unable to decode JWT payload', 0, $e); |
| 183 |
} |
| 184 |
|
| 185 |
if (! is_array($payload)) { |
| 186 |
throw new InvalidTokenException('Invalid token provided'); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
return $payload; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
private function buildJwks(string $jwt): JWKSet |
| 197 |
{ |
| 198 |
try { |
| 199 |
$jws = (new CompactSerializer())->unserialize($jwt); |
| 200 |
} catch (InvalidArgumentException $e) { |
| 201 |
throw new InvalidTokenException('Invalid JWT provided', 0, $e); |
| 202 |
} |
| 203 |
|
| 204 |
$header = $jws->getSignature(0)->getProtectedHeader(); |
| 205 |
|
| 206 |
$alg = $header['alg'] ?? ''; |
| 207 |
|
| 208 |
|
| 209 |
$kid = $header['kid'] ?? null; |
| 210 |
|
| 211 |
return $this->getSigningJWKSet($alg, $kid); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
private function getSigningJWKSet(string $alg, ?string $kid = null): JWKSet |
| 218 |
{ |
| 219 |
if (! str_starts_with($alg, 'HS')) { |
| 220 |
|
| 221 |
return null !== $kid |
| 222 |
? new JWKSet([$this->getJWKFromKid($kid)]) |
| 223 |
: JWKSet::createFromKeyData($this->jwksProvider->getJwks()); |
| 224 |
} |
| 225 |
|
| 226 |
if (null === $this->clientSecret) { |
| 227 |
throw new InvalidTokenException('Signature requires client_secret to be verified'); |
| 228 |
} |
| 229 |
|
| 230 |
return new JWKSet([jose_secret_key($this->clientSecret)]); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private function getJWKFromKid(string $kid): JWK |
| 237 |
{ |
| 238 |
$jwks = JWKSet::createFromKeyData($this->jwksProvider->getJwks()); |
| 239 |
$jwk = $jwks->selectKey('sig', null, ['kid' => $kid]); |
| 240 |
|
| 241 |
if (null === $jwk) { |
| 242 |
$jwks = JWKSet::createFromKeyData($this->jwksProvider->reload()->getJwks()); |
| 243 |
$jwk = $jwks->selectKey('sig', null, ['kid' => $kid]); |
| 244 |
} |
| 245 |
|
| 246 |
if (null === $jwk) { |
| 247 |
throw new InvalidTokenException('Unable to find the jwk with the provided kid: ' . $kid); |
| 248 |
} |
| 249 |
|
| 250 |
return $jwk; |
| 251 |
} |
| 252 |
} |