1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Facile\OpenIDClient\Client\Metadata; |
||
6 | |||
7 | use function array_diff; |
||
8 | use function array_filter; |
||
9 | use const ARRAY_FILTER_USE_BOTH; |
||
10 | use function array_key_exists; |
||
11 | use function array_keys; |
||
12 | use function array_merge; |
||
13 | use function count; |
||
14 | use Facile\OpenIDClient\Exception\InvalidArgumentException; |
||
15 | use function implode; |
||
16 | |||
17 | /** |
||
18 | * @psalm-import-type ClientMetadataObject from \Facile\JoseVerifier\Psalm\PsalmTypes |
||
19 | */ |
||
20 | final class ClientMetadata implements ClientMetadataInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var array<string, mixed> |
||
24 | * @psalm-var ClientMetadataObject |
||
25 | */ |
||
26 | private $metadata; |
||
27 | |||
28 | /** @var string[] */ |
||
29 | private static $requiredKeys = [ |
||
30 | 'client_id', |
||
31 | ]; |
||
32 | |||
33 | /** @var array<string, mixed> */ |
||
34 | private static $defaults = []; |
||
35 | |||
36 | /** |
||
37 | * IssuerMetadata constructor. |
||
38 | * |
||
39 | * @param string $clientId |
||
40 | * @param array<string, mixed> $claims |
||
41 | * @psalm-param ClientMetadataObject|array<empty, empty> $claims |
||
42 | */ |
||
43 | 25 | public function __construct(string $clientId, array $claims = []) |
|
44 | { |
||
45 | $requiredClaims = [ |
||
46 | 25 | 'client_id' => $clientId, |
|
47 | ]; |
||
48 | |||
49 | 25 | $defaults = self::$defaults; |
|
50 | |||
51 | /** @var ClientMetadataObject $merged */ |
||
52 | 25 | $merged = array_merge($defaults, $claims, $requiredClaims); |
|
53 | 25 | $this->metadata = $merged; |
|
0 ignored issues
–
show
|
|||
54 | 25 | } |
|
55 | |||
56 | /** |
||
57 | * @param array<string, mixed> $claims |
||
58 | * |
||
59 | * @return static |
||
60 | * |
||
61 | * @psalm-param ClientMetadataObject $claims |
||
62 | */ |
||
63 | 2 | public static function fromArray(array $claims): self |
|
64 | { |
||
65 | 2 | $missingKeys = array_diff(self::$requiredKeys, array_keys($claims)); |
|
66 | 2 | if (0 !== count($missingKeys)) { |
|
67 | 1 | throw new InvalidArgumentException( |
|
68 | 1 | 'Invalid client metadata. Missing keys: ' . implode(', ', $missingKeys) |
|
69 | ); |
||
70 | } |
||
71 | |||
72 | 1 | return new static($claims['client_id'], $claims); |
|
73 | } |
||
74 | |||
75 | 3 | public function getClientId(): string |
|
76 | { |
||
77 | 3 | return $this->metadata['client_id']; |
|
78 | } |
||
79 | |||
80 | 1 | public function getClientSecret(): ?string |
|
81 | { |
||
82 | 1 | return $this->metadata['client_secret'] ?? null; |
|
83 | } |
||
84 | |||
85 | 2 | public function getRedirectUris(): array |
|
86 | { |
||
87 | 2 | return $this->metadata['redirect_uris'] ?? []; |
|
88 | } |
||
89 | |||
90 | 2 | public function getResponseTypes(): array |
|
91 | { |
||
92 | 2 | return $this->metadata['response_types'] ?? ['code']; |
|
93 | } |
||
94 | |||
95 | 4 | public function getTokenEndpointAuthMethod(): string |
|
96 | { |
||
97 | 4 | return $this->metadata['token_endpoint_auth_method'] ?? 'client_secret_basic'; |
|
98 | } |
||
99 | |||
100 | 1 | public function getAuthorizationSignedResponseAlg(): ?string |
|
101 | { |
||
102 | 1 | return $this->metadata['authorization_signed_response_alg'] ?? null; |
|
103 | } |
||
104 | |||
105 | 1 | public function getAuthorizationEncryptedResponseAlg(): ?string |
|
106 | { |
||
107 | 1 | return $this->metadata['authorization_encrypted_response_alg'] ?? null; |
|
108 | } |
||
109 | |||
110 | 1 | public function getAuthorizationEncryptedResponseEnc(): ?string |
|
111 | { |
||
112 | 1 | return $this->metadata['authorization_encrypted_response_enc'] ?? null; |
|
113 | } |
||
114 | |||
115 | 2 | public function getIdTokenSignedResponseAlg(): string |
|
116 | { |
||
117 | 2 | return $this->metadata['id_token_signed_response_alg'] ?? 'RS256'; |
|
118 | } |
||
119 | |||
120 | 1 | public function getIdTokenEncryptedResponseAlg(): ?string |
|
121 | { |
||
122 | 1 | return $this->metadata['id_token_encrypted_response_alg'] ?? null; |
|
123 | } |
||
124 | |||
125 | 1 | public function getIdTokenEncryptedResponseEnc(): ?string |
|
126 | { |
||
127 | 1 | return $this->metadata['id_token_encrypted_response_enc'] ?? null; |
|
128 | } |
||
129 | |||
130 | 1 | public function getUserinfoSignedResponseAlg(): ?string |
|
131 | { |
||
132 | 1 | return $this->metadata['userinfo_signed_response_alg'] ?? null; |
|
133 | } |
||
134 | |||
135 | 1 | public function getUserinfoEncryptedResponseAlg(): ?string |
|
136 | { |
||
137 | 1 | return $this->metadata['userinfo_encrypted_response_alg'] ?? null; |
|
138 | } |
||
139 | |||
140 | 1 | public function getUserinfoEncryptedResponseEnc(): ?string |
|
141 | { |
||
142 | 1 | return $this->metadata['userinfo_encrypted_response_enc'] ?? null; |
|
143 | } |
||
144 | |||
145 | 1 | public function getRequestObjectSigningAlg(): ?string |
|
146 | { |
||
147 | 1 | return $this->metadata['request_object_signing_alg'] ?? null; |
|
148 | } |
||
149 | |||
150 | 1 | public function getRequestObjectEncryptionAlg(): ?string |
|
151 | { |
||
152 | 1 | return $this->metadata['request_object_encryption_alg'] ?? null; |
|
153 | } |
||
154 | |||
155 | 1 | public function getRequestObjectEncryptionEnc(): ?string |
|
156 | { |
||
157 | 1 | return $this->metadata['request_object_encryption_enc'] ?? null; |
|
158 | } |
||
159 | |||
160 | 1 | public function getIntrospectionEndpointAuthMethod(): string |
|
161 | { |
||
162 | 1 | return $this->metadata['introspection_endpoint_auth_method'] ?? $this->getTokenEndpointAuthMethod(); |
|
163 | } |
||
164 | |||
165 | 1 | public function getRevocationEndpointAuthMethod(): string |
|
166 | { |
||
167 | 1 | return $this->metadata['revocation_endpoint_auth_method'] ?? $this->getTokenEndpointAuthMethod(); |
|
168 | } |
||
169 | |||
170 | public function getJwks(): ?array |
||
171 | { |
||
172 | return $this->metadata['jwks'] ?? null; |
||
173 | } |
||
174 | |||
175 | 1 | public function jsonSerialize(): array |
|
176 | { |
||
177 | 1 | return $this->toArray(); |
|
178 | } |
||
179 | |||
180 | 1 | public function toArray(): array |
|
181 | { |
||
182 | 1 | return $this->metadata; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param string $name |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | 1 | public function has(string $name): bool |
|
191 | { |
||
192 | 1 | return array_key_exists($name, $this->metadata); |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param string $name |
||
197 | * |
||
198 | * @return mixed|null |
||
199 | */ |
||
200 | 2 | public function get(string $name) |
|
201 | { |
||
202 | 2 | return $this->metadata[$name] ?? null; |
|
203 | } |
||
204 | } |
||
205 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..