Passed
Branch master (b11977)
by Charles
02:33
created

Token   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 23 3
A getSignPublicKey() 0 3 1
A getSignKeyPair() 0 5 1
A attributes() 0 10 1
A getAuthResponse() 0 8 1
1
<?php
2
3
namespace yrc\models\redis;
4
5
use Base32\Base32;
0 ignored issues
show
Bug introduced by
The type Base32\Base32 was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use yrc\models\TokenKeyPair;
0 ignored issues
show
Bug introduced by
The type yrc\models\TokenKeyPair was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use yrc\redis\ActiveRecord;
8
use Yii;
0 ignored issues
show
Bug introduced by
The type Yii was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * Abstract class for generating and storing tokens
12
 * @property integer $id
13
 * @property integer $user_id
14
 * @property string $access_token
15
 * @property string $refresh_token
16
 * @property string $ikm
17
 * @property string $secret_sign_key
18
 * @property integer $expires_at
19
 */
20
abstract class Token extends ActiveRecord
21
{
22
    /**
23
     * This is our default token lifespan
24
     * @const TOKEN_EXPIRATION_TIME
25
     */
26
    const TOKEN_EXPIRATION_TIME = '+15 minutes';
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function attributes()
32
    {
33
        return [
34
            'id',
35
            'user_id',
36
            'access_token',
37
            'refresh_token',
38
            'ikm',
39
            'secret_sign_kp',
40
            'expires_at'
41
        ];
42
    }
43
44
    /**
45
     * @return sodium_crypto_sign_keypair
0 ignored issues
show
Bug introduced by
The type yrc\models\redis\sodium_crypto_sign_keypair was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
     */
47
    public function getSignKeyPair()
48
    {
49
        $secret = \base64_decode($this->secret_sign_kp);
50
        $public = sodium_crypto_sign_publickey_from_secretkey($secret);
51
        return sodium_crypto_sign_keypair_from_secretkey_and_publickey($secret, $public);
0 ignored issues
show
Bug Best Practice introduced by
The expression return sodium_crypto_sig...ickey($secret, $public) returns the type string which is incompatible with the documented return type yrc\models\redis\sodium_crypto_sign_keypair.
Loading history...
52
    }
53
54
    /**
55
     * @return sodium_crypto_sign_publickey
0 ignored issues
show
Bug introduced by
The type yrc\models\redis\sodium_crypto_sign_publickey was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
     */
57
    public function getSignPublicKey()
58
    {
59
        return sodium_crypto_sign_publickey($this->getSignKeyPair());
0 ignored issues
show
Bug Best Practice introduced by
The expression return sodium_crypto_sig...this->getSignKeyPair()) returns the type string which is incompatible with the documented return type yrc\models\redis\sodium_crypto_sign_publickey.
Loading history...
60
    }
61
62
    /**
63
     * Generates a new auth and refresh token pair
64
     * @param int $userId
65
     * @return array
66
     */
67
    public static function generate($userId = null)
68
    {
69
        $model = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $model is dead and can be removed.
Loading history...
70
        $signKp = sodium_crypto_sign_keypair();
71
72
        $user = Yii::$app->yrc->userClass::findOne(['id' => $userId]);
73
        if ($user === null) {
74
            throw new \yii\base\Exception('Invalid user');
0 ignored issues
show
Bug introduced by
The type yii\base\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
75
        }
76
       
77
        $token = new static;
78
        $token->user_id = $userId;
79
        $token->access_token = \str_replace('=', '', Base32::encode(\random_bytes(32)));
80
        $token->refresh_token = \str_replace('=', '', Base32::encode(\random_bytes(32)));
81
        $token->ikm =  \base64_encode(\random_bytes(32));
82
        $token->secret_sign_kp = \base64_encode(sodium_crypto_sign_secretkey($signKp));
0 ignored issues
show
Bug Best Practice introduced by
The property secret_sign_kp does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83
        $token->expires_at = \strtotime(static::TOKEN_EXPIRATION_TIME);
84
85
        if ($token->save()) {
86
            return $token;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $token returns the type yrc\models\redis\Token which is incompatible with the documented return type array.
Loading history...
87
        }
88
            
89
        throw new \yii\base\Exception(Yii::t('yrc', 'Token failed to save'));
90
    }
91
92
    /**
93
     * Helper method to get the auth response data
94
     * @return array
95
     */
96
    public function getAuthResponse()
97
    {
98
        $attributes = $this->getAttributes();
99
        unset($attributes['id']);
100
101
        $attributes['signing'] = \base64_encode($this->getSignPublicKey());
102
        unset($attributes['secret_sign_kp']);
103
        return $attributes;
104
    }
105
}