1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yrc\api\models; |
4
|
|
|
|
5
|
|
|
use Base32\Base32; |
6
|
|
|
use yrc\api\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
|
|
|
'crypt_id', |
33
|
|
|
'expires_at' |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns the token key pair object |
39
|
|
|
* @return TokenKeyPair |
40
|
|
|
*/ |
41
|
|
|
public function getCryptToken() |
42
|
|
|
{ |
43
|
|
|
return TokenKeyPair::find()->where(['id' => $this->crypt_id])->one(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Generates a new auth and refresh token pair |
48
|
|
|
* @param int $userId |
49
|
|
|
* @param bool $pubkey |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public static function generate($userId = null, $pubkey = null) |
53
|
|
|
{ |
54
|
|
|
$model = null; |
|
|
|
|
55
|
|
|
$user = Yii::$app->yrc->userClass::findOne(['id' => $userId]); |
56
|
|
|
if ($user === null) { |
57
|
|
|
throw new \yii\base\Exception('Invalid user'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$token = new static; |
61
|
|
|
$token->user_id = $userId; |
62
|
|
|
$token->access_token = \str_replace('=', '', Base32::encode(\random_bytes(32))); |
63
|
|
|
$token->refresh_token = \str_replace('=', '', Base32::encode(\random_bytes(32))); |
64
|
|
|
$token->ikm = \base64_encode(\random_bytes(32)); |
65
|
|
|
$token->expires_at = \strtotime(static::TOKEN_EXPIRATION_TIME); |
66
|
|
|
|
67
|
|
|
if ($pubkey !== null) { |
68
|
|
|
$model = TokenKeyPair::generate(TokenKeyPair::DEFAULT_TYPE, $pubkey); |
69
|
|
|
$token->crypt_id = $model->id; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($token->save()) { |
73
|
|
|
return $token; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
throw new \yii\base\Exception(Yii::t('yrc', 'Token failed to save')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Helper method to get the auth response data |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function getAuthResponse() |
84
|
|
|
{ |
85
|
|
|
$attributes = $this->getAttributes(); |
86
|
|
|
$model = $this->getCryptToken(); |
87
|
|
|
|
88
|
|
|
if ($model !== null) { |
89
|
|
|
$attributes['crypt'] = [ |
90
|
|
|
'public' => \base64_encode($model->getBoxPublicKey()), |
91
|
|
|
'signing' => \base64_encode($model->getSignPublicKey()), |
92
|
|
|
'signature' => \base64_encode(\Sodium\crypto_sign( |
93
|
|
|
$model->getBoxPublicKey(), |
94
|
|
|
\base64_decode($model->secret_sign_kp) |
95
|
|
|
)), |
96
|
|
|
'hash' => $model->hash |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $attributes; |
101
|
|
|
} |
102
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.