1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\components\openidconnect\scopes; |
4
|
|
|
|
5
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeCollectionInterface; |
6
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeInterface; |
7
|
|
|
use Yii; |
8
|
|
|
use yii\base\BaseObject; |
9
|
|
|
use yii\base\InvalidArgumentException; |
10
|
|
|
use yii\base\InvalidConfigException; |
11
|
|
|
use yii\helpers\ArrayHelper; |
12
|
|
|
|
13
|
|
|
class Oauth2OidcScopeCollection extends BaseObject implements Oauth2OidcScopeCollectionInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Oauth2OidcScopeInterface[] |
17
|
|
|
*/ |
18
|
|
|
protected $_oidcScopes = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritDoc |
22
|
|
|
*/ |
23
|
4 |
|
public function getOidcScopes() |
24
|
|
|
{ |
25
|
4 |
|
if (!array_key_exists(Oauth2OidcScopeInterface::OPENID_CONNECT_SCOPE_OPENID, $this->_oidcScopes)) { |
26
|
4 |
|
$this->_oidcScopes = array_merge( // ensure openid scope is always the first element |
27
|
|
|
[ |
28
|
|
|
Oauth2OidcScopeInterface::OPENID_CONNECT_SCOPE_OPENID => |
29
|
4 |
|
$this->getDefaultOidcScope(Oauth2OidcScopeInterface::OPENID_CONNECT_SCOPE_OPENID) |
30
|
|
|
], |
31
|
4 |
|
$this->_oidcScopes |
32
|
|
|
); |
33
|
|
|
} |
34
|
4 |
|
return $this->_oidcScopes; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
3 |
|
public function setOidcScopes($oidcScopes) |
41
|
|
|
{ |
42
|
3 |
|
$this->clearOidcScopes(); |
43
|
3 |
|
return $this->addOidcScopes($oidcScopes); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
4 |
|
public function addOidcScopes($oidcScopes) |
50
|
|
|
{ |
51
|
4 |
|
foreach ($oidcScopes as $scopeIdentifier => $scopeConfig) { |
52
|
4 |
|
if ($scopeConfig instanceof Oauth2OidcScopeInterface) { |
53
|
1 |
|
$this->addOidcScope($scopeConfig); |
54
|
4 |
|
} elseif (is_string($scopeConfig)) { |
55
|
1 |
|
$this->addOidcScope($scopeConfig); |
56
|
4 |
|
} elseif (is_array($scopeConfig)) { |
57
|
3 |
|
if (is_numeric($scopeIdentifier)) { |
58
|
1 |
|
$this->addOidcScope($scopeConfig); |
59
|
|
|
} else { |
60
|
3 |
|
$this->addOidcScope([ |
61
|
3 |
|
'identifier' => $scopeIdentifier, |
62
|
3 |
|
'claims' => $scopeConfig, |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
1 |
|
throw new InvalidArgumentException( |
67
|
1 |
|
'Elements should be of type array, string or ' . Oauth2OidcScopeInterface::class |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
3 |
|
public function clearOidcScopes() |
79
|
|
|
{ |
80
|
3 |
|
$this->_oidcScopes = []; |
81
|
3 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
3 |
|
public function getOidcScope($scopeIdentifier) |
88
|
|
|
{ |
89
|
3 |
|
return $this->getOidcScopes()[$scopeIdentifier] ?? null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc |
94
|
|
|
*/ |
95
|
4 |
|
public function addOidcScope($oidcScope) |
96
|
|
|
{ |
97
|
4 |
|
if (is_string($oidcScope)) { |
98
|
2 |
|
$oidcScope = $this->getDefaultOidcScope($oidcScope); |
99
|
4 |
|
} elseif (is_array($oidcScope)) { |
100
|
4 |
|
$oidcScope = Yii::createObject(ArrayHelper::merge( |
101
|
|
|
[ |
102
|
4 |
|
'class' => Oauth2OidcScopeInterface::class, |
103
|
|
|
], |
104
|
4 |
|
$oidcScope |
105
|
|
|
)); |
106
|
|
|
} |
107
|
4 |
|
$identifier = $oidcScope->getIdentifier(); |
108
|
4 |
|
if (empty($identifier)) { |
109
|
|
|
throw new InvalidArgumentException('Scope identifier must be set.'); |
110
|
|
|
} |
111
|
4 |
|
$this->_oidcScopes[$identifier] = $oidcScope; |
112
|
4 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritDoc |
117
|
|
|
*/ |
118
|
1 |
|
public function removeOidcScope($scopeIdentifier) |
119
|
|
|
{ |
120
|
1 |
|
unset($this->_oidcScopes[$scopeIdentifier]); |
121
|
1 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritDoc |
126
|
|
|
*/ |
127
|
2 |
|
public function hasOidcScope($scopeIdentifier) |
128
|
|
|
{ |
129
|
2 |
|
return array_key_exists($scopeIdentifier, $this->getOidcScopes()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritDoc |
134
|
|
|
*/ |
135
|
6 |
|
public function getDefaultOidcScope($scopeIdentifier) |
136
|
|
|
{ |
137
|
6 |
|
if (!in_array($scopeIdentifier, static::OPENID_CONNECT_DEFAULT_SCOPES)) { |
138
|
1 |
|
throw new InvalidArgumentException( |
139
|
1 |
|
'Invalid $scopeName "' . $scopeIdentifier . '", it must be an OpenID Connect default claims scope (' |
140
|
1 |
|
. implode(', ', static::OPENID_CONNECT_DEFAULT_SCOPES) . ').' |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
5 |
|
return Yii::createObject([ |
145
|
5 |
|
'class' => Oauth2OidcScopeInterface::class, |
146
|
5 |
|
'identifier' => $scopeIdentifier, |
147
|
5 |
|
'claims' => Oauth2OidcScopeInterface::OPENID_CONNECT_DEFAULT_SCOPE_CLAIMS[$scopeIdentifier], |
148
|
|
|
]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritDoc |
153
|
|
|
*/ |
154
|
1 |
|
public function getSupportedScopeAndClaimIdentifiers() |
155
|
|
|
{ |
156
|
|
|
$result = [ |
157
|
1 |
|
'scopeIdentifiers' => [], |
158
|
|
|
'claimIdentifiers' => [], |
159
|
|
|
]; |
160
|
|
|
|
161
|
1 |
|
foreach ($this->getOidcScopes() as $oidcScope) { |
162
|
1 |
|
$result['scopeIdentifiers'][] = $oidcScope->getIdentifier(); |
163
|
1 |
|
$result['claimIdentifiers'] = array_merge($result['claimIdentifiers'], $oidcScope->getClaims()); |
164
|
|
|
} |
165
|
1 |
|
$result['claimIdentifiers'] = array_keys($result['claimIdentifiers']); |
166
|
1 |
|
sort($result['claimIdentifiers']); |
167
|
|
|
|
168
|
1 |
|
return $result; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @inheritDoc |
173
|
|
|
*/ |
174
|
1 |
|
public function getFilteredClaims($scopeIdentifiers) |
175
|
|
|
{ |
176
|
1 |
|
$claims = []; |
177
|
1 |
|
foreach ($scopeIdentifiers as $scopeIdentifier) { |
178
|
1 |
|
$oidcScope = $this->getOidcScope($scopeIdentifier); |
179
|
1 |
|
if ($oidcScope) { |
180
|
1 |
|
$claims = array_merge($claims, $oidcScope->getClaims()); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
1 |
|
ksort($claims); |
185
|
|
|
|
186
|
1 |
|
return $claims; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|