Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] add optional prefix for cache key #53448

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Illuminate/Auth/Passwords/CacheTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function __construct(
protected HasherContract $hasher,
protected string $hashKey,
protected int $expires = 3600,
protected int $throttle = 60
protected int $throttle = 60,
protected string $prefix = '',
) {
}

Expand All @@ -35,13 +36,15 @@ public function __construct(
*/
public function create(CanResetPasswordContract $user)
{
$email = $user->getEmailForPasswordReset();

$this->delete($user);

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

$this->cache->put($email, [$token, Carbon::now()->format($this->format)], $this->expires);
$this->cache->put(
$this->prefix.$user->getEmailForPasswordReset(),
[$token, Carbon::now()->format($this->format)],
$this->expires,
);

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

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

return $record && $this->tokenRecentlyCreated($createdAt);
}
Expand Down Expand Up @@ -111,7 +114,7 @@ protected function tokenRecentlyCreated($createdAt)
*/
public function delete(CanResetPasswordContract $user)
{
$this->cache->forget($user->getEmailForPasswordReset());
$this->cache->forget($this->prefix.$user->getEmailForPasswordReset());
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Auth/Passwords/PasswordBrokerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ protected function createTokenRepository(array $config)
$this->app['hash'],
$key,
($config['expire'] ?? 60) * 60,
$config['throttle'] ?? 0
$config['throttle'] ?? 0,
$config['prefix'] ?? '',
);
}

Expand Down