-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathClaimTranslatorExtractor.php
More file actions
391 lines (353 loc) · 12.1 KB
/
ClaimTranslatorExtractor.php
File metadata and controls
391 lines (353 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
declare(strict_types=1);
/*
* This file is part of the simplesamlphp-module-oidc.
*
* Copyright (C) 2018 by the Spanish Research and Academic Network.
*
* This code was developed by Universidad de Córdoba (UCO https://www.uco.es)
* for the RedIRIS SIR service (SIR: http://www.rediris.es/sir)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This file contains modified code from the 'steverhoades/oauth2-openid-connect-server' library
* (https://github.com/steverhoades/oauth2-openid-connect-server), with original author, copyright notice and licence:
* @author Steve Rhoades <sedonami@gmail.com>
* @copyright (c) 2018 Steve Rhoades <sedonami@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace SimpleSAML\Module\oidc\Utils;
use Lcobucci\JWT\Token\RegisteredClaims;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use RuntimeException;
use SimpleSAML\Module\oidc\Entities\Interfaces\ClaimSetEntityInterface;
use SimpleSAML\Module\oidc\Factories\Entities\ClaimSetEntityFactory;
use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException;
class ClaimTranslatorExtractor
{
/** @var array<string, ClaimSetEntityInterface> */
protected array $claimSets = [];
/** @var string[] */
protected array $protectedScopes = ['openid', 'profile', 'email', 'address', 'phone'];
protected array $translationTable = [
'sub' => [
'eduPersonPrincipalName',
'eduPersonTargetedID',
'eduPersonUniqueId',
],
'name' => [
'cn',
'displayName',
],
'family_name' => [
'sn',
],
'given_name' => [
'givenName',
],
'middle_name' => [
// Empty
],
'nickname' => [
'eduPersonNickname',
],
'preferred_username' => [
'uid',
],
'profile' => [
'labeledURI',
'description',
],
'picture' => [
// Empty 'jpegPhoto', Previously 'jpegPhoto' however spec calls for an url to photo, not an actual photo.
],
'website' => [
// Empty
],
'gender' => [
// Empty
],
'birthdate' => [
// Empty
],
'zoneinfo' => [
// Empty
],
'locale' => [
'preferredLanguage',
],
'updated_at' => [
// 'type' => 'int',
],
'email' => [
'mail',
],
'email_verified' => [
// 'type' => 'bool',
],
'address' => [
'type' => 'json',
'claims' => [
'formatted' => ['postalAddress'],
],
],
'phone_number' => [
'mobile',
'telephoneNumber',
'homePhone',
],
'phone_number_verified' => [
// 'type' => 'bool',
// Empty
],
];
/**
* From JSON Web Token Claims registry: https://www.iana.org/assignments/jwt/jwt.xhtml
*/
final public const REGISTERED_CLAIMS = [
...RegisteredClaims::ALL,
'azp',
'nonce',
'auth_time',
'at_hash',
'c_hash',
'acr',
'amr',
'sub_jwk',
];
/**
* As per https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
*/
final public const MANDATORY_SINGLE_VALUE_CLAIMS = [
'sub',
'name',
'given_name',
'family_name',
'middle_name',
'nickname',
'preferred_username',
'profile',
'picture',
'website',
'email',
'email_verified',
'gender',
'birthdate',
'zoneinfo',
'locale',
'phone_number',
'phone_number_verified',
'address',
'updated_at',
];
/**
* ClaimTranslatorExtractor constructor.
*
* @param \SimpleSAML\Module\oidc\Entities\Interfaces\ClaimSetEntityInterface[] $claimSets
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
*/
public function __construct(
string $userIdAttr,
protected readonly ClaimSetEntityFactory $claimSetEntityFactory,
array $claimSets = [],
array $translationTable = [],
protected array $allowedMultiValueClaims = [],
) {
// By default, add the userIdAttribute as one of the attribute for 'sub' claim.
/** @psalm-suppress MixedArgument */
array_unshift($this->translationTable['sub'], $userIdAttr);
$this->translationTable = array_merge($this->translationTable, $translationTable);
$this->addClaimSet($this->claimSetEntityFactory->build('openid', [
'sub',
]));
// Add Default OpenID Connect Claims
// @see http://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims
$this->addClaimSet(
$this->claimSetEntityFactory->build('profile', [
'name',
'family_name',
'given_name',
'middle_name',
'nickname',
'preferred_username',
'profile',
'picture',
'website',
'gender',
'birthdate',
'zoneinfo',
'locale',
'updated_at',
]),
);
$this->addClaimSet(
$this->claimSetEntityFactory->build('email', [
'email',
'email_verified',
]),
);
$this->addClaimSet(
$this->claimSetEntityFactory->build('address', [
'address',
]),
);
$this->addClaimSet(
$this->claimSetEntityFactory->build('phone', [
'phone_number',
'phone_number_verified',
]),
);
foreach ($claimSets as $claimSet) {
$this->addClaimSet($claimSet);
}
}
/**
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
*/
public function addClaimSet(ClaimSetEntityInterface $claimSet): self
{
$scope = $claimSet->getScope();
if (in_array($scope, $this->protectedScopes, true) && isset($this->claimSets[$scope])) {
throw OidcServerException::serverError(
sprintf("%s is a protected scope and is pre-defined by the OpenID Connect specification.", $scope),
);
}
$this->claimSets[$scope] = $claimSet;
return $this;
}
public function getClaimSet(string $scope): ?ClaimSetEntityInterface
{
if (!$this->hasClaimSet($scope)) {
return null;
}
return $this->claimSets[$scope];
}
public function hasClaimSet(string $scope): bool
{
return array_key_exists($scope, $this->claimSets);
}
private function translateSamlAttributesToClaims(array $translationTable, array $samlAttributes): array
{
$claims = [];
/**
* @var string $claim
* @var array $mappingConfig
*/
foreach ($translationTable as $claim => $mappingConfig) {
$type = (string)($mappingConfig['type'] ?? 'string');
unset($mappingConfig['type']);
if ($type === 'json') {
$mappingConfigClaims = is_array($mappingConfig['claims']) ? $mappingConfig['claims'] : [];
$subClaims = $this->translateSamlAttributesToClaims($mappingConfigClaims, $samlAttributes);
$claims[$claim] = $subClaims;
continue;
}
// Look for attributes in the attribute key, if not set then assume to legacy style configuration
$attributes = isset($mappingConfig['attributes']) && is_array($mappingConfig['attributes']) ?
$mappingConfig['attributes'] :
$mappingConfig;
/** @var string $samlMatch */
foreach ($attributes as $samlMatch) {
if (array_key_exists($samlMatch, $samlAttributes)) {
/** @psalm-suppress MixedAssignment, MixedArgument */
$values = (!in_array($claim, self::MANDATORY_SINGLE_VALUE_CLAIMS, true)) &&
in_array($claim, $this->allowedMultiValueClaims, true) ?
$samlAttributes[$samlMatch] :
current($samlAttributes[$samlMatch]);
/** @psalm-suppress MixedAssignment */
$claims[$claim] = $this->convertType($type, $values);
break;
}
}
}
return $claims;
}
private function convertType(string $type, mixed $attributes): mixed
{
if (is_array($attributes)) {
$values = [];
/** @psalm-suppress MixedAssignment */
foreach ($attributes as $attribute) {
/** @psalm-suppress MixedAssignment */
$values[] = $this->convertType($type, $attribute);
}
return $values;
}
switch ($type) {
case 'int':
if (is_numeric($attributes)) {
return (int)$attributes;
} else {
throw new RuntimeException("Cannot convert '$attributes' to int");
}
case 'bool':
return filter_var($attributes, FILTER_VALIDATE_BOOLEAN);
}
return $attributes;
}
/**
* @param array<array-key, string|\League\OAuth2\Server\Entities\ScopeEntityInterface> $scopes
*/
public function extract(array $scopes, array $claims): array
{
$claims = $this->translateSamlAttributesToClaims($this->translationTable, $claims);
$claimData = [];
$keys = array_keys($claims);
foreach ($scopes as $scope) {
$scopeName = ($scope instanceof ScopeEntityInterface) ? $scope->getIdentifier() : $scope;
$claimSet = $this->getClaimSet($scopeName);
if (null === $claimSet) {
continue;
}
$intersected = array_intersect($claimSet->getClaims(), $keys);
if (empty($intersected)) {
continue;
}
$data = array_filter(
$claims,
fn($key) => in_array($key, $intersected, true),
ARRAY_FILTER_USE_KEY,
);
$claimData = array_merge($claimData, $data);
}
return $claimData;
}
public function extractAdditionalIdTokenClaims(?array $claimsRequest, array $claims): array
{
/** @var array $idTokenClaims */
$idTokenClaims = $claimsRequest['id_token'] ?? [];
return $this->extractAdditionalClaims($idTokenClaims, $claims);
}
public function extractAdditionalUserInfoClaims(?array $claimsRequest, array $claims): array
{
/** @var array $userInfoClaims */
$userInfoClaims = $claimsRequest['userinfo'] ?? [];
return $this->extractAdditionalClaims($userInfoClaims, $claims);
}
/**
* Add any individually requested claims
* @link https://openid.net/specs/openid-connect-core-1_0.html#IndividualClaimsRequests
* @param array $requestedClaims keys are requested claims, value is array of additional info on the request
*/
private function extractAdditionalClaims(array $requestedClaims, array $claims): array
{
if (empty($requestedClaims)) {
return [];
}
$translatedClaims = $this->translateSamlAttributesToClaims($this->translationTable, $claims);
return array_filter(
$translatedClaims,
fn(/** @param array-key $key */ $key) => array_key_exists($key, $requestedClaims),
ARRAY_FILTER_USE_KEY,
);
}
/**
* Get supported claims for this OP. This will return all the claims for which the "SAML attribute to OIDC claim
* translation" has been defined in module config, meaning it is expected for OP to release those claims.
*/
public function getSupportedClaims(): array
{
return array_keys(array_filter($this->translationTable));
}
}