Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/NestedToken/NestedTokenLoader.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\NestedToken;
6
7 use InvalidArgumentException;
8 use Jose\Component\Core\JWKSet;
9 use Jose\Component\Encryption\JWE;
10 use Jose\Component\Encryption\JWELoader;
11 use Jose\Component\Signature\JWS;
12 use Jose\Component\Signature\JWSLoader;
13 use function is_string;
14
15 class NestedTokenLoader
16 {
17 public function __construct(
18 private readonly JWELoader $jweLoader,
19 private readonly JWSLoader $jwsLoader
20 ) {
21 }
22
23 /**
24 * This method will try to load, decrypt and verify the token. In case of failure, an exception is thrown, otherwise
25 * returns the JWS and populates the $signature variable.
26 */
27 public function load(string $token, JWKSet $encryptionKeySet, JWKSet $signatureKeySet, ?int &$signature = null): JWS
28 {
29 $recipient = null;
30 $jwe = $this->jweLoader->loadAndDecryptWithKeySet($token, $encryptionKeySet, $recipient);
31 $this->checkContentTypeHeader($jwe, $recipient);
32 if ($jwe->getPayload() === null) {
33 throw new InvalidArgumentException('The token has no payload.');
34 }
35
36 return $this->jwsLoader->loadAndVerifyWithKeySet($jwe->getPayload(), $signatureKeySet, $signature);
37 }
38
39 private function checkContentTypeHeader(JWE $jwe, int $recipient): void
40 {
41 $cty = match (true) {
42 $jwe->hasSharedProtectedHeaderParameter('cty') => $jwe->getSharedProtectedHeaderParameter('cty'),
43 $jwe->hasSharedHeaderParameter('cty') => $jwe->getSharedHeaderParameter('cty'),
44 $jwe->getRecipient($recipient)
45 ->hasHeaderParameter('cty') => $jwe->getRecipient($recipient)
46 ->getHeaderParameter('cty'),
47 default => throw new InvalidArgumentException('The token is not a nested token.'),
48 };
49 if (! is_string($cty)) {
50 throw new InvalidArgumentException('Invalid "cty" header parameter.');
51 }
52
53 if (strcasecmp($cty, 'jwt') !== 0) {
54 throw new InvalidArgumentException('The token is not a nested token.');
55 }
56 }
57 }