项目作者: sicaboy

项目描述 :
Laravel 2FA / Multi-factor Authentication
高级语言: PHP
项目地址: git://github.com/sicaboy/laravel-mfa.git
创建时间: 2020-04-12T13:08:33Z
项目社区:https://github.com/sicaboy/laravel-mfa

开源协议:MIT License

下载


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)

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

Option 2: Add to Existing Laravel App

  1. composer require users-au/laravel-client

Option 3: Socialite Integration

  1. 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

Install via Composer

  1. composer require sicaboy/laravel-mfa

Publish Configuration and Views

  1. 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:

  1. 'providers' => [
  2. // ...
  3. Sicaboy\LaravelMFA\LaravelMFAServiceProvider::class,
  4. ],

Usage

Basic Usage

Protect your routes by applying the mfa middleware:

  1. // Protect individual routes
  2. Route::get('/dashboard', 'DashboardController@index')->middleware('mfa');
  3. // Protect route groups
  4. Route::middleware(['mfa'])->group(function () {
  5. Route::get('/admin', 'AdminController@index');
  6. Route::get('/profile', 'ProfileController@show');
  7. });

Multiple Authentication Guards

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

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

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

  1. return [
  2. 'default' => [
  3. // Default configuration...
  4. ],
  5. 'group' => [
  6. 'admin' => [ // Example, when using middleware 'mfa:admin'. Attributes not mentioned will be inherit from `default` above
  7. 'login_route' => 'admin.login',
  8. 'auth_user_closure' => function() {
  9. return \Encore\Admin\Facades\Admin::user();
  10. },
  11. ],
  12. 'other_name' => [ // Middleware 'mfa:other_name'
  13. ...
  14. ]
  15. ],
  16. ];

Configuration Options

Email Configuration

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

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

Code Expiration

Set how long verification codes remain valid:

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

Queue Configuration

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

  1. return [
  2. 'default' => [
  3. 'email' => [
  4. 'queue' => true, // Enable queue processing
  5. ]
  6. ]
  7. ];

Make sure your queue worker is running:

  1. php artisan queue:work

API Responses

The middleware provides JSON responses for API requests:

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

Testing

Run the test suite:

  1. composer test

Or run PHPUnit directly:

  1. ./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:

    1. git clone https://github.com/sicaboy/laravel-mfa.git
    2. cd laravel-mfa
  2. Install dependencies:

    1. composer install
  3. Run tests:

    1. composer test

Running Tests

  1. # Run all tests
  2. composer test
  3. # Run tests with coverage
  4. ./vendor/bin/phpunit --coverage-html build/coverage
  5. # Run specific test file
  6. ./vendor/bin/phpunit tests/Unit/MFAHelperTest.php
  7. # Run specific test method
  8. ./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