Файл: vendor/laravel/framework/src/Illuminate/Auth/Passwords/DatabaseTokenRepository.php
Строк: 236
<?php
namespace IlluminateAuthPasswords;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
use IlluminateContractsHashingHasher as HasherContract;
use IlluminateDatabaseConnectionInterface;
use IlluminateSupportCarbon;
use IlluminateSupportStr;
class DatabaseTokenRepository implements TokenRepositoryInterface
{
/**
* Create a new token repository instance.
*
* @param int $expires The number of seconds a token should remain valid.
* @param int $throttle Minimum number of seconds before the user can generate new password reset tokens.
*/
public function __construct(
protected ConnectionInterface $connection,
protected HasherContract $hasher,
protected string $table,
protected string $hashKey,
protected int $expires = 3600,
protected int $throttle = 60,
) {
}
/**
* Create a new token record.
*
* @param IlluminateContractsAuthCanResetPassword $user
* @return string
*/
public function create(CanResetPasswordContract $user)
{
$email = $user->getEmailForPasswordReset();
$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($email, $token));
return $token;
}
/**
* Delete all existing reset tokens from the database.
*
* @param IlluminateContractsAuthCanResetPassword $user
* @return int
*/
protected function deleteExisting(CanResetPasswordContract $user)
{
return $this->getTable()->where('email', $user->getEmailForPasswordReset())->delete();
}
/**
* Build the record payload for the table.
*
* @param string $email
* @param string $token
* @return array
*/
protected function getPayload($email, #[SensitiveParameter] $token)
{
return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => new Carbon];
}
/**
* Determine if a token record exists and is valid.
*
* @param IlluminateContractsAuthCanResetPassword $user
* @param string $token
* @return bool
*/
public function exists(CanResetPasswordContract $user, #[SensitiveParameter] $token)
{
$record = (array) $this->getTable()->where(
'email', $user->getEmailForPasswordReset()
)->first();
return $record &&
! $this->tokenExpired($record['created_at']) &&
$this->hasher->check($token, $record['token']);
}
/**
* Determine if the token has expired.
*
* @param string $createdAt
* @return bool
*/
protected function tokenExpired($createdAt)
{
return Carbon::parse($createdAt)->addSeconds($this->expires)->isPast();
}
/**
* Determine if the given user recently created a password reset token.
*
* @param IlluminateContractsAuthCanResetPassword $user
* @return bool
*/
public function recentlyCreatedToken(CanResetPasswordContract $user)
{
$record = (array) $this->getTable()->where(
'email', $user->getEmailForPasswordReset()
)->first();
return $record && $this->tokenRecentlyCreated($record['created_at']);
}
/**
* Determine if the token was recently created.
*
* @param string $createdAt
* @return bool
*/
protected function tokenRecentlyCreated($createdAt)
{
if ($this->throttle <= 0) {
return false;
}
return Carbon::parse($createdAt)->addSeconds(
$this->throttle
)->isFuture();
}
/**
* Delete a token record by user.
*
* @param IlluminateContractsAuthCanResetPassword $user
* @return void
*/
public function delete(CanResetPasswordContract $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()
{
return hash_hmac('sha256', Str::random(40), $this->hashKey);
}
/**
* Get the database connection instance.
*
* @return IlluminateDatabaseConnectionInterface
*/
public function getConnection()
{
return $this->connection;
}
/**
* Begin a new database query against the table.
*
* @return IlluminateDatabaseQueryBuilder
*/
protected function getTable()
{
return $this->connection->table($this->table);
}
/**
* Get the hasher instance.
*
* @return IlluminateContractsHashingHasher
*/
public function getHasher()
{
return $this->hasher;
}
}