Debian Codegraph @

lib/3rdParty/composer/facile-it/php-openid-client/src/Service/RegistrationService.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Facile\OpenIDClient\Service;
6
7 use Facile\OpenIDClient\Exception\InvalidArgumentException;
8 use Facile\OpenIDClient\Exception\RuntimeException;
9 use Facile\OpenIDClient\Issuer\IssuerInterface;
10 use JsonException;
11 use Psr\Http\Client\ClientExceptionInterface;
12 use Psr\Http\Client\ClientInterface;
13 use Psr\Http\Message\RequestFactoryInterface;
14
15 use function array_diff_key;
16 use function array_flip;
17 use function array_intersect_key;
18 use function array_key_exists;
19 use function array_merge;
20 use function Facile\OpenIDClient\check_server_response;
21 use function Facile\OpenIDClient\parse_metadata_response;
22 use function json_encode;
23
24 /**
25 * Dynamic Client Registration Protocol.
26 *
27 * @see https://tools.ietf.org/html/rfc7591
28 * @see https://openid.net/specs/openid-connect-registration-1_0.html
29 *
30 * @psalm-api
31 */
32 final class RegistrationService
33 {
34 private ClientInterface $client;
35
36 private RequestFactoryInterface $requestFactory;
37
38 /** @var string[] */
39 private static array $registrationClaims = [
40 'registration_access_token',
41 'registration_client_uri',
42 'client_secret_expires_at',
43 'client_id_issued_at',
44 ];
45
46 public function __construct(
47 ClientInterface $client,
48 RequestFactoryInterface $requestFactory
49 ) {
50 $this->client = $client;
51 $this->requestFactory = $requestFactory;
52 }
53
54 /**
55 * @param array<string, mixed> $metadata
56 *
57 * @return array<string, mixed>
58 */
59 public function register(
60 IssuerInterface $issuer,
61 array $metadata,
62 ?string $initialToken = null
63 ): array {
64 $registrationEndpoint = $issuer->getMetadata()->getRegistrationEndpoint();
65
66 if (null === $registrationEndpoint) {
67 throw new InvalidArgumentException('Issuer does not support dynamic client registration');
68 }
69
70 try {
71 $encodedMetadata = json_encode($metadata, JSON_THROW_ON_ERROR);
72 } catch (JsonException $e) {
73 throw new RuntimeException('Unable to encode client metadata', 0, $e);
74 }
75
76 $request = $this->requestFactory->createRequest('POST', $registrationEndpoint)
77 ->withHeader('content-type', 'application/json')
78 ->withHeader('accept', 'application/json');
79
80 $request->getBody()->write($encodedMetadata);
81
82 if (null !== $initialToken) {
83 $request = $request->withHeader('authorization', 'Bearer ' . $initialToken);
84 }
85
86 try {
87 $response = $this->client->sendRequest($request);
88 } catch (ClientExceptionInterface $e) {
89 throw new RuntimeException('Unable to register OpenID client', 0, $e);
90 }
91
92 $data = parse_metadata_response($response, 201);
93
94 if (! array_key_exists('client_id', $data)) {
95 throw new RuntimeException('Registration response did not return a client_id field');
96 }
97
98 return $data;
99 }
100
101 /**
102 * @return array<string, mixed>
103 */
104 public function read(string $clientUri, string $accessToken): array
105 {
106 $request = $this->requestFactory->createRequest('GET', $clientUri)
107 ->withHeader('accept', 'application/json')
108 ->withHeader('authorization', 'Bearer ' . $accessToken);
109
110 try {
111 $response = $this->client->sendRequest($request);
112 } catch (ClientExceptionInterface $e) {
113 throw new RuntimeException('Unable to read OpenID client', 0, $e);
114 }
115
116 $claims = parse_metadata_response($response, 200);
117
118 if (! array_key_exists('client_id', $claims)) {
119 throw new RuntimeException('Registration response did not return a client_id field');
120 }
121
122 return $claims;
123 }
124
125 /**
126 * @param array<string, mixed> $metadata
127 *
128 * @return array<string, mixed>
129 */
130 public function update(
131 string $clientUri,
132 string $accessToken,
133 array $metadata
134 ): array {
135 $clientRegistrationMetadata = array_intersect_key($metadata, array_flip(self::$registrationClaims));
136 $metadata = array_diff_key($metadata, $clientRegistrationMetadata);
137
138 try {
139 $encodedMetadata = json_encode($metadata, JSON_THROW_ON_ERROR);
140 } catch (JsonException $e) {
141 throw new RuntimeException('Unable to encode client metadata', 0, $e);
142 }
143
144 $request = $this->requestFactory->createRequest('PUT', $clientUri)
145 ->withHeader('accept', 'application/json')
146 ->withHeader('content-type', 'application/json')
147 ->withHeader('authorization', 'Bearer ' . $accessToken);
148
149 $request->getBody()->write($encodedMetadata);
150
151 try {
152 $response = $this->client->sendRequest($request);
153 } catch (ClientExceptionInterface $e) {
154 throw new RuntimeException('Unable to update OpenID client', 0, $e);
155 }
156
157 $data = parse_metadata_response($response, 200);
158
159 if (! array_key_exists('client_id', $data)) {
160 throw new RuntimeException('Registration response did not return a client_id field');
161 }
162
163 /** @var array<string, mixed> $merged */
164 $merged = array_merge($clientRegistrationMetadata, $data);
165
166 return $merged;
167 }
168
169 public function delete(string $clientUri, string $accessToken): void
170 {
171 $request = $this->requestFactory->createRequest('DELETE', $clientUri)
172 ->withHeader('authorization', 'Bearer ' . $accessToken);
173
174 try {
175 $response = $this->client->sendRequest($request);
176 } catch (ClientExceptionInterface $e) {
177 throw new RuntimeException('Unable to delete OpenID client', 0, $e);
178 }
179
180 check_server_response($response, 204);
181 }
182 }