|
1 | 1 | # mfa |
2 | 2 | Multi Factor Authentication |
| 3 | +CodingLibs Laravel MFA |
| 4 | + |
| 5 | +Installation |
| 6 | +- Require in your Laravel 12 app composer.json or via path repository. |
| 7 | +- The service provider auto-registers. Publish config and migrations: |
| 8 | +``` |
| 9 | +php artisan vendor:publish --tag=mfa-config |
| 10 | +php artisan vendor:publish --tag=mfa-migrations |
| 11 | +php artisan migrate |
| 12 | +``` |
| 13 | + |
| 14 | +Usage |
| 15 | +```php |
| 16 | +use CodingLibs\MFA\Facades\MFA; |
| 17 | + |
| 18 | +// Email/SMS |
| 19 | +$challenge = MFA::issueChallenge(auth()->user(), 'email'); |
| 20 | +// then later |
| 21 | +$ok = MFA::verifyChallenge(auth()->user(), 'email', '123456'); |
| 22 | + |
| 23 | +// TOTP |
| 24 | +$setup = MFA::setupTotp(auth()->user()); |
| 25 | +// $setup['otpauth_url'] -> QR code; then verify |
| 26 | +$ok = MFA::verifyTotp(auth()->user(), '123456'); |
| 27 | + |
| 28 | +// Generate QR code (base64 PNG) from existing TOTP (uses bacon/bacon-qr-code) |
| 29 | +$base64 = MFA::generateTotpQrCodeBase64(auth()->user(), issuer: 'MyApp'); |
| 30 | +// <img src="$base64" /> |
| 31 | +``` |
| 32 | + |
| 33 | +Configuration |
| 34 | +- See `config/mfa.php` for email/sms/totp options. |
| 35 | + |
| 36 | +Extending: Custom Channels |
| 37 | +```php |
| 38 | +use CodingLibs\MFA\Contracts\MfaChannel; |
| 39 | +use CodingLibs\MFA\Facades\MFA; |
| 40 | +use Illuminate\Contracts\Auth\Authenticatable; |
| 41 | + |
| 42 | +class WhatsAppChannel implements MfaChannel { |
| 43 | + public function __construct(private array $config = []) {} |
| 44 | + public function getName(): string { return 'whatsapp'; } |
| 45 | + public function send(Authenticatable $user, string $code, array $options = []): void { |
| 46 | + // send via provider... |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// register at boot |
| 51 | +MFA::registerChannel(new WhatsAppChannel(config('mfa.whatsapp', []))); |
| 52 | + |
| 53 | +// then issue |
| 54 | +MFA::issueChallenge(auth()->user(), 'whatsapp'); |
| 55 | +``` |
0 commit comments