/
DatabaseTokenRepository.php
247 lines (218 loc) · 5.94 KB
/
DatabaseTokenRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
namespace Yaquawa\Laravel\TokenRepository;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Yaquawa\Laravel\TokenRepository\Contracts\TokenRepository;
use Illuminate\Database\Query\Builder;
class DatabaseTokenRepository implements TokenRepository
{
/**
* The database connection instance.
*
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/**
* The Hasher implementation.
*
* @var \Illuminate\Contracts\Hashing\Hasher
*/
protected $hasher;
/**
* The token database table.
*
* @var string
*/
protected $table;
/**
* The hashing key.
*
* @var string
*/
protected $hashKey;
/**
* The number of seconds a token should last.
*
* @var int
*/
protected $expires;
/**
* The filter function called before "INSERT INTO" operation.
* The returned value should be an array representing a row.
*
* @var callable
*/
protected $payloadFilter;
/**
* Create a new token repository instance.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
* @param string $table
* @param string $hashKey
* @param int $expires
*
* @param callable|null $payloadFilter
*/
public function __construct(
ConnectionInterface $connection,
HasherContract $hasher,
string $table,
string $hashKey,
int $expires = 60,
callable $payloadFilter = null
) {
$this->table = $table;
$this->hasher = $hasher;
$this->hashKey = $hashKey;
$this->expires = $expires * 60;
$this->connection = $connection;
$this->payloadFilter = $payloadFilter;
}
/**
* Create a new token record.
*
* @param Authenticatable $user
*
* @return string
*/
public function create(Authenticatable $user): string
{
$this->deleteExisting($user);
// We will create a new, random token for the user so that we can e-mail them
// a safe link to the password reset form. Then we will insert a record in
// the database so that we can verify the token within the actual reset.
$token = $this->createNewToken();
$this->getTable()->insert($this->getPayload($user, $token));
return $token;
}
/**
* Delete all existing reset tokens from the database.
*
* @param Authenticatable $user
*
* @return int
*/
protected function deleteExisting(Authenticatable $user)
{
return $this->getTable()->where('user_id', $user->getAuthIdentifier())->delete();
}
/**
* Build the record payload for the table.
*
* @param Authenticatable $user
* @param string $token
*
* @return array
*/
protected function getPayload(Authenticatable $user, string $token): array
{
$payLoad = ['user_id' => $user->getAuthIdentifier(), 'token' => $this->hasher->make($token), 'created_at' => new Carbon];
if ($this->payloadFilter) {
$payLoad = \call_user_func($this->payloadFilter, $payLoad, $user);
}
return $payLoad;
}
/**
* Get the record by the given user's id
* and make sure the token is not expired.
*
* @param Authenticatable $user
* @param string $token
*
* @return array|null
*/
public function find(Authenticatable $user, string $token): ?array
{
$record = (array)$this->getTable()->where(
'user_id', $user->getAuthIdentifier()
)->first();
$exists = $record &&
! $this->tokenExpired($record['created_at']) &&
$this->hasher->check($token, $record['token']);
return $exists ? $record : null;
}
/**
* Determine if a token record exists and is valid.
*
* @param Authenticatable $user
* @param string $token
*
* @return bool
*/
public function exists(Authenticatable $user, $token): bool
{
return (bool)$this->find($user, $token);
}
/**
* Determine if the token has expired.
*
* @param string $createdAt
*
* @return bool
*/
protected function tokenExpired($createdAt): bool
{
return Carbon::parse($createdAt)->addSeconds($this->expires)->isPast();
}
/**
* Delete a token record by user.
*
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
*
* @return void
*/
public function delete(Authenticatable $user)
{
$this->deleteExisting($user);
}
/**
* Delete expired tokens.
*
* @return void
*/
public function deleteExpired()
{
$expiredAt = Carbon::now()->subSeconds($this->expires);
$this->getTable()->where('created_at', '<', $expiredAt)->delete();
}
/**
* Create a new token for the user.
*
* @return string
*/
public function createNewToken(): string
{
return hash_hmac('sha256', Str::random(40), $this->hashKey);
}
/**
* Get the database connection instance.
*
* @return \Illuminate\Database\ConnectionInterface
*/
public function getConnection(): ConnectionInterface
{
return $this->connection;
}
/**
* Begin a new database query against the table.
*
* @return \Illuminate\Database\Query\Builder
*/
protected function getTable(): Builder
{
return $this->connection->table($this->table);
}
/**
* Get the hasher instance.
*
* @return \Illuminate\Contracts\Hashing\Hasher
*/
public function getHasher(): HasherContract
{
return $this->hasher;
}
}