1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\components\authorization\base; |
4
|
|
|
|
5
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\authorization\base\Oauth2BaseAuthorizationRequestInterface; |
6
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2UserInterface; |
7
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface; |
8
|
|
|
use rhertogh\Yii2Oauth2Server\Oauth2Module; |
9
|
|
|
use yii\base\InvalidArgumentException; |
10
|
|
|
use yii\base\InvalidCallException; |
11
|
|
|
use yii\base\Model; |
12
|
|
|
use yii\helpers\StringHelper; |
13
|
|
|
|
14
|
|
|
abstract class Oauth2BaseAuthorizationRequest extends Model implements Oauth2BaseAuthorizationRequestInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Oauth2Module|null |
18
|
|
|
*/ |
19
|
|
|
public $_module = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
protected $_requestId = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int|null |
28
|
|
|
*/ |
29
|
|
|
public $_clientIdentifier = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Oauth2ClientInterface|null |
33
|
|
|
*/ |
34
|
|
|
protected $_client = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int|string|null |
38
|
|
|
*/ |
39
|
|
|
protected $_userIdentifier = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Oauth2UserInterface|null |
43
|
|
|
*/ |
44
|
|
|
protected $_userIdentity = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string|null |
48
|
|
|
*/ |
49
|
|
|
public $_redirectUri = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string|null |
53
|
|
|
*/ |
54
|
|
|
public $_authorizationStatus = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
protected $_isCompleted = false; |
60
|
|
|
|
61
|
3 |
|
public static function getPossibleAuthorizationStatuses() |
62
|
|
|
{ |
63
|
3 |
|
return [ |
64
|
3 |
|
Oauth2BaseAuthorizationRequestInterface::AUTHORIZATION_APPROVED, |
65
|
3 |
|
Oauth2BaseAuthorizationRequestInterface::AUTHORIZATION_DENIED, |
66
|
3 |
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
1 |
|
public function __serialize() |
73
|
|
|
{ |
74
|
1 |
|
return [ |
75
|
1 |
|
'_requestId' => $this->_requestId, |
76
|
1 |
|
'_clientIdentifier' => $this->_clientIdentifier, |
77
|
1 |
|
'_userIdentifier' => $this->_userIdentifier, |
78
|
1 |
|
'_redirectUri' => $this->_redirectUri, |
79
|
1 |
|
'_authorizationStatus' => $this->_authorizationStatus, |
80
|
1 |
|
'_isCompleted' => $this->_isCompleted, |
81
|
1 |
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
1 |
|
public function __unserialize($data) |
88
|
|
|
{ |
89
|
1 |
|
foreach ($data as $name => $value) { |
90
|
1 |
|
$this->$name = $value; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
14 |
|
public function init() |
98
|
|
|
{ |
99
|
14 |
|
parent::init(); |
100
|
14 |
|
$this->_requestId = \Yii::$app->security->generateRandomString(128); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritDoc |
105
|
|
|
*/ |
106
|
6 |
|
public function getModule() |
107
|
|
|
{ |
108
|
6 |
|
if (empty($this->_module)) { |
109
|
1 |
|
throw new InvalidCallException('Can not call getModule() before it\'s set.'); |
110
|
|
|
} |
111
|
5 |
|
return $this->_module; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
*/ |
117
|
5 |
|
public function setModule($module) |
118
|
|
|
{ |
119
|
5 |
|
$this->_module = $module; |
120
|
5 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritDoc |
125
|
|
|
*/ |
126
|
3 |
|
public function getRequestId() |
127
|
|
|
{ |
128
|
3 |
|
return $this->_requestId; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritDoc |
133
|
|
|
*/ |
134
|
4 |
|
public function getClientIdentifier() |
135
|
|
|
{ |
136
|
4 |
|
return $this->_clientIdentifier; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritDoc |
141
|
|
|
*/ |
142
|
4 |
|
public function setClientIdentifier($clientIdentifier) |
143
|
|
|
{ |
144
|
4 |
|
if ($this->_client && $this->_client->getIdentifier() !== $clientIdentifier) { |
145
|
1 |
|
$this->_client = null; |
146
|
|
|
} |
147
|
|
|
|
148
|
4 |
|
$this->_clientIdentifier = $clientIdentifier; |
149
|
4 |
|
$this->setCompleted(false); |
150
|
4 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritDoc |
155
|
|
|
*/ |
156
|
|
|
public function getClient() |
157
|
|
|
{ |
158
|
|
|
$clientIdentifier = $this->getClientIdentifier(); |
159
|
|
|
if (empty($clientIdentifier)) { |
160
|
|
|
return null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (empty($this->_client) || $this->_client->getIdentifier() != $clientIdentifier) { |
164
|
|
|
$this->_client = $this->getModule()->getClientRepository()->getClientEntity($clientIdentifier); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->_client; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritDoc |
172
|
|
|
*/ |
173
|
1 |
|
public function setClient($client) |
174
|
|
|
{ |
175
|
1 |
|
$this->_client = $client; |
176
|
1 |
|
$this->setClientIdentifier($client ? $client->getIdentifier() : null); |
177
|
1 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @inheritDoc |
182
|
|
|
*/ |
183
|
3 |
|
public function isClientIdentifiable() |
184
|
|
|
{ |
185
|
3 |
|
return |
186
|
3 |
|
$this->getClient()->isConfidential() |
187
|
3 |
|
|| StringHelper::startsWith((string)$this->getRedirectUri(), 'https://'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the user identifier. |
192
|
|
|
* @return string|int|null $userIdentifier |
193
|
|
|
* @since 1.0.0 |
194
|
|
|
*/ |
195
|
4 |
|
protected function getUserIdentifier() |
196
|
|
|
{ |
197
|
4 |
|
return $this->_userIdentifier; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Set the user identifier. |
202
|
|
|
* @param string|int $userIdentifier |
203
|
|
|
* @return $this |
204
|
|
|
* @since 1.0.0 |
205
|
|
|
*/ |
206
|
5 |
|
protected function setUserIdentifier($userIdentifier) |
207
|
|
|
{ |
208
|
5 |
|
$this->_userIdentifier = $userIdentifier; |
209
|
5 |
|
if ($this->_userIdentity && $this->_userIdentity->getIdentifier() !== $userIdentifier) { |
210
|
1 |
|
$this->_userIdentity = null; |
211
|
|
|
} |
212
|
5 |
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @inheritDoc |
217
|
|
|
*/ |
218
|
5 |
|
public function getUserIdentity() |
219
|
|
|
{ |
220
|
5 |
|
if ($this->_userIdentity === null && $this->_userIdentifier !== null) { |
221
|
1 |
|
$this->_userIdentity = $this->getModule() |
222
|
1 |
|
->getUserRepository() |
223
|
1 |
|
->getUserEntityByIdentifier($this->_userIdentifier); |
224
|
|
|
} |
225
|
5 |
|
return $this->_userIdentity; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @inheritDoc |
230
|
|
|
*/ |
231
|
5 |
|
public function setUserIdentity($userIdentity) |
232
|
|
|
{ |
233
|
5 |
|
$this->_userIdentity = $userIdentity; |
234
|
5 |
|
$this->setUserIdentifier($userIdentity->getIdentifier()); |
235
|
5 |
|
$this->setCompleted(false); |
236
|
5 |
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @inheritDoc |
241
|
|
|
*/ |
242
|
2 |
|
public function getRedirectUri() |
243
|
|
|
{ |
244
|
2 |
|
return $this->_redirectUri; |
245
|
|
|
} |
246
|
|
|
/** |
247
|
|
|
* @inheritDoc |
248
|
|
|
*/ |
249
|
4 |
|
public function setRedirectUri($redirectUri) |
250
|
|
|
{ |
251
|
4 |
|
$this->_redirectUri = $redirectUri; |
252
|
4 |
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @inheritDoc |
257
|
|
|
*/ |
258
|
2 |
|
public function getAuthorizationStatus() |
259
|
|
|
{ |
260
|
2 |
|
return $this->_authorizationStatus; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @inheritDoc |
265
|
|
|
*/ |
266
|
4 |
|
public function setAuthorizationStatus($authorizationStatus) |
267
|
|
|
{ |
268
|
4 |
|
if ($authorizationStatus !== null && !in_array($authorizationStatus, $this->getPossibleAuthorizationStatuses())) { |
269
|
1 |
|
throw new InvalidArgumentException('$authorizationStatus must be null or exist in the return value of `getPossibleAuthorizationStatuses()`.'); |
270
|
|
|
} |
271
|
|
|
|
272
|
3 |
|
$this->_authorizationStatus = $authorizationStatus; |
273
|
3 |
|
$this->setCompleted(false); |
274
|
3 |
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @inheritDoc |
279
|
|
|
*/ |
280
|
1 |
|
public function isApproved() |
281
|
|
|
{ |
282
|
1 |
|
return $this->getAuthorizationStatus() === static::AUTHORIZATION_APPROVED; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param bool $isCompleted |
287
|
|
|
* @return $this |
288
|
|
|
*/ |
289
|
9 |
|
protected function setCompleted($isCompleted) |
290
|
|
|
{ |
291
|
9 |
|
$this->_isCompleted = $isCompleted; |
292
|
9 |
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @inheritDoc |
297
|
|
|
*/ |
298
|
6 |
|
public function isCompleted() |
299
|
|
|
{ |
300
|
6 |
|
return $this->_isCompleted; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @inheritDoc |
305
|
|
|
*/ |
306
|
2 |
|
public function rules() |
307
|
|
|
{ |
308
|
2 |
|
return [ |
309
|
2 |
|
[['authorizationStatus'], 'required'], |
310
|
2 |
|
[['authorizationStatus'], 'in', 'range' => [static::AUTHORIZATION_APPROVED, static::AUTHORIZATION_DENIED]], |
311
|
2 |
|
]; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|