Two-Factor Authentication (2FA)
Also Known As
2FA
MFA
multi-factor authentication
two-step verification
TL;DR
Requiring a second verification factor (OTP, hardware key) in addition to a password dramatically reduces account takeover risk.
Explanation
2FA adds a second authentication layer — something the user has (TOTP app, hardware key) — to something they know (password). Even if an attacker obtains valid credentials through phishing or a data breach, they cannot authenticate without the second factor. Implementation considerations include: using TOTP (RFC 6238) rather than SMS (vulnerable to SIM-swapping), providing recovery codes, enforcing 2FA on admin accounts, and protecting the 2FA setup flow from being bypassed.
Diagram
sequenceDiagram
participant U as User
participant APP as Application
participant TOTP as TOTP / SMS
U->>APP: Username and Password
APP-->>APP: Password correct
APP-->>U: Request 2FA code
APP->>TOTP: Generate or send OTP
U->>APP: Submit OTP
APP-->>APP: Verify OTP time-based
APP-->>U: Authenticated successfully
Note over U,APP: Even if password stolen<br/>attacker needs the second factor
Common Misconception
✗ Any 2FA method provides equal protection. SMS-based 2FA is vulnerable to SIM-swapping and SS7 interception. TOTP apps are significantly stronger, and hardware keys (FIDO2/WebAuthn) are phishing-resistant in ways that SMS and TOTP are not.
Why It Matters
2FA requires something you know (password) plus something you have (phone, hardware key) — a stolen password alone is not enough to authenticate, neutralising credential stuffing and phishing attacks.
Common Mistakes
- SMS-based 2FA as the only option — SIM swapping defeats it; offer TOTP or hardware keys.
- 2FA bypass via account recovery — a weak recovery flow lets attackers skip 2FA entirely.
- Not requiring 2FA re-verification for high-risk actions (password change, payment method update).
- Storing TOTP secrets in plaintext in the database — should be encrypted at rest.
Avoid When
- Do not make 2FA optional for high-privilege accounts — treat it as mandatory for admins.
- Do not implement 2FA over SMS for high-security contexts — SS7 attacks and SIM swapping make it interceptable.
When To Use
- Enable 2FA for all admin accounts, privileged roles, and any account with access to sensitive data.
- Use TOTP (authenticator app) as the default second factor — more secure than SMS which is vulnerable to SIM swap.
Code Examples
✗ Vulnerable
// 2FA check bypassable via recovery:
if ($user->hasTwoFactor() && !$this->verifyTotp($code)) {
return $this->offerRecovery(); // Recovery flow has weaker verification
// Attacker: 'forgot my 2FA device' → answers security questions → bypasses 2FA
}
✓ Fixed
// TOTP (Google Authenticator compatible) — use spomky-labs/otphp
use OTPHP\TOTP;
// Setup — generate secret and QR code URI
\$totp = TOTP::generate();
\$totp->setLabel(\$user->email);
\$totp->setIssuer('MyApp');
\$secret = \$totp->getSecret(); // store encrypted in DB
\$uri = \$totp->getProvisioningUri(); // display as QR code
// Verify — check the 6-digit code
function verifyTotp(string \$secret, string \$code): bool {
\$totp = TOTP::createFromSecret(\$secret);
return \$totp->verify(\$code, null, 1); // ±1 time window tolerance
}
// Recovery codes — generate 8 codes, hash and store, show once
\$codes = array_map(fn() => bin2hex(random_bytes(5)), range(1, 8));
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
31 Mar 2026
Views
33
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Perplexity 9
Amazonbot 6
Google 3
SEMrush 3
ChatGPT 3
Ahrefs 2
Also referenced
How they use it
crawler 23
crawler_json 3
Related categories
⚡
DEV INTEL
Tools & Severity
🟠 High
⚙ Fix effort: Medium
⚡ Quick Fix
Implement TOTP with a library like RobThree/TwoFactorAuth; generate backup codes with random_bytes(10); never use SMS as sole 2FA
📦 Applies To
PHP 7.0+
web
🔗 Prerequisites
🔍 Detection Hints
Authentication flow with no second factor check after password verification
Auto-detectable:
✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Low
✗ Manual fix
Fix: High
Context: File
Tests: Update
CWE-308