1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\OpenIdConnect\UserInfo\Claim; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccount; |
17
|
|
|
|
18
|
|
|
class ClaimManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Claim[] |
22
|
|
|
*/ |
23
|
|
|
private $claims = []; |
24
|
|
|
|
25
|
|
|
public function add(Claim $claim): void |
26
|
|
|
{ |
27
|
|
|
$this->claims[$claim->name()] = $claim; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return Claim[] |
32
|
|
|
*/ |
33
|
|
|
public function list(): array |
34
|
|
|
{ |
35
|
|
|
return array_keys($this->claims); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return Claim[] |
40
|
|
|
*/ |
41
|
|
|
public function all(): array |
42
|
|
|
{ |
43
|
|
|
return $this->claims; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function has(string $claim): bool |
47
|
|
|
{ |
48
|
|
|
return \array_key_exists($claim, $this->claims); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function get(string $claim): Claim |
52
|
|
|
{ |
53
|
|
|
if (!$this->has($claim)) { |
54
|
|
|
throw new \InvalidArgumentException(\Safe\sprintf('Unsupported claim "%s".', $claim)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->claims[$claim]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getUserInfo(UserAccount $userAccount, array $claims, array $claimLocales): array |
61
|
|
|
{ |
62
|
|
|
$result = []; |
63
|
|
|
$claimLocales[] = null; |
64
|
|
|
foreach ($claims as $claimName => $config) { |
65
|
|
|
if ($this->has($claimName)) { |
66
|
|
|
$claim = $this->get($claimName); |
67
|
|
|
foreach ($claimLocales as $claimLocale) { |
68
|
|
|
if ($claim->isAvailableForUserAccount($userAccount, $claimLocale)) { |
69
|
|
|
$value = $claim->getForUserAccount($userAccount, $claimLocale); |
70
|
|
|
switch (true) { |
71
|
|
|
case \is_array($config) && \array_key_exists('value', $config): |
72
|
|
|
if ($claim === $config['value']) { |
73
|
|
|
$result[$claimName] = $value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
break; |
77
|
|
|
case \is_array($config) && \array_key_exists('values', $config) && \is_array($config['values']): |
78
|
|
|
if (\in_array($claim, $config['values'], true)) { |
79
|
|
|
$result[$claimName] = $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
break; |
83
|
|
|
default: |
84
|
|
|
$result[$claimName] = $value; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|