Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 2 of 10

Multi-Factor Authentication (MFA)

MFA (Multi-Factor Authentication) requires users to present multiple pieces of evidence from different factor categories. It is the single most impactful security control for preventing account compromise - Microsoft reports MFA blocks 99.9% of automated account attacks.

45 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Authentication Factor Types

  • Knowledge (Something you know): Passwords, PINs, security questions. Weakest factor - can be phished, guessed, and stolen in breaches
  • Possession (Something you have): Hardware tokens (YubiKey), smartphone with authenticator app (TOTP), SMS/email OTP. Phishable via real-time phishing proxies (Evilginx2)
  • Inherence (Something you are): Fingerprint, face recognition, iris scan. Not transferable but has accuracy trade-offs (FRR/FAR)
  • Location (Somewhere you are): GPS location, network IP range restrictions. Used to enforce 'only allow logins from office network' policies
  • MFA strength ranking (strongest to weakest): Hardware security key (FIDO2/WebAuthn) > Authenticator app (TOTP) > Push notification > SMS OTP > Email OTP. SMS is weakest due to SIM swapping attacks

TOTP Implementation

TOTP Implementation
import pyotp
import qrcode
import io

# TOTP (Time-based One-Time Password) - RFC 6238
# Used by Google Authenticator, Authy, Microsoft Authenticator

# Generate a secret for a new user
def setup_mfa(user_id: str) -> dict:
    # 160-bit = 20-byte base32 secret
    secret = pyotp.random_base32()
    
    # Store secret securely - encrypted in database
    db.store_mfa_secret(user_id, encrypt(secret))
    
    # Generate provisioning URI for QR code
    # user scans this with their authenticator app
    totp = pyotp.TOTP(secret)
    uri = totp.provisioning_uri(
        name="alice@example.com",
        issuer_name="YourApp"
    )
    
    return {
        "secret": secret,      # Show once during setup (backup codes)
        "qr_uri": uri,         # For QR code display
    }

# Verify TOTP during login
def verify_totp(user_id: str, submitted_code: str) -> bool:
    encrypted_secret = db.get_mfa_secret(user_id)
    secret = decrypt(encrypted_secret)
    
    totp = pyotp.TOTP(secret)
    # valid_window=1 allows ±30 seconds clock drift
    return totp.verify(submitted_code, valid_window=1)

# TOTP generates a new 6-digit code every 30 seconds
# Code = HOTP(secret, floor(unix_timestamp / 30))
# Without the secret, brute-forcing 6 digits in 30 seconds is impractical

FIDO2 / WebAuthn - Phishing-Resistant MFA

  • WebAuthn is a W3C standard for hardware security key authentication - built into browsers natively
  • How it works: During registration, the device generates a key pair - private key stays on device (never exported), public key registered with the server
  • During authentication: Server sends a challenge; device signs the challenge with the private key; server verifies with stored public key
  • Phishing resistance: The device signs origin-bound challenges - even if victim reaches phishing site (evil-bank.com), the credential is only valid for the legitimate origin (bank.com). Phishing attacks are structurally impossible
  • Hardware: YubiKey ($25-55), Google Titan Key, platform authenticators (Apple Touch/Face ID, Windows Hello)
  • Adoption: Google made phishing rate zero among 85,000 employees after requiring hardware keys in 2017

Common Mistakes

  • SMS OTP as the only MFA option - SIM swapping allows attackers to redirect SMS to their device. Offer app-based TOTP or hardware keys
  • Not providing backup codes on MFA enrollment - if the user loses their device, they need a way to recover the account without support intervention
  • Not requiring MFA on admin/privileged accounts - these are the highest-value targets; enforce MFA unconditionally for all privileged access

Tip

Tip

Practice MultiFactor Authentication MFA in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).

Practice Task

Note

Practice Task - (1) Write a working example of MultiFactor Authentication MFA from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with MultiFactor Authentication MFA is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.

Key Takeaways

  • MFA (Multi-Factor Authentication) requires users to present multiple pieces of evidence from different factor categories.
  • Knowledge (Something you know): Passwords, PINs, security questions. Weakest factor - can be phished, guessed, and stolen in breaches
  • Possession (Something you have): Hardware tokens (YubiKey), smartphone with authenticator app (TOTP), SMS/email OTP. Phishable via real-time phishing proxies (Evilginx2)
  • Inherence (Something you are): Fingerprint, face recognition, iris scan. Not transferable but has accuracy trade-offs (FRR/FAR)

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/10 done