1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: hugh.li |
5
|
|
|
* Date: 2022/6/14 |
6
|
|
|
* Time: 17:41 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace HughCube\Laravel\OTS\Sanctum; |
10
|
|
|
|
11
|
|
|
use Aliyun\OTS\Consts\ColumnTypeConst; |
12
|
|
|
use Aliyun\OTS\Consts\RowExistenceExpectationConst; |
13
|
|
|
use Aliyun\OTS\OTSClientException; |
14
|
|
|
use Aliyun\OTS\OTSServerException; |
15
|
|
|
use Carbon\Carbon; |
16
|
|
|
use Exception; |
17
|
|
|
use HughCube\Base\Base; |
18
|
|
|
use HughCube\Laravel\OTS\Connection; |
19
|
|
|
use HughCube\Laravel\OTS\Ots; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @property string $tokenable_type |
23
|
|
|
* @property int $tokenable_id |
24
|
|
|
* @property string $name |
25
|
|
|
* @property string $token |
26
|
|
|
* @property array $abilities |
27
|
|
|
* @property Carbon|null $last_used_at |
28
|
|
|
* @property Carbon|null $created_at |
29
|
|
|
* @property Carbon|null $updated_at |
30
|
|
|
*/ |
31
|
|
|
class PersonalAccessToken extends \Laravel\Sanctum\PersonalAccessToken |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @throws Exception |
35
|
|
|
*/ |
36
|
|
|
protected static function getOts(): Connection |
37
|
|
|
{ |
38
|
|
|
return Ots::connection(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function getOtsTable(): string |
42
|
|
|
{ |
43
|
|
|
return 'personal_access_tokens'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function getApp(): string |
47
|
|
|
{ |
48
|
|
|
return config('app.name'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* 最后一次使用时间在15天之前 |
53
|
|
|
* |
54
|
|
|
* @param bool $isValid 验证的当前值 |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function isValidAccessToken(bool $isValid = true): bool |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
if (!$this->last_used_at instanceof Carbon) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->last_used_at->gt(Carbon::now()->subSeconds(3600 * 24 * 15)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws OTSServerException |
68
|
|
|
* @throws OTSClientException |
69
|
|
|
* @throws Exception |
70
|
|
|
*/ |
71
|
|
|
public static function findToken($token): ?PersonalAccessToken |
72
|
|
|
{ |
73
|
|
|
$request = [ |
74
|
|
|
'table_name' => static::getOtsTable(), |
75
|
|
|
'primary_key' => [ |
76
|
|
|
['token', hash('sha256', $token)], |
77
|
|
|
['app', static::getApp()] |
78
|
|
|
], |
79
|
|
|
'max_versions' => 1 |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$row = Ots::parseRow(static::getOts()->getRow($request)); |
|
|
|
|
83
|
|
|
|
84
|
|
|
/** @var static $model */ |
85
|
|
|
$model = static::query()->newModelInstance(); |
86
|
|
|
$model->forceFill($row); |
87
|
|
|
$model->exists = true; |
88
|
|
|
|
89
|
|
|
if (is_string($model->abilities)) { |
|
|
|
|
90
|
|
|
$abilities = json_decode($model->abilities, true); |
91
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
92
|
|
|
$abilities = []; |
93
|
|
|
} |
94
|
|
|
$model->abilities = $abilities; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $model; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @throws OTSServerException |
102
|
|
|
* @throws OTSClientException |
103
|
|
|
* @throws Exception |
104
|
|
|
*/ |
105
|
|
|
public function save(array $options = []): bool |
106
|
|
|
{ |
107
|
|
|
$this->created_at = $this->created_at ?? Carbon::now(); |
108
|
|
|
$this->updated_at = $this->updated_at ?? Carbon::now(); |
109
|
|
|
|
110
|
|
|
if (!$this->exists) { |
111
|
|
|
return $this->saveInstall(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->updated_at = Carbon::now(); |
115
|
|
|
$request = [ |
116
|
|
|
'table_name' => $this->table, |
117
|
|
|
'condition' => RowExistenceExpectationConst::CONST_EXPECT_EXIST, |
118
|
|
|
'primary_key' => [ |
119
|
|
|
['token', $this->token], |
120
|
|
|
['app', $this->getApp()] |
121
|
|
|
], |
122
|
|
|
'update_of_attribute_columns' => [ |
123
|
|
|
'PUT' => [ |
124
|
|
|
['tokenable_type', $this->tokenable_type, ColumnTypeConst::CONST_STRING], |
125
|
|
|
['tokenable_id', Base::digitalToString($this->tokenable_id), ColumnTypeConst::CONST_STRING], |
126
|
|
|
['name', $this->name, ColumnTypeConst::CONST_STRING], |
127
|
|
|
['abilities', json_encode($this->abilities), ColumnTypeConst::CONST_STRING], |
128
|
|
|
['last_used_at', $this->last_used_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
|
|
|
|
129
|
|
|
['created_at', $this->created_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
130
|
|
|
['updated_at', $this->updated_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
131
|
|
|
] |
132
|
|
|
], |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
static::getOts()->updateRow($request); |
|
|
|
|
136
|
|
|
|
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @throws OTSServerException |
142
|
|
|
* @throws OTSClientException |
143
|
|
|
* @throws Exception |
144
|
|
|
*/ |
145
|
|
|
protected function saveInstall(): bool |
146
|
|
|
{ |
147
|
|
|
$this->last_used_at = $this->last_used_at ?? Carbon::now(); |
148
|
|
|
|
149
|
|
|
$request = [ |
150
|
|
|
'table_name' => $this->table, |
151
|
|
|
'condition' => RowExistenceExpectationConst::CONST_EXPECT_NOT_EXIST, |
152
|
|
|
'primary_key' => [ |
153
|
|
|
['token', $this->token], |
154
|
|
|
['app', $this->getApp()] |
155
|
|
|
], |
156
|
|
|
'attribute_columns' => [ |
157
|
|
|
['tokenable_type', $this->tokenable_type, ColumnTypeConst::CONST_STRING], |
158
|
|
|
['tokenable_id', Base::digitalToString($this->tokenable_id), ColumnTypeConst::CONST_STRING], |
159
|
|
|
['name', $this->name, ColumnTypeConst::CONST_STRING], |
160
|
|
|
['abilities', json_encode($this->abilities), ColumnTypeConst::CONST_STRING], |
161
|
|
|
['last_used_at', $this->last_used_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
162
|
|
|
['created_at', $this->created_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
|
|
|
|
163
|
|
|
['updated_at', $this->updated_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
|
|
|
|
164
|
|
|
] |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
static::getOts()->putRow($request); |
|
|
|
|
168
|
|
|
$this->exists = true; |
169
|
|
|
|
170
|
|
|
return true; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.