ClientMetadata::getRedirectUris()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\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 function implode;
15
use TMV\OpenIdClient\Exception\InvalidArgumentException;
16
17
final class ClientMetadata implements ClientMetadataInterface
18
{
19
    /** @var array<string, mixed> */
20
    private $metadata;
21
22
    /** @var string[] */
23
    private static $requiredKeys = [
24
        'client_id',
25
    ];
26
27
    /** @var array<string, mixed> */
28
    private static $defaults = [
29
        'client_id' => null,
30
        'redirect_uris' => [],
31
        'client_secret' => null,
32
        'jwks' => null,
33
        'jwks_uri' => null,
34
        'id_token_signed_response_alg' => 'RS256',
35
        'id_token_encrypted_response_alg' => null,
36
        'id_token_encrypted_response_enc' => null,
37
        'userinfo_signed_response_alg' => null,
38
        'userinfo_encrypted_response_alg' => null,
39
        'userinfo_encrypted_response_enc' => null,
40
        'response_types' => ['code'],
41
        'post_logout_redirect_uris' => [],
42
        'require_auth_time' => false,
43
        'request_object_signing_alg' => null,
44
        'request_object_encryption_alg' => null,
45
        'request_object_encryption_enc' => null,
46
        'token_endpoint_auth_method' => 'client_secret_basic',
47
        //'introspection_endpoint_auth_method' => 'client_secret_basic',
48
        //'revocation_endpoint_auth_method' => 'client_secret_basic',
49
        'token_endpoint_auth_signing_alg' => null,
50
        'introspection_endpoint_auth_signing_alg' => null,
51
        'revocation_endpoint_auth_signing_alg' => null,
52
        'tls_client_certificate_bound_access_tokens' => false,
53
    ];
54
55
    /**
56
     * IssuerMetadata constructor.
57
     *
58
     * @param string $clientId
59
     * @param array<string, mixed> $claims
60
     */
61 24
    public function __construct(string $clientId, $claims = [])
62
    {
63
        $requiredClaims = [
64 24
            'client_id' => $clientId,
65
        ];
66
67 24
        $defaults = self::$defaults;
68
69 24
        $this->metadata = array_merge($defaults, $claims, $requiredClaims);
70 24
    }
71
72 2
    public static function fromArray(array $claims): self
73
    {
74 2
        $missingKeys = array_diff(self::$requiredKeys, array_keys($claims));
75 2
        if (0 !== count($missingKeys)) {
76 1
            throw new InvalidArgumentException(
77 1
                'Invalid client metadata. Missing keys: ' . implode(', ', $missingKeys)
78
            );
79
        }
80
81 1
        return new self($claims['client_id'], $claims);
82
    }
83
84 2
    public function getClientId(): string
85
    {
86 2
        return $this->metadata['client_id'];
87
    }
88
89 1
    public function getClientSecret(): ?string
90
    {
91 1
        return $this->metadata['client_secret'] ?? null;
92
    }
93
94 2
    public function getRedirectUris(): array
95
    {
96 2
        return $this->metadata['redirect_uris'] ?? [];
97
    }
98
99 1
    public function getResponseTypes(): array
100
    {
101 1
        return $this->metadata['response_types'] ?? ['code'];
102
    }
103
104 4
    public function getTokenEndpointAuthMethod(): string
105
    {
106 4
        return $this->metadata['token_endpoint_auth_method'];
107
    }
108
109 1
    public function getAuthorizationSignedResponseAlg(): ?string
110
    {
111 1
        return $this->metadata['authorization_signed_response_alg'] ?? null;
112
    }
113
114 1
    public function getAuthorizationEncryptedResponseAlg(): ?string
115
    {
116 1
        return $this->metadata['authorization_encrypted_response_alg'] ?? null;
117
    }
118
119 1
    public function getAuthorizationEncryptedResponseEnc(): ?string
120
    {
121 1
        return $this->metadata['authorization_encrypted_response_enc'] ?? null;
122
    }
123
124 1
    public function getIdTokenSignedResponseAlg(): string
125
    {
126 1
        return $this->metadata['id_token_signed_response_alg'];
127
    }
128
129 1
    public function getIdTokenEncryptedResponseAlg(): ?string
130
    {
131 1
        return $this->metadata['id_token_encrypted_response_alg'] ?? null;
132
    }
133
134 1
    public function getIdTokenEncryptedResponseEnc(): ?string
135
    {
136 1
        return $this->metadata['id_token_encrypted_response_enc'] ?? null;
137
    }
138
139 1
    public function getUserinfoSignedResponseAlg(): ?string
140
    {
141 1
        return $this->metadata['userinfo_signed_response_alg'] ?? null;
142
    }
143
144 1
    public function getUserinfoEncryptedResponseAlg(): ?string
145
    {
146 1
        return $this->metadata['userinfo_encrypted_response_alg'] ?? null;
147
    }
148
149 1
    public function getUserinfoEncryptedResponseEnc(): ?string
150
    {
151 1
        return $this->metadata['userinfo_encrypted_response_enc'] ?? null;
152
    }
153
154 1
    public function getRequestObjectSigningAlg(): ?string
155
    {
156 1
        return $this->metadata['request_object_signing_alg'] ?? null;
157
    }
158
159 1
    public function getRequestObjectEncryptionAlg(): ?string
160
    {
161 1
        return $this->metadata['request_object_encryption_alg'] ?? null;
162
    }
163
164 1
    public function getRequestObjectEncryptionEnc(): ?string
165
    {
166 1
        return $this->metadata['request_object_encryption_enc'] ?? null;
167
    }
168
169 1
    public function getIntrospectionEndpointAuthMethod(): string
170
    {
171 1
        return $this->metadata['introspection_endpoint_auth_method'] ?? $this->getTokenEndpointAuthMethod();
172
    }
173
174 1
    public function getRevocationEndpointAuthMethod(): string
175
    {
176 1
        return $this->metadata['revocation_endpoint_auth_method'] ?? $this->getTokenEndpointAuthMethod();
177
    }
178
179
    /**
180
     * @return array<string, mixed>
181
     */
182 1
    private function getFilteredClaims(): array
183
    {
184
        return array_filter($this->metadata, static function ($value, string $key) {
185 1
            return array_key_exists($key, self::$requiredKeys)
186 1
                || $value !== (self::$defaults[$key] ?? null);
187 1
        }, ARRAY_FILTER_USE_BOTH);
188
    }
189
190
    /**
191
     * @return array<string, mixed>
192
     */
193 1
    public function jsonSerialize(): array
194
    {
195 1
        return $this->getFilteredClaims();
196
    }
197
198
    /**
199
     * @param string $name
200
     *
201
     * @return bool
202
     */
203 1
    public function has(string $name): bool
204
    {
205 1
        return array_key_exists($name, $this->metadata);
206
    }
207
208
    /**
209
     * @param string $name
210
     *
211
     * @return mixed|null
212
     */
213 2
    public function get(string $name)
214
    {
215 2
        return $this->metadata[$name] ?? null;
216
    }
217
}
218