Debian Codegraph @

lib/3rdParty/composer/facile-it/php-openid-client/src/AuthMethod/ClientSecretJwt.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Facile\OpenIDClient\AuthMethod;
6
7 use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient;
8 use Facile\OpenIDClient\Exception\InvalidArgumentException;
9 use Facile\OpenIDClient\Exception\LogicException;
10 use Jose\Component\Core\AlgorithmManager;
11 use Jose\Component\Signature\Algorithm\HS256;
12 use Jose\Component\Signature\JWSBuilder;
13 use Jose\Component\Signature\Serializer\CompactSerializer;
14 use Jose\Component\Signature\Serializer\JWSSerializer;
15 use Override;
16
17 use function class_exists;
18 use function Facile\OpenIDClient\base64url_encode;
19 use function Facile\OpenIDClient\get_endpoint_uri;
20 use function Facile\OpenIDClient\jose_secret_key;
21 use function json_encode;
22 use function random_bytes;
23 use function time;
24
25 final class ClientSecretJwt extends AbstractJwtAuth
26 {
27 private ?JWSBuilder $jwsBuilder;
28
29 private JWSSerializer $jwsSerializer;
30
31 /**
32 * ClientSecretJwt constructor.
33 */
34 public function __construct(
35 ?JWSBuilder $jwsBuilder = null,
36 ?JWSSerializer $jwsSerializer = null
37 ) {
38 $this->jwsBuilder = $jwsBuilder;
39 $this->jwsSerializer = $jwsSerializer ?? new CompactSerializer();
40 }
41
42 #[Override]
43 public function getSupportedMethod(): string
44 {
45 return 'client_secret_jwt';
46 }
47
48 private function getJwsBuilder(): JWSBuilder
49 {
50 if (null !== $this->jwsBuilder) {
51 return $this->jwsBuilder;
52 }
53
54 if (! class_exists(HS256::class)) {
55 throw new LogicException('To use the client_secret_jwt auth method you should install web-token/jwt-signature-algorithm-hmac package');
56 }
57
58 return $this->jwsBuilder = new JWSBuilder(new AlgorithmManager([new HS256()]));
59 }
60
61 #[Override]
62 protected function createAuthJwt(OpenIDClient $client, array $claims = []): string
63 {
64 $clientSecret = $client->getMetadata()->getClientSecret();
65
66 if (null === $clientSecret) {
67 throw new InvalidArgumentException($this->getSupportedMethod() . ' cannot be used without client_secret metadata');
68 }
69
70 $clientId = $client->getMetadata()->getClientId();
71
72 $jwk = jose_secret_key($clientSecret);
73
74 $time = time();
75 $jti = base64url_encode(random_bytes(32));
76
77 $payload = json_encode($claims
78 + [
79 'iss' => $clientId,
80 'sub' => $clientId,
81 'aud' => get_endpoint_uri($client, 'token_endpoint'),
82 'iat' => $time,
83 'exp' => $time + 60,
84 'jti' => $jti,
85 ], JSON_THROW_ON_ERROR);
86
87 $jws = $this->getJwsBuilder()->create()
88 ->withPayload($payload)
89 ->addSignature($jwk, ['alg' => 'HS256', 'jti' => $jti])
90 ->build();
91
92 return $this->jwsSerializer->serialize($jws, 0);
93 }
94 }