1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\components\openidconnect\scopes; |
4
|
|
|
|
5
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcClaimInterface; |
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\InvalidCallException; |
11
|
|
|
use yii\helpers\ArrayHelper; |
12
|
|
|
|
13
|
|
|
class Oauth2OidcScope extends BaseObject implements Oauth2OidcScopeInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string|null |
17
|
|
|
*/ |
18
|
|
|
protected $_identifier; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $_claims; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritDoc |
27
|
|
|
*/ |
28
|
3 |
|
public function getIdentifier() |
29
|
|
|
{ |
30
|
3 |
|
if (empty($this->_identifier)) { |
31
|
1 |
|
throw new InvalidCallException('Trying to get scope identifier without it being set.'); |
32
|
|
|
} |
33
|
2 |
|
return $this->_identifier; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
2 |
|
public function setIdentifier($identifier) |
40
|
|
|
{ |
41
|
2 |
|
$this->_identifier = $identifier; |
42
|
2 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
2 |
|
public function getClaims() |
49
|
|
|
{ |
50
|
2 |
|
return $this->_claims; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
2 |
|
public function setClaims($claims) |
57
|
|
|
{ |
58
|
2 |
|
$this->clearClaims(); |
59
|
2 |
|
return $this->addClaims($claims); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
5 |
|
public function addClaims($claims) |
66
|
|
|
{ |
67
|
5 |
|
foreach ($claims as $claimIdentifier => $claimConfig) { |
68
|
5 |
|
if ($claimConfig instanceof Oauth2OidcClaimInterface) { |
69
|
1 |
|
$this->addClaim($claimConfig); |
70
|
5 |
|
} elseif (is_callable($claimConfig)) { |
71
|
2 |
|
if (is_numeric($claimIdentifier)) { |
72
|
1 |
|
throw new InvalidArgumentException( |
73
|
1 |
|
'If an element is an callable it should be declared as an associative element.' |
74
|
1 |
|
); |
75
|
|
|
} |
76
|
|
|
// e.g. ['claim_identifier' => ['CustomClass', 'customFunction']]. |
77
|
1 |
|
$this->addClaim([ |
78
|
1 |
|
'identifier' => $claimIdentifier, |
79
|
1 |
|
'determiner' => $claimConfig, |
80
|
1 |
|
]); |
81
|
4 |
|
} elseif (is_string($claimConfig)) { |
82
|
2 |
|
if (is_numeric($claimIdentifier)) { |
83
|
|
|
// e.g. ['claim_identifier']. |
84
|
2 |
|
$this->addClaim($claimConfig); |
85
|
|
|
} else { |
86
|
|
|
// e.g. ['claim_identifier' => 'determiner']. |
87
|
2 |
|
$this->addClaim([ |
88
|
2 |
|
'identifier' => $claimIdentifier, |
89
|
2 |
|
'determiner' => $claimConfig, |
90
|
2 |
|
]); |
91
|
|
|
} |
92
|
3 |
|
} elseif (is_array($claimConfig)) { |
93
|
2 |
|
if (is_numeric($claimIdentifier) && !array_key_exists('identifier', $claimConfig)) { |
94
|
1 |
|
throw new InvalidArgumentException( |
95
|
1 |
|
'If an element is an array it should either be declared as an associative element' |
96
|
1 |
|
. ' or contain an "identifier" key.' |
97
|
1 |
|
); |
98
|
|
|
} |
99
|
|
|
// e.g. ['claim' => [...]]. |
100
|
1 |
|
$this->addClaim(ArrayHelper::merge( |
101
|
1 |
|
[ |
102
|
1 |
|
'identifier' => $claimIdentifier, |
103
|
1 |
|
], |
104
|
1 |
|
$claimConfig |
105
|
1 |
|
)); |
106
|
|
|
} else { |
107
|
1 |
|
throw new InvalidArgumentException( |
108
|
1 |
|
'Elements must either be an array, string or a ' . Oauth2OidcClaimInterface::class |
109
|
1 |
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritDoc |
118
|
|
|
*/ |
119
|
2 |
|
public function clearClaims() |
120
|
|
|
{ |
121
|
2 |
|
$this->_claims = []; |
122
|
2 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritDoc |
127
|
|
|
*/ |
128
|
2 |
|
public function getClaim($claimIdentifier) |
129
|
|
|
{ |
130
|
2 |
|
return $this->_claims[$claimIdentifier] ?? null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @inheritDoc |
135
|
|
|
*/ |
136
|
3 |
|
public function addClaim($claim) |
137
|
|
|
{ |
138
|
3 |
|
if (is_string($claim)) { |
139
|
3 |
|
$claim = ['identifier' => $claim]; |
140
|
|
|
} |
141
|
3 |
|
if (is_array($claim)) { |
142
|
3 |
|
$claim = Yii::createObject(ArrayHelper::merge( |
143
|
3 |
|
[ |
144
|
3 |
|
'class' => Oauth2OidcClaimInterface::class, |
145
|
3 |
|
], |
146
|
3 |
|
$claim |
147
|
3 |
|
)); |
148
|
|
|
} |
149
|
|
|
|
150
|
3 |
|
$identifier = $claim->getIdentifier(); |
151
|
3 |
|
$this->_claims[$identifier] = $claim; |
152
|
3 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @inheritDoc |
157
|
|
|
*/ |
158
|
1 |
|
public function removeClaim($claimIdentifier) |
159
|
|
|
{ |
160
|
1 |
|
unset($this->_claims[$claimIdentifier]); |
161
|
1 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @inheritDoc |
166
|
|
|
*/ |
167
|
2 |
|
public function hasClaim($claimIdentifier) |
168
|
|
|
{ |
169
|
2 |
|
return array_key_exists($claimIdentifier, $this->_claims); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|