madmikeyb/reauthenticate

通过让用户重新输入密码来重新验证应用程序特定部分的用户。

1.7.0 2023-04-08 13:07 UTC

This package is auto-updated.

Last update: 2024-09-08 15:59:16 UTC


README

因为有时,你需要那额外的安全层

Software License

通过让用户重新输入应用程序特定部分的密码来重新验证用户(适用于Laravel 5)。

Route::group(['middleware' => ['auth','reauthenticate']], function () {

    Route::get('user/payment', function () {
        // Needs to re-enter password to see this
    });

});

注意

此软件包是 mpociot/reauthenticate 的分支,增加了两个额外功能,并更新了README说明以适应Laravel 5.5+,这是Laravel的新LTS版本。

我对这项工作不享有版权,我只是将其分支并修改了它。 已提交了一个PR,但没有得到处理,因此,这个分支被发布到了Packagist。

内容

安装

要将重新验证添加到您的项目,只需在终端运行以下命令

composer require madmikeyb/reauthenticate

用法

将中间件添加到您的Kernel

在您的 app\Http\Kernel.php 文件中,将重新验证中间件添加到 $routeMiddleware 数组。

protected $routeMiddleware = [
    // ...
    'reauthenticate'         => \Mpociot\Reauthenticate\Middleware\Reauthenticate::class,
    // ...
];

添加路由和视图

默认情况下,reauthanticate 正在寻找一个路由 auth/reauthenticate 和一个视图 auth.reauthenticate,其中将包含一个密码字段。

一个示例视图可以从 这里 复制。请注意,此文件需要手动复制,因为我不想把这个包与一个服务提供商一起膨胀。

可以从 Reauthenticates 特性中使用HTTP控制器方法,因此您的AuthController看起来像这样

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Mpociot\Reauthenticate\Reauthenticates;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
    use AuthenticatesUsers, Reauthenticates;

请确保将重新验证路由从 guest 中间件中排除。

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => ['logout','getReauthenticate','postReauthenticate'] ]);
    }

要开始,将这些路由添加到您的 routes.php 文件中

// Reauthentication routes
Route::namespace('Auth')->group(function () {
    Route::get('auth/reauthenticate', 'LoginController@getReauthenticate');
    Route::post('auth/reauthenticate', 'LoginController@postReauthenticate');
});

就这样。

一旦用户成功重新验证,有效的登录将默认存储30分钟。

可选配置

可以通过您的 config/app.php 文件(可选)配置重新验证。以下键受支持

return [
    /*
    |--------------------------------------------------------------------------
    | Reauthenticate
    |--------------------------------------------------------------------------
    */
    
    /**
     * The URL to redirect to after re-authentication is successful.
     */
    'reauthenticate_url' => '/custom-url',

    /**
     * The key to use as your re-authentication session key.
     */
    'reauthenticate_key' => 'custom-reauthentication-key',

    /**
     * The time (in minutes) that the user is allowed to access the protected area 
     * before being prompted to re re-authenticate.
     */
    'reauthenticate_time' => 30,
];

许可证

重新验证是免费软件,根据MIT许可证条款分发。