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