Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Facile\JoseVerifier\Decrypter;
6
7 use Facile\JoseVerifier\Exception\InvalidTokenException;
8 use Facile\JoseVerifier\Exception\LogicException;
9 use Facile\JoseVerifier\Internal\Checker\ContentEncryptionAlgorithmChecker;
10 use Facile\JoseVerifier\JWK\JwksProviderInterface;
11 use Facile\JoseVerifier\JWK\MemoryJwksProvider;
12 use Jose\Component\Checker\AlgorithmChecker;
13 use Jose\Component\Checker\HeaderCheckerManager;
14 use Jose\Component\Core\Algorithm;
15 use Jose\Component\Core\AlgorithmManager;
16 use Jose\Component\Core\JWKSet;
17 use Jose\Component\Encryption\Algorithm\ContentEncryption;
18 use Jose\Component\Encryption\Algorithm\KeyEncryption;
19 use Jose\Component\Encryption\JWEDecrypter;
20 use Jose\Component\Encryption\JWELoader;
21 use Jose\Component\Encryption\JWETokenSupport;
22 use Jose\Component\Encryption\Serializer\CompactSerializer;
23 use Jose\Component\Encryption\Serializer\JWESerializerManager;
24 use Throwable;
25
26 use function class_exists;
27 use function Facile\JoseVerifier\jose_secret_key;
28 use function preg_match;
29
30 final class TokenDecrypter implements TokenDecrypterInterface
31 {
32 private ?string $expectedAlg = null;
33
34 private ?string $expectedEnc = null;
35
36 private JwksProviderInterface $jwksProvider;
37
38 private ?string $clientSecret = null;
39
40 /** @var Algorithm[] */
41 private array $algorithms = [];
42
43 public function withExpectedAlg(?string $expectedAlg): static
44 {
45 $new = clone $this;
46 $new->expectedAlg = $expectedAlg;
47
48 return $new;
49 }
50
51 public function withExpectedEnc(?string $expectedEnc): static
52 {
53 $new = clone $this;
54 $new->expectedEnc = $expectedEnc;
55
56 return $new;
57 }
58
59 public function withJwksProvider(JwksProviderInterface $jwksProvider): static
60 {
61 $new = clone $this;
62 $new->jwksProvider = $jwksProvider;
63
64 return $new;
65 }
66
67 public function withClientSecret(?string $clientSecret): static
68 {
69 $new = clone $this;
70 $new->clientSecret = $clientSecret;
71
72 return $new;
73 }
74
75 public function __construct()
76 {
77 $this->jwksProvider = new MemoryJwksProvider();
78 foreach ($this->getAlgorithmMap() as $algorithmClass) {
79 if (class_exists($algorithmClass)) {
80 try {
81 $this->algorithms[] = new $algorithmClass();
82 } catch (Throwable $throwable) {
83 // does nothing
84 }
85 }
86 }
87 }
88
89 private function buildJwks(string $jwt): JWKSet
90 {
91 $jwe = (new CompactSerializer())->unserialize($jwt);
92 $header = $jwe->getSharedProtectedHeader();
93
94 $alg = $header['alg'] ?? '';
95 $enc = $header['enc'] ?? '';
96
97 if (preg_match('/^(?:RSA|ECDH)/', $alg)) {
98 $jwks = JWKSet::createFromKeyData($this->jwksProvider->getJwks());
99 } else {
100 $jwk = jose_secret_key($this->clientSecret ?? '', $alg === 'dir' ? $enc : $alg);
101 $jwks = new JWKSet([$jwk]);
102 }
103
104 return $jwks;
105 }
106
107 /**
108 * @throws InvalidTokenException
109 * @throws LogicException When web-token/jwt-encryption is not installed
110 */
111 public function decrypt(string $jwt): ?string
112 {
113 if (! class_exists(JWELoader::class)) {
114 throw new LogicException('In order to decrypt JWT you should install web-token/jwt-encryption package');
115 }
116
117 $headerCheckers = [];
118
119 if (null !== $this->expectedAlg) {
120 $headerCheckers[] = new AlgorithmChecker([$this->expectedAlg], true);
121 }
122
123 if (null !== $this->expectedEnc) {
124 $headerCheckers[] = new ContentEncryptionAlgorithmChecker([$this->expectedEnc], true);
125 }
126
127 $headerChecker = new HeaderCheckerManager($headerCheckers, [new JWETokenSupport()]);
128
129 $jweLoader = new JWELoader(
130 new JWESerializerManager([new CompactSerializer()]),
131 new JWEDecrypter(
132 new AlgorithmManager($this->algorithms),
133 new AlgorithmManager($this->algorithms)
134 ),
135 $headerChecker
136 );
137
138 try {
139 return $jweLoader->loadAndDecryptWithKeySet(
140 $jwt,
141 $this->buildJwks($jwt),
142 $recipient
143 )->getPayload();
144 } catch (Throwable $e) {
145 throw new InvalidTokenException('Unable to decrypt JWE', 0, $e);
146 }
147 }
148
149 /**
150 * @return string[]
151 *
152 * @psalm-return list<class-string<Algorithm>>
153 */
154 protected function getAlgorithmMap(): array
155 {
156 return [
157 KeyEncryption\A128GCMKW::class,
158 KeyEncryption\A192GCMKW::class,
159 KeyEncryption\A128GCMKW::class,
160 KeyEncryption\A192GCMKW::class,
161 KeyEncryption\A256GCMKW::class,
162 ...(class_exists('AESKW\Wrapper') ? [
163 KeyEncryption\A128KW::class,
164 KeyEncryption\A192KW::class,
165 KeyEncryption\A256KW::class,
166 KeyEncryption\ECDHESA128KW::class,
167 KeyEncryption\ECDHESA192KW::class,
168 KeyEncryption\ECDHESA256KW::class,
169 ] : []),
170 KeyEncryption\Dir::class,
171 KeyEncryption\ECDHES::class,
172 KeyEncryption\PBES2HS256A128KW::class,
173 KeyEncryption\PBES2HS384A192KW::class,
174 KeyEncryption\PBES2HS512A256KW::class,
175 KeyEncryption\RSA15::class,
176 KeyEncryption\RSAOAEP::class,
177 KeyEncryption\RSAOAEP256::class,
178 ContentEncryption\A128GCM::class,
179 ContentEncryption\A192GCM::class,
180 ContentEncryption\A256GCM::class,
181 ContentEncryption\A128CBCHS256::class,
182 ContentEncryption\A192CBCHS384::class,
183 ContentEncryption\A256CBCHS512::class,
184 ];
185 }
186 }