1 | <?php |
||
2 | |||
3 | namespace Parroauth2\Client\Jwt; |
||
4 | |||
5 | use Exception; |
||
6 | use InvalidArgumentException; |
||
7 | use Jose\Component\Core\JWKSet; |
||
8 | use Jose\Component\Signature\JWSLoader; |
||
9 | use Jose\Component\Signature\JWSVerifier; |
||
10 | use Jose\Component\Signature\Serializer\CompactSerializer; |
||
11 | use Jose\Component\Signature\Serializer\JWSSerializerManager; |
||
12 | |||
13 | /** |
||
14 | * Decode utility for JWT |
||
15 | * |
||
16 | * @todo handle JWE |
||
17 | */ |
||
18 | final class JwtDecoder |
||
19 | { |
||
20 | /** |
||
21 | * @var JWA |
||
22 | */ |
||
23 | private $jwa; |
||
24 | |||
25 | /** |
||
26 | * @var JWSSerializerManager |
||
27 | */ |
||
28 | private $serializerManager; |
||
29 | |||
30 | /** |
||
31 | * JwtParser constructor. |
||
32 | * |
||
33 | * @param JWA|null $jwa |
||
34 | * @param JWSSerializerManager|null $serializerManager |
||
35 | */ |
||
36 | 124 | public function __construct(?JWA $jwa = null, ?JWSSerializerManager $serializerManager = null) |
|
37 | { |
||
38 | 124 | $this->jwa = $jwa ?: new JWA(); |
|
39 | 124 | $this->serializerManager = $serializerManager ?: new JWSSerializerManager([new CompactSerializer()]); |
|
40 | 124 | } |
|
41 | |||
42 | /** |
||
43 | * Get the supported algorithms |
||
44 | * |
||
45 | * @return JWA |
||
46 | */ |
||
47 | 19 | public function jwa(): JWA |
|
48 | { |
||
49 | 19 | return $this->jwa; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Define supported algorithms |
||
54 | * |
||
55 | * @param string[] $algorithms |
||
56 | * |
||
57 | * @return self A new JwtDecoder instance, with filtered algorithms |
||
58 | * |
||
59 | * @see JWA::filter() |
||
60 | */ |
||
61 | 1 | public function supportedAlgorithms(array $algorithms): self |
|
62 | { |
||
63 | 1 | $decoder = clone $this; |
|
64 | |||
65 | 1 | $decoder->jwa = $decoder->jwa->filter($algorithms); |
|
66 | |||
67 | 1 | return $decoder; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Decode the JWT string |
||
72 | * |
||
73 | * @param string $jwt String to decode |
||
74 | * @param JWKSet $keySet Keys to use |
||
75 | * |
||
76 | * @return JWT |
||
77 | * |
||
78 | * @throws InvalidArgumentException When cannot decode the JWT string |
||
79 | */ |
||
80 | 38 | public function decode(string $jwt, JWKSet $keySet): JWT |
|
81 | { |
||
82 | 38 | $loader = new JWSLoader( |
|
83 | 38 | $this->serializerManager, |
|
84 | 38 | new JWSVerifier($this->jwa->manager()), |
|
85 | 38 | null |
|
86 | ); |
||
87 | |||
88 | try { |
||
89 | 38 | $decoded = $loader->loadAndVerifyWithKeySet($jwt, $keySet, $signatureOffset); |
|
90 | 11 | } catch (Exception $e) { |
|
91 | 11 | throw new InvalidArgumentException('Invalid ID Token or signature', 0, $e); |
|
92 | } |
||
93 | |||
94 | /** @psalm-suppress PossiblyNullArrayOffset */ |
||
95 | 28 | $signature = $decoded->getSignatures()[$signatureOffset]; |
|
96 | |||
97 | /** @psalm-suppress PossiblyNullArgument */ |
||
98 | 28 | return new JWT( |
|
99 | 28 | $jwt, |
|
100 | 28 | $signature->getProtectedHeader() + $signature->getHeader(), |
|
101 | 28 | json_decode($decoded->getPayload(), true) |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
102 | ); |
||
103 | } |
||
104 | } |
||
105 |