Skip to content

sicaboy/laravel-mfa

Repository files navigation

Laravel Multi-factor Authentication (MFA) / Two-factor Authentication (2FA)

Latest Stable Version Total Downloads License Tests PHP Version Require Packagist GitHub issues GitHub stars

Introduction

A powerful and flexible Laravel package that provides Multi-factor Authentication (MFA) / Two-factor Authentication (2FA) middleware to secure your Laravel applications. This package was originally part of sicaboy/laravel-security and has been moved to this dedicated repository.

Features

  • βœ… Easy Integration - Simple middleware-based implementation
  • βœ… Email-based MFA - Secure code delivery via email
  • βœ… Multiple Auth Guards - Support for different authentication contexts (user, admin, etc.)
  • βœ… Configurable - Flexible configuration options
  • βœ… Queue Support - Background email sending with Laravel queues
  • βœ… Cache-based - Efficient code storage and verification tracking
  • βœ… Customizable Views - Override templates to match your design
  • βœ… Laravel 5.7+ Support - Compatible with modern Laravel versions

Note

πŸš€ Advertisement: Don't Want to Build Authentication From Scratch?

Save weeks of development time with Users.au - a complete authentication solution for Laravel!

Users.au MFA Screenshot

Why Choose Users.au?

  • 🎯 Ready-to-use Authentication - Complete user management system
  • πŸ” Built-in MFA/2FA - No need for additional packages
  • ⚑ Laravel Integration - Seamless setup with your existing Laravel app
  • πŸ†“ Free to Start - Get started without any upfront costs
  • πŸ› οΈ Developer-friendly - Multiple integration options

Get Started in Minutes:

Option 1: Laravel Starter Kit (Fastest)

git clone https://github.com/Users-au/laravel-starter-kit.git
cd laravel-starter-kit
composer install

Option 2: Add to Existing Laravel App

composer require users-au/laravel-client

Option 3: Socialite Integration

composer require users-au/socialite-provider

Resources:

Skip the complexity of building authentication from scratch and focus on what makes your app unique!


Installation

Requirements

  • PHP 7.1+ or 8.0+
  • Laravel 5.7+
  • Composer

Install via Composer

composer require sicaboy/laravel-mfa

Publish Configuration and Views

php artisan vendor:publish --provider="Sicaboy\LaravelMFA\LaravelMFAServiceProvider"

This will publish:

  • Configuration file: config/laravel-mfa.php
  • View templates: resources/views/vendor/laravel-mfa/

Service Provider Registration (Laravel < 5.5)

If you're using Laravel < 5.5, manually register the service provider in config/app.php:

'providers' => [
    // ...
    Sicaboy\LaravelMFA\LaravelMFAServiceProvider::class,
],

Usage

Basic Usage

Protect your routes by applying the mfa middleware:

// Protect individual routes
Route::get('/dashboard', 'DashboardController@index')->middleware('mfa');

// Protect route groups
Route::middleware(['mfa'])->group(function () {
    Route::get('/admin', 'AdminController@index');
    Route::get('/profile', 'ProfileController@show');
});

Multiple Authentication Guards

If you use multiple authentication guards (e.g., separate user and admin authentication), specify the guard group:

// For admin routes
Route::middleware(['mfa:admin'])->group(function () {
    Route::get('/admin/dashboard', 'Admin\DashboardController@index');
});

Configure the corresponding group in config/laravel-mfa.php:

return [
    'default' => [
        // Default configuration...
    ],
    'group' => [
        'admin' => [ // Example, when using middleware 'mfa:admin'. Attributes not mentioned will be inherit from `default` above
            'login_route' => 'admin.login',
            'auth_user_closure' => function() {
                return \Encore\Admin\Facades\Admin::user();
            },
        ],
        'other_name' => [ // Middleware 'mfa:other_name'
            ...
        ]
    ],
];

Configuration Options

Email Configuration

Configure email settings in config/laravel-mfa.php:

'email' => [
    'queue' => true, // Enable queue for background sending
    'template' => 'laravel-mfa::emails.authentication-code',
    'subject' => 'Your Authentication Code',
],

Code Expiration

Set how long verification codes remain valid:

'code_expire_after_minutes' => 10, // Default: 10 minutes

Queue Configuration

For applications with queue workers running, enable background email sending:

return [
    'default' => [
        'email' => [
            'queue' => true, // Enable queue processing
        ]
    ]
];

Make sure your queue worker is running:

php artisan queue:work

API Responses

The middleware provides JSON responses for API requests:

  • 403 - User not authenticated
  • 423 - MFA verification required
{
    "error": "MFA Required",
    "url": "/mfa/generate?group=default"
}

Testing

Run the test suite:

composer test

Or run PHPUnit directly:

./vendor/bin/phpunit

Security Considerations

  • Codes expire after the configured time limit (default: 10 minutes)
  • Verification status is cached to prevent replay attacks
  • Email delivery can be queued for better performance
  • Multiple authentication contexts are supported

Roadmap

  • βœ… Email-based MFA
  • πŸ”„ SMS-based MFA
  • πŸ”„ TOTP/Authenticator app support
  • πŸ”„ User-specific MFA settings
  • πŸ”„ Backup codes

Contributing

We welcome contributions! Please see CONTRIBUTING.md for details.

Development Setup

  1. Clone the repository:
git clone https://github.com/sicaboy/laravel-mfa.git
cd laravel-mfa
  1. Install dependencies:
composer install
  1. Run tests:
composer test

Running Tests

# Run all tests
composer test

# Run tests with coverage
./vendor/bin/phpunit --coverage-html build/coverage

# Run specific test file
./vendor/bin/phpunit tests/Unit/MFAHelperTest.php

# Run specific test method
./vendor/bin/phpunit --filter testGetConfigByGroupReturnsGroupConfig

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

Support

Credits

About

Laravel 2FA / Multi-factor Authentication - Secure code delivery via email

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published