← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Two-Factor Authentication (2FA)

security OWASP A7:2021 PHP 7.0+ Beginner

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));

Added 15 Mar 2026
Edited 31 Mar 2026
Views 33
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 3 pings S 0 pings M 0 pings T 0 pings W 4 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T
No pings yesterday
Perplexity 9 Amazonbot 6 Google 3 SEMrush 3 ChatGPT 3 Ahrefs 2
crawler 23 crawler_json 3
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

✓ schema.org compliant