Skip to content

Commit c895495

Browse files
[12.x] remove the "prefix" option for cache password resets (#56127)
* remove the "prefix" option for cache password resets this is mostly a rollback of #53448 per Taylor's request. rather than allowing an optional prefix, we'll use a deterministic hash of the user's email for our cache key. this should make the chance of a collision if no dedicated store is used statistically insignificant. I've also opted to extract out a `makeCacheKey()` method here mainly to reduce the duplicated code and help prevent bugs from divergence. however, this could possibly help userland override the cache key generation. maybe I'm overthinking that, maybe it's honestly not a problem. * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 3c3e0e6 commit c895495

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/Illuminate/Auth/Passwords/CacheTokenRepository.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public function __construct(
2424
protected string $hashKey,
2525
protected int $expires = 3600,
2626
protected int $throttle = 60,
27-
protected string $prefix = '',
2827
) {
2928
}
3029

@@ -41,7 +40,7 @@ public function create(CanResetPasswordContract $user)
4140
$token = hash_hmac('sha256', Str::random(40), $this->hashKey);
4241

4342
$this->cache->put(
44-
$this->prefix.$user->getEmailForPasswordReset(),
43+
$this->cacheKey($user),
4544
[$this->hasher->make($token), Carbon::now()->format($this->format)],
4645
$this->expires,
4746
);
@@ -58,7 +57,7 @@ public function create(CanResetPasswordContract $user)
5857
*/
5958
public function exists(CanResetPasswordContract $user, #[\SensitiveParameter] $token)
6059
{
61-
[$record, $createdAt] = $this->cache->get($this->prefix.$user->getEmailForPasswordReset());
60+
[$record, $createdAt] = $this->cache->get($this->cacheKey($user));
6261

6362
return $record
6463
&& ! $this->tokenExpired($createdAt)
@@ -84,7 +83,7 @@ protected function tokenExpired($createdAt)
8483
*/
8584
public function recentlyCreatedToken(CanResetPasswordContract $user)
8685
{
87-
[$record, $createdAt] = $this->cache->get($this->prefix.$user->getEmailForPasswordReset());
86+
[$record, $createdAt] = $this->cache->get($this->cacheKey($user));
8887

8988
return $record && $this->tokenRecentlyCreated($createdAt);
9089
}
@@ -114,7 +113,7 @@ protected function tokenRecentlyCreated($createdAt)
114113
*/
115114
public function delete(CanResetPasswordContract $user)
116115
{
117-
$this->cache->forget($this->prefix.$user->getEmailForPasswordReset());
116+
$this->cache->forget($this->cacheKey($user));
118117
}
119118

120119
/**
@@ -125,4 +124,15 @@ public function delete(CanResetPasswordContract $user)
125124
public function deleteExpired()
126125
{
127126
}
127+
128+
/**
129+
* Determine the cache key for the given user.
130+
*
131+
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
132+
* @return string
133+
*/
134+
public function cacheKey(CanResetPasswordContract $user): string
135+
{
136+
return hash('sha256', $user->getEmailForPasswordReset());
137+
}
128138
}

src/Illuminate/Auth/Passwords/PasswordBrokerManager.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ protected function createTokenRepository(array $config)
9595
$key,
9696
($config['expire'] ?? 60) * 60,
9797
$config['throttle'] ?? 0,
98-
$config['prefix'] ?? '',
9998
);
10099
}
101100

0 commit comments

Comments
 (0)