OauthStorage::getClientDetails()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.7333
cc 4
nc 6
nop 1
1
<?php
2
/**
3
 * OauthStorage.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author pgaultier
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version 1.2.0
11
 * @link http://www.sweelix.net
12
 * @packages sweelix\oauth2\server\storage
13
 */
14
15
namespace sweelix\oauth2\server\storage;
16
17
use OAuth2\Encryption\Jwt;
18
use OAuth2\OpenID\Storage\AuthorizationCodeInterface;
19
use OAuth2\Storage\AccessTokenInterface;
20
use OAuth2\Storage\ClientCredentialsInterface;
21
use OAuth2\Storage\JwtAccessTokenInterface;
22
use OAuth2\Storage\JwtBearerInterface;
23
use OAuth2\Storage\PublicKeyInterface;
24
use OAuth2\Storage\RefreshTokenInterface;
25
use OAuth2\Storage\ScopeInterface;
26
use OAuth2\Storage\UserCredentialsInterface;
27
use Yii;
28
use yii\helpers\ArrayHelper;
29
30
/**
31
 * OauthStorage class
32
 *
33
 * PHP version 5.6+
34
 *
35
 * @author pgaultier
36
 * @copyright 2010-2017 Philippe Gaultier
37
 * @license http://www.sweelix.net/license license
38
 * @version 1.2.0
39
 * @link http://www.sweelix.net
40
 * @packages sweelix\oauth2\server\storage
41
 * @since 1.0.0
42
 */
43
class OauthStorage implements
44
    AccessTokenInterface,
45
    AuthorizationCodeInterface,
46
    ClientCredentialsInterface,
47
    JwtAccessTokenInterface, // identical to AccessTokenInterface
48
    JwtBearerInterface,
49
    PublicKeyInterface,
50
    RefreshTokenInterface,
51
    ScopeInterface,
52
    UserCredentialsInterface
53
{
54
    /**
55
     * @var string
56
     */
57
    private $accessTokenClass;
58
59
    /**
60
     * @var string
61
     */
62
    private $authCodeClass;
63
64
    /**
65
     * @var string
66
     */
67
    private $clientClass;
68
69
    /**
70
     * @var string
71
     */
72
    private $cypherKeyClass;
73
74
    /**
75
     * @var string
76
     */
77
    private $jtiClass;
78
79
    /**
80
     * @var string
81
     */
82
    private $jwtClass;
83
84
    /**
85
     * @var string
86
     */
87
    private $refreshTokenClass;
88
89
    /**
90
     * @var string
91
     */
92
    private $scopeClass;
93
94
    /**
95
     * @var string
96
     */
97
    private $userClass;
98
99
    /**
100
     * @return string classname for selected interface
101
     * @throws \yii\base\InvalidConfigException
102
     * @since 1.0.0
103
     */
104
    protected function getAccessTokenClass()
105
    {
106
        if ($this->accessTokenClass === null) {
107
            $accessToken = Yii::createObject('sweelix\oauth2\server\interfaces\AccessTokenModelInterface');
108
            $this->accessTokenClass = get_class($accessToken);
109
        }
110
        return $this->accessTokenClass;
111
    }
112
113
    /**
114
     * @return string classname for selected interface
115
     * @throws \yii\base\InvalidConfigException
116
     * @since 1.0.0
117
     */
118
    protected function getAuthCodeClass()
119
    {
120
        if ($this->authCodeClass === null) {
121
            $authCode = Yii::createObject('sweelix\oauth2\server\interfaces\AuthCodeModelInterface');
122
            $this->authCodeClass = get_class($authCode);
123
        }
124
        return $this->authCodeClass;
125
    }
126
127
    /**
128
     * @return string classname for selected interface
129
     * @throws \yii\base\InvalidConfigException
130
     * @since 1.0.0
131
     */
132
    protected function getClientClass()
133
    {
134
        if ($this->clientClass === null) {
135
            $client = Yii::createObject('sweelix\oauth2\server\interfaces\ClientModelInterface');
136
            $this->clientClass = get_class($client);
137
        }
138
        return $this->clientClass;
139
    }
140
141
    /**
142
     * @return string classname for selected interface
143
     * @throws \yii\base\InvalidConfigException
144
     * @since 1.0.0
145
     */
146
    protected function getCypherKeyClass()
147
    {
148
        if ($this->cypherKeyClass === null) {
149
            $cypherKey = Yii::createObject('sweelix\oauth2\server\interfaces\CypherKeyModelInterface');
150
            $this->cypherKeyClass = get_class($cypherKey);
151
        }
152
        return $this->cypherKeyClass;
153
    }
154
155
    /**
156
     * @return string classname for selected interface
157
     * @throws \yii\base\InvalidConfigException
158
     * @since 1.0.0
159
     */
160
    protected function getJtiClass()
161
    {
162
        if ($this->jtiClass === null) {
163
            $jti = Yii::createObject('sweelix\oauth2\server\interfaces\JtiModelInterface');
164
            $this->jtiClass = get_class($jti);
165
        }
166
        return $this->jtiClass;
167
    }
168
169
    /**
170
     * @return string classname for selected interface
171
     * @throws \yii\base\InvalidConfigException
172
     * @since 1.0.0
173
     */
174
    protected function getJwtClass()
175
    {
176
        if ($this->jwtClass === null) {
177
            $jwt = Yii::createObject('sweelix\oauth2\server\interfaces\JwtModelInterface');
178
            $this->jwtClass = get_class($jwt);
179
        }
180
        return $this->jwtClass;
181
    }
182
183
    /**
184
     * @return string classname for selected interface
185
     * @throws \yii\base\InvalidConfigException
186
     * @since 1.0.0
187
     */
188
    protected function getRefreshTokenClass()
189
    {
190
        if ($this->refreshTokenClass === null) {
191
            $refreshToken = Yii::createObject('sweelix\oauth2\server\interfaces\RefreshTokenModelInterface');
192
            $this->refreshTokenClass = get_class($refreshToken);
193
        }
194
        return $this->refreshTokenClass;
195
    }
196
197
    /**
198
     * @return string classname for selected interface
199
     * @throws \yii\base\InvalidConfigException
200
     * @since 1.0.0
201
     */
202
    public function getScopeClass()
203
    {
204
        if ($this->scopeClass === null) {
205
            $scope = Yii::createObject('sweelix\oauth2\server\interfaces\ScopeModelInterface');
206
            $this->scopeClass = get_class($scope);
207
        }
208
        return $this->scopeClass;
209
    }
210
211
    /**
212
     * @return string classname for selected interface
213
     * @throws \yii\base\InvalidConfigException
214
     * @since 1.0.0
215
     */
216
    public function getUserClass()
217
    {
218
        if ($this->userClass === null) {
219
            $scope = Yii::createObject('sweelix\oauth2\server\interfaces\UserModelInterface');
220
            $this->userClass = get_class($scope);
221
        }
222
        return $this->userClass;
223
    }
224
225
    /**
226
     * @inheritdoc
227
     */
228
    public function getAccessToken($oauth_token)
229
    {
230
        $accessToken = null;
231
        $accessTokenClass = $this->getAccessTokenClass();
232
        if (preg_match($accessTokenClass::JWT_REGEX, $oauth_token)) {
233
            $jwt = new Jwt();
234
            $decodedJwt = $jwt->decode($oauth_token, null, false);
235
            $key = $this->getPublicKey($decodedJwt['aud']);
236
            if (($key !== null) && ($decodedJwt = $jwt->decode($oauth_token, $key, true))) {
237
                $accessToken = ArrayHelper::merge([
238
                    'expires' => $decodedJwt['exp'],
239
                    'client_id' => $decodedJwt['aud'],
240
                    'user_id' => $decodedJwt['sub'],
241
                    'scope' => $decodedJwt['scope'],
242
                    'id_token' => $decodedJwt['jti'],
243
                ], $decodedJwt);
244
            }
245
        } else {
246
            $accessToken = $accessTokenClass::findOne($oauth_token);
247
            /* @var \sweelix\oauth2\server\interfaces\AccessTokenModelInterface $accessToken */
248
            if ($accessToken !== null) {
249
                $finalToken = [
250
                    'expires' => $accessToken->expiry,
0 ignored issues
show
Bug introduced by
Accessing expiry on the interface sweelix\oauth2\server\in...cessTokenModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
251
                    'client_id' => $accessToken->clientId,
0 ignored issues
show
Bug introduced by
Accessing clientId on the interface sweelix\oauth2\server\in...cessTokenModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
252
                    'user_id' => $accessToken->userId,
0 ignored issues
show
Bug introduced by
Accessing userId on the interface sweelix\oauth2\server\in...cessTokenModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
253
                    'scope' => implode(' ', $accessToken->scopes),
0 ignored issues
show
Bug introduced by
Accessing scopes on the interface sweelix\oauth2\server\in...cessTokenModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
254
                    'id_token' => $accessToken->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface sweelix\oauth2\server\in...cessTokenModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
255
                ];
256
                $accessToken = $finalToken;
257
            }
258
        }
259
        return $accessToken;
260
    }
261
262
    /**
263
     * @inheritdoc
264
     */
265
    public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null)
266
    {
267
        $response = false;
268
        if ($expires > time()) {
269
            $accessTokenClass = $this->getAccessTokenClass();
270
            if (preg_match($accessTokenClass::JWT_REGEX, $oauth_token)) {
271
                $response = true;
272
            } else {
273
                $accessToken = Yii::createObject('sweelix\oauth2\server\interfaces\AccessTokenModelInterface');
274
                /* @var \sweelix\oauth2\server\interfaces\AccessTokenModelInterface $accessToken */
275
                $accessToken->id = $oauth_token;
0 ignored issues
show
Bug introduced by
Accessing id on the interface sweelix\oauth2\server\in...cessTokenModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
276
                $accessToken->clientId = $client_id;
0 ignored issues
show
Bug introduced by
Accessing clientId on the interface sweelix\oauth2\server\in...cessTokenModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
277
                $accessToken->userId = $user_id;
0 ignored issues
show
Bug introduced by
Accessing userId on the interface sweelix\oauth2\server\in...cessTokenModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
278
                $accessToken->expiry = $expires;
0 ignored issues
show
Bug introduced by
Accessing expiry on the interface sweelix\oauth2\server\in...cessTokenModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
279
                if ($scope === null) {
280
                    $scopes = [];
281
                } else {
282
                    $scopes = explode(' ', $scope);
283
                }
284
                $accessToken->scopes = $scopes;
0 ignored issues
show
Bug introduced by
Accessing scopes on the interface sweelix\oauth2\server\in...cessTokenModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
285
                $response = $accessToken->save();
286
            }
287
        }
288
        return $response;
289
    }
290
291
    /**
292
     * @inheritdoc
293
     */
294
    public function unsetAccessToken($access_token)
295
    {
296
        $accessTokenClass = $this->getAccessTokenClass();
297
        $accessToken = $accessTokenClass::findOne($access_token);
298
        /* @var \sweelix\oauth2\server\interfaces\AccessTokenModelInterface $accessToken */
299
        if ($accessToken !== null) {
300
            return $accessToken->delete();
301
        }
302
        return true;
303
    }
304
305
    /**
306
     * @inheritdoc
307
     */
308
    public function getAuthorizationCode($code)
309
    {
310
        $authCodeClass = $this->getAuthCodeClass();
311
        $authCode = $authCodeClass::findOne($code);
312
        if ($authCode !== null) {
313
            $finalCode = [
314
                'client_id' => $authCode->clientId,
315
                'user_id' => $authCode->userId,
316
                'expires' => $authCode->expiry,
317
                'redirect_uri' => $authCode->redirectUri,
318
                'scope' => implode(' ', $authCode->scopes),
319
                'id_token' => $authCode->tokenId,
320
            ];
321
            $authCode = $finalCode;
322
        }
323
        return $authCode;
324
    }
325
326
    /**
327
     * @inheritdoc
328
     */
329
    public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null, $id_token = null)
330
    {
331
        $response = false;
332
        if ($expires > time()) {
333
            $authCode = Yii::createObject('sweelix\oauth2\server\interfaces\AuthCodeModelInterface');
334
            $authCode->id = $code;
335
            $authCode->clientId = $client_id;
336
            $authCode->userId = $user_id;
337
            $authCode->redirectUri = $redirect_uri;
338
            $authCode->expiry = $expires;
339
            $authCode->tokenId = $id_token;
340
            if ($scope === null) {
341
                $scopes = [];
342
            } else {
343
                $scopes = explode(' ', $scope);
344
            }
345
            $authCode->scopes = $scopes;
346
            $response = $authCode->save();
347
        }
348
        return $response;
349
    }
350
351
    /**
352
     * @inheritdoc
353
     */
354
    public function expireAuthorizationCode($code)
355
    {
356
        $authCodeClass = $this->getAuthCodeClass();
357
        $authCode = $authCodeClass::findOne($code);
358
        if ($authCode !== null) {
359
            return $authCode->delete();
360
        }
361
        return true;
362
    }
363
364
    /**
365
     * @inheritdoc
366
     */
367
    public function getClientDetails($client_id)
368
    {
369
        $clientClass = $this->getClientClass();
370
        $client = $clientClass::findOne($client_id);
371
        if ($client !== null) {
372
            $finalClient = [
373
                'redirect_uri' => is_array($client->redirectUri) ? implode(' ', $client->redirectUri) : $client->redirectUri,
374
                'client_id' => $client->id,
375
                'grant_types' => $client->grantTypes,
376
                'user_id' => $client->userId,
377
                'scope' => implode(' ', $client->scopes),
378
            ];
379
            $client = $finalClient;
380
        }
381
        return ($client !== null) ? $client : false;
382
    }
383
384
    /**
385
     * @inheritdoc
386
     */
387
    public function getClientScope($client_id)
388
    {
389
        $clientClass = $this->getClientClass();
390
        $client = $clientClass::findOne($client_id);
391
        $scopes = '';
392
        if ($client !== null) {
393
            $scopes = implode(' ', $client->scopes);
394
        }
395
        return $scopes;
396
    }
397
398
    /**
399
     * @inheritdoc
400
     */
401
    public function checkRestrictedGrantType($client_id, $grant_type)
402
    {
403
        $clientClass = $this->getClientClass();
404
        $client = $clientClass::findOne($client_id);
405
        $notRestricted = true;
406
        if ($client !== null) {
407
            if (empty($client->grantTypes) === false) {
408
                $notRestricted = in_array($grant_type, $client->grantTypes);
409
            }
410
        }
411
        return $notRestricted;
412
    }
413
414
    /**
415
     * @inheritdoc
416
     */
417
    public function checkClientCredentials($client_id, $client_secret = null)
418
    {
419
        $clientClass = $this->getClientClass();
420
        $client = $clientClass::findOne($client_id);
421
        return ($client !== null) ? ($client->secret === $client_secret) : false;
422
    }
423
424
    /**
425
     * @inheritdoc
426
     */
427
    public function isPublicClient($client_id)
428
    {
429
        $clientClass = $this->getClientClass();
430
        $client = $clientClass::findOne($client_id);
431
        return ($client !== null) ? $client->isPublic : false;
432
    }
433
434
    /**
435
     * @inheritdoc
436
     */
437
    public function getJti($client_id, $subject, $audience, $expiration, $jti)
438
    {
439
        $jtiClass = $this->getJtiClass();
440
        $jtiModel = $jtiClass::findOne([
441
            'clientId' => $client_id,
442
            'subject' => $subject,
443
            'audience' => $audience,
444
            'expires' => $expiration,
445
            'jti' => $jti,
446
        ]);
447
        if ($jtiModel !== null) {
448
            $finalJti = [
449
                'issuer' => $jtiModel->clientId,
450
                'subject' => $jtiModel->subject,
451
                'audience' => $jtiModel->audience,
452
                'expires' => $jtiModel->expires,
453
                'jti' => $jtiModel->jti,
454
            ];
455
            $jtiModel = $finalJti;
456
        }
457
        return $jtiModel;
458
    }
459
460
    /**
461
     * @inheritdoc
462
     */
463
    public function setJti($client_id, $subject, $audience, $expiration, $jti)
464
    {
465
        $response = false;
466
        if ($expiration > time()) {
467
            $jtiModel = Yii::createObject('sweelix\oauth2\server\interfaces\JtiModelInterface');
468
            /* @var \sweelix\oauth2\server\interfaces\JtiModelInterface $jtiModel */
469
            $jtiModel->clientId = $client_id;
0 ignored issues
show
Bug introduced by
Accessing clientId on the interface sweelix\oauth2\server\interfaces\JtiModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
470
            $jtiModel->subject = $subject;
0 ignored issues
show
Bug introduced by
Accessing subject on the interface sweelix\oauth2\server\interfaces\JtiModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
471
            $jtiModel->audience = $audience;
0 ignored issues
show
Bug introduced by
Accessing audience on the interface sweelix\oauth2\server\interfaces\JtiModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
472
            $jtiModel->expires = $expiration;
0 ignored issues
show
Bug introduced by
Accessing expires on the interface sweelix\oauth2\server\interfaces\JtiModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
473
            $jtiModel->jti = $jti;
0 ignored issues
show
Bug introduced by
Accessing jti on the interface sweelix\oauth2\server\interfaces\JtiModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
474
            $response = $jtiModel->save();
475
        }
476
        return $response;
477
    }
478
479
    /**
480
     * @inheritdoc
481
     */
482
    public function getClientKey($client_id, $subject)
483
    {
484
        $jwtClass = $this->getJwtClass();
485
        $jwt = $jwtClass::findOne([
486
            'clientId' => $client_id,
487
            'subject' => $subject,
488
        ]);
489
        if ($jwt !== null) {
490
            $finalJwt = $jwt->publicKey;
491
            $jwt = $finalJwt;
492
        }
493
        return $jwt;
494
    }
495
496
    /**
497
     * @inheritdoc
498
     */
499
    public function getPublicKey($client_id = null)
500
    {
501
        $cypherKeyClass = $this->getCypherKeyClass();
502
        if ($client_id === null) {
503
            $client_id = $cypherKeyClass::DEFAULT_KEY;
504
        }
505
        $cypherKey = $cypherKeyClass::findOne($client_id);
506
        if ($cypherKey === null) {
507
            $cypherKey = $cypherKeyClass::findOne($cypherKeyClass::DEFAULT_KEY);
508
        }
509
        if ($cypherKey !== null) {
510
            $cypherKey = $cypherKey->publicKey;
511
        }
512
        return $cypherKey;
513
    }
514
515
    /**
516
     * @inheritdoc
517
     */
518
    public function getPrivateKey($client_id = null)
519
    {
520
        $cypherKeyClass = $this->getCypherKeyClass();
521
        if ($client_id === null) {
522
            $client_id = $cypherKeyClass::DEFAULT_KEY;
523
        }
524
        $cypherKey = $cypherKeyClass::findOne($client_id);
525
        if ($cypherKey === null) {
526
            $cypherKey = $cypherKeyClass::findOne($cypherKeyClass::DEFAULT_KEY);
527
        }
528
        if ($cypherKey !== null) {
529
            $cypherKey = $cypherKey->privateKey;
530
        }
531
        return $cypherKey;
532
    }
533
534
    /**
535
     * @inheritdoc
536
     */
537
    public function getEncryptionAlgorithm($client_id = null)
538
    {
539
        $cypherKeyClass = $this->getCypherKeyClass();
540
        if ($client_id === null) {
541
            $client_id = $cypherKeyClass::DEFAULT_KEY;
542
        }
543
        $cypherKey = $cypherKeyClass::findOne($client_id);
544
        if ($cypherKey === null) {
545
            $cypherKey = $cypherKeyClass::findOne($cypherKeyClass::DEFAULT_KEY);
546
        }
547
        if ($cypherKey !== null) {
548
            $cypherKey = $cypherKey->encryptionAlgorithm;
549
        }
550
        return $cypherKey;
551
    }
552
553
    /**
554
     * @inheritdoc
555
     */
556
    public function getRefreshToken($refresh_token)
557
    {
558
        $refreshTokenClass = $this->getRefreshTokenClass();
559
        $refreshToken = $refreshTokenClass::findOne($refresh_token);
560
        if ($refreshToken !== null) {
561
            $finalToken = [
562
                'refresh_token' => $refreshToken->id,
563
                'client_id' => $refreshToken->clientId,
564
                'user_id' => $refreshToken->userId,
565
                'expires' => $refreshToken->expiry,
566
                'scope' => implode(' ', $refreshToken->scopes),
567
            ];
568
            $refreshToken = $finalToken;
569
        }
570
        return $refreshToken;
571
    }
572
573
    /**
574
     * @inheritdoc
575
     */
576
    public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = null)
577
    {
578
        $response = false;
579
        if ($expires > time()) {
580
            $refreshToken = Yii::createObject('sweelix\oauth2\server\interfaces\RefreshTokenModelInterface');
581
            $refreshToken->id = $refresh_token;
582
            $refreshToken->clientId = $client_id;
583
            $refreshToken->userId = $user_id;
584
            $refreshToken->expiry = $expires;
585
            if ($scope === null) {
586
                $scopes = [];
587
            } else {
588
                $scopes = explode(' ', $scope);
589
            }
590
            $refreshToken->scopes = $scopes;
591
            $response = $refreshToken->save();
592
        }
593
        return $response;
594
    }
595
596
    /**
597
     * @inheritdoc
598
     */
599
    public function unsetRefreshToken($refresh_token)
600
    {
601
        $refreshTokenClass = $this->getRefreshTokenClass();
602
        $refreshToken = $refreshTokenClass::findOne($refresh_token);
603
        if ($refreshToken !== null) {
604
            return $refreshToken->delete();
605
        }
606
        return true;
607
    }
608
609
    /**
610
     * @inheritdoc
611
     */
612
    public function scopeExists($scope)
613
    {
614
        $scopeClass = $this->getScopeClass();
615
        $availableScopes = $scopeClass::findAvailableScopeIds();
616
        $requestedScopes = explode(' ', $scope);
617
        $missingScopes = array_diff($requestedScopes, $availableScopes);
618
        return empty($missingScopes);
619
    }
620
621
    /**
622
     * @inheritdoc
623
     */
624
    public function getDefaultScope($client_id = null)
625
    {
626
        $scopeClass = $this->getScopeClass();
627
        $availableDefaultScopes = $scopeClass::findDefaultScopeIds($client_id);
628
        $scope = implode(' ', $availableDefaultScopes);
629
        if (empty($scope) === true) {
630
            $scope = null;
631
        }
632
        return $scope;
633
    }
634
635
    /**
636
     * @inheritdoc
637
     */
638
    public function checkUserCredentials($username, $password)
639
    {
640
        $userClass = $this->getUserClass();
641
        $user = $userClass::findByUsernameAndPassword($username, $password);
642
        return ($user !== null);
643
    }
644
645
    /**
646
     * @inheritdoc
647
     */
648
    public function getUserDetails($username)
649
    {
650
        $userClass = $this->getUserClass();
651
        $user = $userClass::findByUsername($username);
652
        /* @var \sweelix\oauth2\server\interfaces\UserModelInterface $user ) */
653
        $details = false;
654
        if ($user !== null) {
655
            $details = [
656
                'user_id' => $user->getId(),
657
            ];
658
            $restrictedScopes = $user->getRestrictedScopes();
659
            if (($restrictedScopes !== null) && (is_array($restrictedScopes) === true)) {
660
                $details['scope'] = implode(' ', $restrictedScopes);
661
            }
662
        }
663
        return $details;
664
    }
665
666
}
667