Passed
Push — master ( a721e7...5904cd )
by Rutger
13:40
created

Oauth2BaseAuthorizationRequest::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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