2 Factor Authentication

Introduction

A package to add 2 Factor Authentication to your litstack application. Secure the login or the submitting of forms.

The verify Field

With the verify field a form can be confirmed with a one time password become.

$form->input('header');

$form->needsVerification();

verify field

If the authenticated user hasn't activated 2 factor authentication for his account. He is asked to verify the form using his password.

Setup

Install the package via composer:

composer require litstack/2fa

User Model

Too enable 2 factor authentication on the model used for the litstack authentication it must add the 2 columns to the corresponding table:

pa make:migration add_two_fa_columns_to_lit_users_table
class AddTwoFaColumnsToLitUsersTable extends Migration
{
    public function up()
    {
        Schema::table('lit_users', function (Blueprint $table) {
            $table->boolean('two_fa_enabled');
            $table->string('two_fa_secret');
        });
    }

    public function down()
    {
        Schema::table('lit_users', function (Blueprint $table) {
            $table->dropColumn('two_fa_enabled');
            $table->dropColumn('two_fa_secret');
        });
    }
}

Execute the migraton:

php artisan migrate

Now your Model needs to implement the Litstack\TwoFA\Authenticatable contract and use the Litstack\TwoFA\HasTwoFactorAuthentication trait:

// ...

use Litstack\TwoFA\Authenticatable  as TwoFA;
use Litstack\TwoFA\HasTwoFactorAuthentication;

class User extends Authenticatable implements CanResetPasswordContract, TwoFA
{
    use HasTwoFactorAuthentication;

    // ...
}

The final thing you need to do, is adding the two_fa_secret to the hidden attributes of your model and the two_fa_enabled as a boolean cast like this:

/**
 * Hidden attributes.
 *
 * @var array
 */
protected $hidden = ['password', 'two_fa_secret'];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
    'two_fa_enabled'    => 'boolean',
];
Join us on our discord server