6

私は実際にプロジェクトに2要素認証を実装しています。私がしたことは

Auth::user()->google2fa_passed = 1;

実際、実際には保存されません。別のページに移動すると、値が失われます。

また、ユーザーがログアウトすると (またはユーザーがブラウザーからセッション Cookie を削除すると)、ログイン ページが表示され、2 要素認証が再度行われるため、別のセッションを維持したくありません。

ユーザーセッションにもう1つの属性を保存する方法はありますか?

4

2 に答える 2

2

結局、私はsession保管することにしました。

6桁のコードを入力した後、フラグをセッションに保存します

\Session::put('totp_passed', 1);

app/Http/Middleware/Authenticate.phpで、セッションが期限切れになった場合は2FA セッションを削除します

public function handle($request, Closure $next)
{   
    if ($this->auth->guest()) {
        // remove the 2-factor auth if the user session expired
        \Session::forget('totp_passed'); // <------- add this line

        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->route('auth.login');
        }   
    }
    return $next($request);
}

次に、別のミドルウェアを作成します (例: app/Http/Middleware/TwoFactorAuth.php)。

namespace App\Http\Middleware;

use Closure;

class TwoFactorAuth
{
    /** 
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {   
        if (!\Session::has('totp_passed')) {
            return redirect()->route('auth.2fa');
        }   

        return $next($request);
    }   
}

app/Http/Kernel.php

protected $routeMiddleware = [ 
    'auth'       => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest'      => \App\Http\Middleware\RedirectIfAuthenticated::class,
    '2fa'        => \App\Http\Middleware\TwoFactorAuth::class, // <------ add this line
];

使い方

Route::group(['middleware' => 'auth'], function () {
    // must be login first only can access this page
    Route::get('2fa', ['as' => 'auth.2fa', 'uses' => 'Auth\AuthController@get2FactorAuthentication']);
    Route::post('2fa', ['uses' => 'Auth\AuthController@post2FactorAuthentication']);

    // add 2-factor auth middleware
    Route::group(['middleware' => '2fa'], function () {
        // all routes that required login
    }); 
});
于 2015-11-11T03:39:57.187 に答える