vanthao03596/laravel-password-history

保存用户密码历史,防止他们像Facebook、Google一样重复使用相同的密码

v1.1 2021-04-05 14:36 UTC

This package is auto-updated.

Last update: 2024-09-05 22:24:28 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

为了安全原因,防止用户重复使用相同的密码,保存用户密码历史,就像Google、Apple所做的那样。

安装

您可以通过composer安装此包

composer require vanthao03596/laravel-password-history

您可以使用以下命令发布并运行迁移

php artisan vendor:publish --provider="Vanthao03596\LaravelPasswordHistory\LaravelPasswordHistoryServiceProvider" --tag="password-history-migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Vanthao03596\LaravelPasswordHistory\LaravelPasswordHistoryServiceProvider" --tag="password-history-config"

这是发布配置文件的内容

return [
    /**
     * The table name to save your password histories.
     */
    'table_name' => 'password_histories',

    /*
     * The fully qualified class name of the password_histories model.
     */
    'password_history_model' => \Vanthao03596\LaravelPasswordHistory\Models\PasswordHistory::class,

    /*
     * The number of months you want to check against new password.
     */

     'months_to_check' => 12,
];

用法

要将Eloquent模型存储密码历史,只需将其添加到\Vanthao03596\LaravelPasswordHistory\HasPasswordHistory特质中

use Illuminate\Database\Eloquent\Model;
use Vanthao03596\LaravelPasswordHistory\HasPasswordHistory;

class YourModel extends Model
{
    use HasPasswordHistory;
    
    ...
}

验证规则

并且有一个验证规则可以帮助您在Laravel验证规则中检查整个密码历史与新密码。

use Vanthao03596\LaravelPasswordHistory\Rules\NotInPasswordHistory;
//...

$rules = [
    // ... 
    'password' => [
       'required',
       'confirmed',
       new NotInPasswordHistory(request()->user()),
    ]
    // ... 
];

$this->validate(...);

清理日志

使用此包一段时间后,您可能会记录大量的密码历史。此包提供了一个artisan命令password-history:clean来清理历史记录。

php artisan password-history:clean
//app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
   $schedule->command('password-history:clean')->daily();
}

每次调用时覆盖要保留的月份数

php artisan password-history:clean --months=6

测试

composer test

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全漏洞

请查阅我们的安全策略了解如何报告安全漏洞。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件