|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yrc\models\redis; |
|
4
|
|
|
|
|
5
|
|
|
use Base32\Base32; |
|
6
|
|
|
use yrc\models\TokenKeyPair; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstract class for generating and storing tokens |
|
11
|
|
|
* @class Token |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Token extends \yrc\redis\ActiveRecord |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* This is our default token lifespan |
|
17
|
|
|
* @const TOKEN_EXPIRATION_TIME |
|
18
|
|
|
*/ |
|
19
|
|
|
const TOKEN_EXPIRATION_TIME = '+15 minutes'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritdoc |
|
23
|
|
|
*/ |
|
24
|
|
|
public function attributes() |
|
25
|
|
|
{ |
|
26
|
|
|
return [ |
|
27
|
|
|
'id', |
|
28
|
|
|
'user_id', |
|
29
|
|
|
'access_token', |
|
30
|
|
|
'refresh_token', |
|
31
|
|
|
'ikm', |
|
32
|
|
|
'secret_sign_kp', |
|
33
|
|
|
'expires_at' |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return sodium_crypto_sign_keypair |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getSignKeyPair() |
|
41
|
|
|
{ |
|
42
|
|
|
$secret = \base64_decode($this->secret_sign_kp); |
|
43
|
|
|
$public = sodium_crypto_sign_publickey_from_secretkey($secret); |
|
44
|
|
|
return sodium_crypto_sign_keypair_from_secretkey_and_publickey($secret, $public); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return sodium_crypto_sign_publickey |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getSignPublicKey() |
|
51
|
|
|
{ |
|
52
|
|
|
return sodium_crypto_sign_publickey($this->getSignKeyPair()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Generates a new auth and refresh token pair |
|
57
|
|
|
* @param int $userId |
|
58
|
|
|
* @param bool $pubkey |
|
|
|
|
|
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function generate($userId = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$model = null; |
|
|
|
|
|
|
64
|
|
|
$signKp = sodium_crypto_sign_keypair(); |
|
65
|
|
|
|
|
66
|
|
|
$user = Yii::$app->yrc->userClass::findOne(['id' => $userId]); |
|
67
|
|
|
if ($user === null) { |
|
68
|
|
|
throw new \yii\base\Exception('Invalid user'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$token = new static; |
|
72
|
|
|
$token->user_id = $userId; |
|
73
|
|
|
$token->access_token = \str_replace('=', '', Base32::encode(\random_bytes(32))); |
|
74
|
|
|
$token->refresh_token = \str_replace('=', '', Base32::encode(\random_bytes(32))); |
|
75
|
|
|
$token->ikm = \base64_encode(\random_bytes(32)); |
|
76
|
|
|
$token->secret_sign_kp = \base64_encode(sodium_crypto_sign_secretkey($signKp)); |
|
77
|
|
|
$token->expires_at = \strtotime(static::TOKEN_EXPIRATION_TIME); |
|
78
|
|
|
|
|
79
|
|
|
if ($token->save()) { |
|
80
|
|
|
return $token; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
throw new \yii\base\Exception(Yii::t('yrc', 'Token failed to save')); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Helper method to get the auth response data |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getAuthResponse() |
|
91
|
|
|
{ |
|
92
|
|
|
$attributes = $this->getAttributes(); |
|
93
|
|
|
unset($attributes['id']); |
|
94
|
|
|
|
|
95
|
|
|
$attributes['signing'] = \base64_encode($this->getSignPublicKey()); |
|
96
|
|
|
unset($attributes['secret_sign_kp']); |
|
97
|
|
|
return $attributes; |
|
98
|
|
|
} |
|
99
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.