简体   繁体   中英

Laravel 5.1 login with mfa token

I'm trying to get the a login with mfa to work. I'm using the https://github.com/antonioribeiro/google2fa package.

Basically the user-migration looks like this

class CreateUsersTable extends Migration {
/**
 * Run the migrations.
 *
 * @return void
 */
public function up() {
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->rememberToken();
        $table->string('google2fa_secret');
        $table->boolean('useMfa')->default(false);;
        $table->timestamps();
    });
}
...

If the user has not yet activated mfa I create a new secret every time the user opens the profile page.

    if(!$user->useMfaToken()){
        $google2fa = new Google2FA();
        $user->google2fa_secret = $google2fa->generateSecretKey();
        $user->save();
        $google2fa_url = $google2fa->getQRCodeGoogleUrl(
            'DatenPro.de',
            $user->email,
            $user->google2fa_secret
        );
    }

If the user enters the secret for finalizing the activation of mfa this will be executed:

public function saveMfa(){
    $user = \Auth::user();
    $secret = \Input::get('secret');
    $google2fa = new Google2FA();
    $valid = $google2fa->verifyKey($user->google2fa_secret, $secret);
    if($valid){
        $user->useMfa = true;
        $user->save();
        return redirect()->back()->withMessage('mfa sucessfully activated');
    }
...

Now I'm working on the login with a mfa-token. I want that the user has the option to enter the token at the login page, if he has already activated it, otherwise if the mfa-Checkbox is deselected the "secret" text-input is hidden.

Email:    __________
Password: __________
Use Mfa:  [x]
Secret:    __________

Where do I have to put the checks of the mfa token? I have read about it to check it through a middleware and a session-variable, but this seems kind of wrong.

Just figured it out before posting.

You can implement a "authenticated"-method in the AuthController. This could look like this:

public function authenticated($request, $user){        
    if($user->useMfaToken()){
        $secret = \Input::get('secret');
        $google2fa = new Google2FA();
        $validMfaToken = $google2fa->verifyKey($user->google2fa_secret, $secret);
    }else{
        $validMfaToken = true;
    }
    if($validMfaToken){
        return redirect()->intended('dashboard');
    }
    Auth::logout();
    return redirect($this->loginPath)
        ->withInput($request->only('email', 'remember'))
        ->withErrors([
            'secret' => 'mfa token was not corret',
        ]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM