|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Erdemkeren\TemporaryAccess; |
|
4
|
|
|
|
|
5
|
|
|
use stdClass; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use Illuminate\Database\ConnectionInterface as ConnectionContract; |
|
8
|
|
|
use Erdemkeren\TemporaryAccess\Contracts\AccessTokenRepository as AccessTokenRepositoryContract; |
|
9
|
|
|
|
|
10
|
|
|
final class DatabaseAccessTokenRepository implements AccessTokenRepositoryContract |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The connection. |
|
14
|
|
|
* |
|
15
|
|
|
* @var ConnectionContract |
|
16
|
|
|
*/ |
|
17
|
|
|
private $connection; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The temporary access token table. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $table; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The number of seconds a token should last. |
|
28
|
|
|
* |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
private $expires; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* DatabaseAccessTokenRepository constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param ConnectionContract $connection |
|
37
|
|
|
* @param string $table |
|
38
|
|
|
* @param int $expires |
|
39
|
|
|
*/ |
|
40
|
11 |
|
public function __construct(ConnectionContract $connection, $table, $expires) |
|
41
|
|
|
{ |
|
42
|
11 |
|
$this->table = $table; |
|
43
|
11 |
|
$this->expires = $expires; |
|
44
|
11 |
|
$this->connection = $connection; |
|
45
|
11 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Retrieve an access token from the storage. |
|
49
|
|
|
* |
|
50
|
|
|
* @param int $authenticatableId The unique identifier of the authenticatable who owns the access token. |
|
51
|
|
|
* @param string $token The access code of the authenticatable. |
|
52
|
|
|
* |
|
53
|
|
|
* @return stdClass|array|bool |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function retrieve($authenticatableId, $token) |
|
56
|
|
|
{ |
|
57
|
2 |
|
$token = $this->getTable()->where('authenticatable_id', $authenticatableId)->where('token', $token)->first(); |
|
58
|
|
|
|
|
59
|
2 |
|
if ($token && ! $this->tokenExpired((object) $token)) { |
|
60
|
1 |
|
return $token; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Retrieve the first resource by the given attributes. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $queryParams The key - value pairs to match. |
|
70
|
|
|
* @param array $attributes The attributes to be returned from the storage. |
|
71
|
|
|
* |
|
72
|
|
|
* @return stdClass|array|null |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function retrieveByAttributes(array $queryParams, array $attributes = ['*']) |
|
75
|
|
|
{ |
|
76
|
3 |
|
$query = $this->getTable(); |
|
77
|
|
|
|
|
78
|
3 |
|
foreach ($queryParams as $column => $value) { |
|
79
|
3 |
|
$query = $query->where($column, $value); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
$resource = $query->first($attributes); |
|
83
|
|
|
|
|
84
|
3 |
|
if (! $resource || $this->tokenExpired($resource)) { |
|
|
|
|
|
|
85
|
2 |
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
return $resource; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Store a new access token in the storage. |
|
93
|
|
|
* |
|
94
|
|
|
* @param int $authenticatableId The unique identifier of the authenticatable who owns the access token. |
|
95
|
|
|
* @param string $token The access code generated for the authenticatable. |
|
96
|
|
|
* @param string|null $expiresAt The expiration date of the access token. |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function store($authenticatableId, $token, $expiresAt = null) |
|
101
|
|
|
{ |
|
102
|
2 |
|
$payload = $this->getAccessTokenPayload($authenticatableId, $token, $expiresAt); |
|
103
|
|
|
|
|
104
|
2 |
|
$id = $this->getTable()->insertGetId($payload); |
|
105
|
2 |
|
$payload['id'] = $id; |
|
106
|
|
|
|
|
107
|
2 |
|
return $payload; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Update the expire date of the given access token in the storage. |
|
112
|
|
|
* |
|
113
|
|
|
* @param int $authenticatableId The unique identifier of the authenticatable. |
|
114
|
|
|
* @param string $token The encrypted access code to be updated. |
|
115
|
|
|
* @param string $expires The new expiration date of the access token. |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function update($authenticatableId, $token, $expires) |
|
120
|
|
|
{ |
|
121
|
1 |
|
return (bool) $this->getTable()->where('authenticatable_id', $authenticatableId)->where('token', $token)->update([ |
|
122
|
1 |
|
'expires_at' => (string) $expires, |
|
123
|
|
|
]); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Delete a resource from the storage. |
|
128
|
|
|
* |
|
129
|
|
|
* @param int $authenticatableId The unique identifier of the authenticatable. |
|
130
|
|
|
* @param string $token The code of the authenticatable. |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function delete($authenticatableId, $token) |
|
135
|
|
|
{ |
|
136
|
1 |
|
return (bool) $this->getTable()->where('authenticatable_id', $authenticatableId)->where('token', $token)->delete(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Delete expired access tokens from the storage. |
|
141
|
|
|
* |
|
142
|
|
|
* @return void |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function deleteExpired() |
|
145
|
|
|
{ |
|
146
|
1 |
|
$this->getTable()->where('expires_at', '<=', (string) $this->getNow())->delete(); |
|
147
|
1 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get an access token payload. |
|
151
|
|
|
* |
|
152
|
|
|
* @param int $authenticatableId |
|
153
|
|
|
* @param string $token |
|
154
|
|
|
* @param string|null $expiresAt |
|
155
|
|
|
* |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
2 |
|
private function getAccessTokenPayload($authenticatableId, $token, $expiresAt) |
|
159
|
|
|
{ |
|
160
|
2 |
|
$expiresAt = $expiresAt ? $expiresAt : $this->getNow()->addMinutes($this->expires); |
|
161
|
|
|
|
|
162
|
|
|
$payload = [ |
|
163
|
2 |
|
'authenticatable_id' => $authenticatableId, |
|
164
|
2 |
|
'token' => $token, |
|
165
|
2 |
|
'created_at' => (string) $this->getNow(), |
|
166
|
2 |
|
'expires_at' => (string) $expiresAt, |
|
167
|
|
|
]; |
|
168
|
|
|
|
|
169
|
2 |
|
return $payload; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Determine if the token has expired. |
|
174
|
|
|
* |
|
175
|
|
|
* @param stdClass $token |
|
176
|
|
|
* |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
4 |
|
private function tokenExpired(stdClass $token) |
|
180
|
|
|
{ |
|
181
|
4 |
|
$expiresAt = $token->expires_at ? new Carbon($token->expires_at) : $this->getNow()->addMinutes($this->expires); |
|
182
|
|
|
|
|
183
|
4 |
|
return $this->getNow()->gte($expiresAt); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get the current UNIX timestamp. |
|
188
|
|
|
* |
|
189
|
|
|
* @return Carbon |
|
190
|
|
|
*/ |
|
191
|
7 |
|
private function getNow() |
|
192
|
|
|
{ |
|
193
|
7 |
|
return Carbon::now(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Begin a new database query against the table. |
|
198
|
|
|
* |
|
199
|
|
|
* @return \Illuminate\Database\Query\Builder |
|
200
|
|
|
*/ |
|
201
|
10 |
|
private function getTable() |
|
202
|
|
|
{ |
|
203
|
10 |
|
return $this->getConnection()->table($this->table); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the database connection instance. |
|
208
|
|
|
* |
|
209
|
|
|
* @return ConnectionContract |
|
210
|
|
|
*/ |
|
211
|
10 |
|
private function getConnection() |
|
212
|
|
|
{ |
|
213
|
10 |
|
return $this->connection; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.