Skip to content

Commit f405bf0

Browse files
authored
[11.x] add optional prefix for cache key (#53448)
* add optional prefix for cache key if a user does not decide to use a dedicated cache store for their password resets, there's a risk of collision/overwriting of the cache keys in the default cache store, since we are just using the user's email. this allows the user to set an optional config value to use a prefix on the cache key. * minor formatting
1 parent 37f96f1 commit f405bf0

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/Illuminate/Auth/Passwords/CacheTokenRepository.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public function __construct(
2323
protected HasherContract $hasher,
2424
protected string $hashKey,
2525
protected int $expires = 3600,
26-
protected int $throttle = 60
26+
protected int $throttle = 60,
27+
protected string $prefix = '',
2728
) {
2829
}
2930

@@ -35,13 +36,15 @@ public function __construct(
3536
*/
3637
public function create(CanResetPasswordContract $user)
3738
{
38-
$email = $user->getEmailForPasswordReset();
39-
4039
$this->delete($user);
4140

4241
$token = hash_hmac('sha256', Str::random(40), $this->hashKey);
4342

44-
$this->cache->put($email, [$token, Carbon::now()->format($this->format)], $this->expires);
43+
$this->cache->put(
44+
$this->prefix.$user->getEmailForPasswordReset(),
45+
[$token, Carbon::now()->format($this->format)],
46+
$this->expires,
47+
);
4548

4649
return $token;
4750
}
@@ -55,7 +58,7 @@ public function create(CanResetPasswordContract $user)
5558
*/
5659
public function exists(CanResetPasswordContract $user, #[\SensitiveParameter] $token)
5760
{
58-
[$record, $createdAt] = $this->cache->get($user->getEmailForPasswordReset());
61+
[$record, $createdAt] = $this->cache->get($this->prefix.$user->getEmailForPasswordReset());
5962

6063
return $record
6164
&& ! $this->tokenExpired($createdAt)
@@ -81,7 +84,7 @@ protected function tokenExpired($createdAt)
8184
*/
8285
public function recentlyCreatedToken(CanResetPasswordContract $user)
8386
{
84-
[$record, $createdAt] = $this->cache->get($user->getEmailForPasswordReset());
87+
[$record, $createdAt] = $this->cache->get($this->prefix.$user->getEmailForPasswordReset());
8588

8689
return $record && $this->tokenRecentlyCreated($createdAt);
8790
}
@@ -111,7 +114,7 @@ protected function tokenRecentlyCreated($createdAt)
111114
*/
112115
public function delete(CanResetPasswordContract $user)
113116
{
114-
$this->cache->forget($user->getEmailForPasswordReset());
117+
$this->cache->forget($this->prefix.$user->getEmailForPasswordReset());
115118
}
116119

117120
/**

src/Illuminate/Auth/Passwords/PasswordBrokerManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ protected function createTokenRepository(array $config)
9494
$this->app['hash'],
9595
$key,
9696
($config['expire'] ?? 60) * 60,
97-
$config['throttle'] ?? 0
97+
$config['throttle'] ?? 0,
98+
$config['prefix'] ?? '',
9899
);
99100
}
100101

0 commit comments

Comments
 (0)