Debian Codegraph @

lib/3rdParty/composer/facile-it/php-jose-verifier/src/IdTokenVerifier.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Facile\JoseVerifier;
6
7 use Facile\JoseVerifier\Exception\InvalidTokenException;
8 use Facile\JoseVerifier\Internal\Checker\AtHashChecker;
9 use Facile\JoseVerifier\Internal\Checker\CHashChecker;
10 use Facile\JoseVerifier\Internal\Checker\SHashChecker;
11 use InvalidArgumentException;
12 use Jose\Component\Signature\Serializer\CompactSerializer;
13
14 final class IdTokenVerifier extends AbstractTokenVerifier implements IdTokenVerifierInterface
15 {
16 protected ?string $accessToken = null;
17
18 protected ?string $code = null;
19
20 protected ?string $state = null;
21
22 public function withAccessToken(?string $accessToken): static
23 {
24 $new = clone $this;
25 $new->accessToken = $accessToken;
26
27 return $new;
28 }
29
30 public function withCode(?string $code): static
31 {
32 $new = clone $this;
33 $new->code = $code;
34
35 return $new;
36 }
37
38 public function withState(?string $state): static
39 {
40 $new = clone $this;
41 $new->state = $state;
42
43 return $new;
44 }
45
46 public function verify(string $jwt): array
47 {
48 $jwt = $this->decrypt($jwt);
49
50 try {
51 $jws = (new CompactSerializer())->unserialize($jwt);
52 } catch (InvalidArgumentException $e) {
53 throw new InvalidTokenException('Invalid JWT provided', 0, $e);
54 }
55
56 $header = $jws->getSignature(0)->getProtectedHeader();
57
58 $validator = $this->create($jwt);
59
60 $requiredClaims = ['iss', 'sub', 'aud', 'exp', 'iat'];
61 $alg = $header['alg'] ?? null;
62
63 if (null !== $this->accessToken) {
64 $validator = $validator->withClaim(new AtHashChecker($this->accessToken, $alg ?? ''));
65 }
66
67 if (null !== $this->code) {
68 $validator = $validator->withClaim(new CHashChecker($this->code, $alg ?? ''));
69 }
70
71 if (null !== $this->state) {
72 $validator = $validator->withClaim(new SHashChecker($this->state, $alg ?? ''));
73 }
74
75 $validator = $validator->withMandatory($requiredClaims);
76
77 return $validator->run();
78 }
79 }