Passed
Push — master ( 8022de...a1e1aa )
by Thomas Mauro
03:06
created

ClientMetadata::has()   A

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