如何在使用/到期后处理重置密码请求

时间:2014-03-13 09:28:39

标签: php mysql

我正在我的网站上构建一个重置密码功能,它工作得非常好。但在某些方面我有点不确定。

现在我有一张这样的表:

password_change_request:

ID    |     user_id       |       token       |      created_time

user_id 字段中,将存储用户的用户名。在令牌 -field中,将存储生成和加密的令牌。最后在 created_time 中,将存储请求时间的时间戳。


提出请求:

当用户在忘记密码表单中输入他们的电子邮件时,系统将在用户 -table中查找以检查用户是否存在。如果用户确实存在,则会找到用户的用户名。

它还会通过加密用户电子邮件和随机SALT来生成令牌。

然后它会将包含用户名和令牌的链接发送到输入的电子邮件地址,如下所示:http://yoursite.com/login.php?action=reset&token=28130dh29sd129809sda802e&user=yourusername

当令牌存储在数据库中时,它将被额外加密。

重置密码:

当用户点击链接时,在电子邮件中,它会引导用户访问一个站点,该站点将检查 password_change_request -table中是否存在任何请求,并使用给定的用户名和加密令牌。

如果是,它会通过计算数据库中 created_time 与当前时间之间的差异来检查请求是否超过2小时。

如果时间不超过2小时,则会出现两个字段,用户可以输入新密码,然后出于安全原因重新输入密码。

当用户输入新密码然后点击“重置”按钮时,将生成用户的新SALT,并且将使用salt加密的新密码将在用户<中更新/强> - 表

当新密码更新且请求超过2小时后,该请求将从 password_change_request -table中删除。


这是一个很好的方法吗?它受到了WordPress和本网站上其他答案的启发。

但我能找到的一件事是当请求被使用/过期时会发生什么?

是否应该从数据库中永久删除?我似乎无法找到另一种方法。如果没有此功能,用户可能会一次又一次地重复使用该链接。

如果这是一个好方法,那么主要问题是如何在使用或过期后处理请求?

或者我应该只将令牌放在用户 -table的列中,就像WordPress一样,然后在请求使用/过期时清除该字段?

希望有人可以帮助我:)。

  • TheYaXxE

3 个答案:

答案 0 :(得分:1)

了解Laravel Framework如何通过docs

执行此操作

可以找到来源here

基本上它要求用户发送一个电子邮件链接,密码重置,并使用哈希绑定到电子邮件。验证哈希,然后询问新密码并询问新密码确认。

您可以通过查看github上链接的来源了解更多信息。

// Create a new reminder record and token.
public function create(RemindableInterface $user)
{
    $email = $user->getReminderEmail();

    // 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($user);

    $this->getTable()->insert($this->getPayload($email, $token));

    return $token;
}

// Create a new token for the user.
public function createNewToken(RemindableInterface $user)
{
    $email = $user->getReminderEmail();

    $value = str_shuffle(sha1($email.spl_object_hash($this).microtime(true)));

    return hash_hmac('sha1', $value, $this->hashKey);
}

// Determine if the reminder has expired.
protected function reminderExpired($reminder)
{
    $createdPlusHour = strtotime($reminder->created_at) + $this->expires;

    return $createdPlusHour < $this->getCurrentTime();
}

答案 1 :(得分:0)

最好在哈希中包含时间。然后,当用户通过带有哈希的链接进入时,您可以提取时差。 您还可以在使用后清除哈希字段。 通过这种方式,您将能够专门通知用户链接是否已过期,或无效,或者根本没有未决请求。

答案 2 :(得分:0)

是的,因为您正在使用专用表来完成此任务,所以建议您完全删除已使用的行;为什么要保留它?

其次,您可以解密哈希以提取时间,因为它的加密时间与加密时的顺序相同。

   $difference=time()-$extractedTime;       //kindof
   $hoursDifference=ceil($difference/7200); //2*60*60=7200

如果这有用,请花点时间投票,谢谢。