|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yrc\models\redis; |
|
4
|
|
|
|
|
5
|
|
|
use Base32\Base32; |
|
|
|
|
|
|
6
|
|
|
use yrc\models\TokenKeyPair; |
|
|
|
|
|
|
7
|
|
|
use yrc\redis\ActiveRecord; |
|
8
|
|
|
use Yii; |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return sodium_crypto_sign_publickey |
|
|
|
|
|
|
56
|
|
|
*/ |
|
57
|
|
|
public function getSignPublicKey() |
|
58
|
|
|
{ |
|
59
|
|
|
return sodium_crypto_sign_publickey($this->getSignKeyPair()); |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
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)); |
|
|
|
|
|
|
83
|
|
|
$token->expires_at = \strtotime(static::TOKEN_EXPIRATION_TIME); |
|
84
|
|
|
|
|
85
|
|
|
if ($token->save()) { |
|
86
|
|
|
return $token; |
|
|
|
|
|
|
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
|
|
|
} |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths