fusionspim / php-email-tokens
此包已被废弃,不再维护。未建议替代包。
生成用于电子邮件(密码重置、注册验证)的安全令牌
6.1.1
2022-08-01 07:55 UTC
Requires
- php: ^8.0
- ext-mbstring: ^8.0
- nesbot/carbon: ^2.0
- tuupola/base62: ^2.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.6
- fusionspim/php-cs-fixer-config: ^10.0
- phpunit/phpunit: ^9.0.0
README
用于密码重置(或注册验证)电子邮件,这些需要是完全随机的
- 完全随机
- 简短,只包含简单的 (0-9, A-Z 和 a-z) 字符(以避免电子邮件问题)
- 在短时间内过期(尽管仍然依赖于用户邮箱的安全性)
- 一旦使用和/或过期即删除(这部分取决于您!)
- 存储在数据库中时进行散列(如密码,如果通过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字符 对于电子邮件来说相当短,但提供了 ~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! :-)