scheb/2fa-backup-code
Adds backup code support to scheb/2fa for Symfony apps. Generate, store, and validate one-time recovery codes so users can access accounts when they lose their 2FA device. Integrates with existing 2FA flows and user providers.
scheb/2fa-backup-code package extends the scheb/2fa-bundle by adding backup code functionality, which is a critical component of Two-Factor Authentication (2FA). This aligns well with systems requiring compliance with security best practices (e.g., NIST SP 800-63B) where backup codes are mandatory for recovery.scheb/2fa-bundle, it leverages Laravel’s service container, event system, and configuration—reducing boilerplate and ensuring consistency with existing Laravel security patterns.scheb/2fa-bundle (≥v5.0), which in turn depends on google/authenticator and paragonie/sodium_compat.backup_codes). Ensure your DB supports encrypted storage (e.g., Laravel’s encrypt helper).config/2fa.php (e.g., backup code count, expiration).| Risk Area | Mitigation Strategy |
|---|---|
| Backup Code Security | Ensure codes are hashed/encrypted at rest (Laravel’s encrypt or hash). |
| User Experience (UX) | Backup codes must be securely delivered (e.g., email/SMS with rate-limiting). |
| Migration Complexity | If adopting from scratch, plan for database schema changes and user onboarding. |
| Bundle Version Lock | Pin scheb/2fa-bundle version to avoid breaking changes during updates. |
| Multi-Tenant Support | Backup codes may need tenant-scoped storage if using SaaS architecture. |
Compliance Requirements:
Log facade.)User Flow Integration:
Scalability:
Testing:
Authenticatable contracts).scheb/2fa-bundle and its dependencies.backup_codes table (check package docs for schema).// config/2fa.php
'backup_codes' => [
'count' => 10, // Default backup codes per user
'expire_after' => 365, // Days until codes expire
'encrypt' => true, // Use Laravel encryption
],
Scheb\TwoFactorAuth\BackupCodeManager to the container.scheb/2fa-bundle (e.g., TOTP + backup codes).scheb/2fa-bundle compatibility.openssl, bcmath (for cryptographic ops).Scheb\*).scheb/2fa-bundle + backup code extension.scheb/2fa-bundle for breaking changes (e.g., PHP 8.2+ features).composer.json to pin major versions:
"scheb/2fa-bundle": "^5.0",
"scheb/2fa-backup-code": "^1.0"
// app/Console/Commands/RevokeOldBackupCodes.php
use Scheb\TwoFactorAuth\BackupCodeManager;
public function handle(BackupCodeManager $manager) {
$manager->revokeExpiredCodes(Carbon::now()->subDays(365));
}
config/2fa.php and Blade template rendering.BackupCode model uses unique constraints.tinker to inspect backup codes:
$user->backupCodes()->get();
user_id.| Failure Scenario | Mitigation |
|---|---|
| Backup codes leaked | Enforce short-lived codes and rate-limiting. |
| Database corruption | Use transactions for backup code operations. |
| User loses all recovery options | Implement admin override (with audit logs) for critical accounts. |
| Package abandonment | Fork the repo or extract backup code logic into your own service. |
"Print your backup codes and store them in a safe place. Never share them."
How can I help you explore Laravel packages today?