weshooper/php-password-workflow

此包已被弃用且不再维护。作者建议使用 weshooper/php-email-tokens 包。

生成用于电子邮件的加密令牌(密码重置、注册验证)

6.1.1 2022-08-01 07:55 UTC

This package is auto-updated.

Last update: 2022-08-01 07:56:40 UTC


README

用于密码重置(或注册验证)电子邮件,这些令牌需要是

  1. 完全随机的
  2. 简短,仅包含简单的(0-9, A-Z 和 a-z)字符(以避免电子邮件问题)
  3. 在短时间内过期(尽管仍依赖于用户邮箱的安全性)
  4. 一旦使用或过期即删除(这部分取决于你!)
  5. 存储在数据库中时进行哈希处理(如密码,如果通过SQL注入读取则毫无用处)

关于 forgot_password.php 的示例代码

$token = new EmailToken;
$token->getEmailToken(); // include in the link you email the user (don't store anywhere!)
$token->getDatabaseHash(); // store against the user (128 character string) along with `tokenCreated`

提示:最好将用户放入队列中,然后在工作器/定时任务中生成令牌/电子邮件。

关于 reset_password.php 的示例代码

$token = new EmailToken;
$user  = loadFromHash($token->hashFromToken($_GET['token'])); // loadFromHash() is pseudo code, your bit!

if ($user && $token->stillValid($user->tokenCreated)) { // DateTime/Carbon parameter (or validate in your SQL query)
    // show password form, delete hash/expiry stored against the user
} else {
    // show generic/non-revealing 'Sorry, that token is no longer valid' message  
}

选项

可以在构造函数中传递一个数组来覆盖默认值

  • 令牌过期时间:默认的 15分钟 允许电子邮件交付延迟,但降低了电子邮件可能在不被注意的电子邮件客户端中长时间存留的风险
  • 令牌长度:默认的 24字符 对于电子邮件来说既短又好,但给用于62个区分大小写的字母数字字符的组合提供了 ~10,000,000,000,000,000,000,000,000,000,000,000,000,000,000 种组合 - 成功暴力破解是不可能的(建议使用20或更多
new EmailToken(['expiryMinutes' => 60]);
new EmailToken(['tokenLength' => 30]);
new EmailToken(['expiryMinutes' => 60, tokenLength' => 30]);

辅助函数

有两个辅助函数

$token->getExpiryMinutes(); // useful to mention in your email message
$token->getTokenLength(); // not sure what you'd use this for!

致谢

评论建议代码 来自 Martin Stoeckli 对我的知识和理解起到了至关重要的作用,让我对这一切感到满意 - 感谢 Martin!:-)