๐Ÿ”’
๐Ÿ”’ Security & Identity with the Larz Stack ยท SEC-101 ยท Session 4 of 5

Two-Factor Auth with larztotp & larzqr

Passwords alone are weak. Two-factor authentication adds a second check that stops most account takeovers - and the stack lets you add authenticator-app 2FA with a scannable QR in a few lines.

โฑ 8 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

A password can be guessed, phished, reused or leaked - which is why passwords alone are a weak defence. Two-factor authentication (2FA) adds a second, independent check, and it stops the large majority of account-takeover attacks. The stack makes adding authenticator-app 2FA easy: larztotp generates and verifies the codes, and larzqr turns the setup into a scannable QR.

Why 2FA works

2FA requires something you know (password) plus something you have (a code from your phone), so a stolen password alone isn't enough.

AttackPassword onlyWith 2FA
Password leaked in a breachAccount taken overStill needs the phone code
Phishing the passwordCompromisedBlocked without the code
Password reuseCascading breachesEach still needs 2FA
Guessing/brute forcePossibleInsufficient alone

That second factor - a rotating code only the real user's device produces - is why 2FA blocks most account takeovers even when the password is known to an attacker.

Add it in a few lines

TOTP (time-based one-time passwords) is the authenticator-app standard. larztotp generates the codes and provisioning URI; larzqr renders the QR the user scans to enrol.

from larztotp import TOTP, generate_secret
from larzqr import QR

secret = generate_secret()               # store per-user, secret
uri = TOTP(secret).provisioning_uri('user@app.com', issuer='MyApp')
QR(uri).to_svg('enroll.svg')             # user scans this in their authenticator

TOTP(secret).verify(code_from_user)      # at login: True/False
๐ŸŽฏ
Authenticator 2FA, standard and scannable. larztotp implements TOTP/HOTP per RFC 6238/4226 - the exact codes any authenticator app (Google Authenticator, Authy, etc.) produces - and its provisioning URI plus larzqr's QR give users the familiar 'scan to set up' flow with no image library. Store each user's secret securely (e.g. in larzvault), show the QR once at enrolment, and verify the 6-digit code at login. A big security upgrade for a few lines.

Add 2FA to your app

  • Password + code - something you know plus something you have.
  • larztotp - standard TOTP codes (RFC 6238), authenticator-compatible.
  • larzqr - the scannable enrolment QR, no image library.
  • Store the secret securely - per user, e.g. in larzvault.

๐ŸŽฏ Key takeaways

  • Passwords alone are weak (leaked, phished, reused); 2FA adds a second check that stops most takeovers.
  • 2FA = something you know (password) + something you have (a rotating device code).
  • larztotp implements standard TOTP (RFC 6238) - the codes any authenticator app produces.
  • larzqr renders the scannable enrolment QR; store each user's secret securely (e.g. larzvault).

Frequently asked

Is authenticator-app 2FA better than SMS codes?

Generally yes. SMS 2FA is better than nothing, but it's vulnerable to SIM-swapping (an attacker taking over your phone number), interception, and delays, and it depends on cell coverage. Authenticator-app TOTP (what larztotp implements) generates codes on the device itself from a shared secret, with no message to intercept and no phone-number dependency, making it more secure and reliable. It's also free and standards-based - any authenticator app works via the QR enrolment. For most applications, offering authenticator-based TOTP 2FA is the recommended default; SMS can be a fallback but shouldn't be the only option where security matters.

Where and how do I store each user's 2FA secret?

The per-user TOTP secret is sensitive - anyone with it can generate that user's codes - so it must be stored securely, never in plaintext or in code. Store it in your database as part of the user record, protected like other sensitive data (ideally encrypted at rest, which is exactly what larzvault, built on larzcrypt, is for). You generate the secret once at enrolment, show the QR to the user that one time so they can add it to their authenticator, persist the secret securely on your side, and thereafter use it only to verify the codes they enter at login. Treat it with the same care as a password hash or API key: secured, access-controlled, and rotated/reset if you suspect compromise.

Session 4 of 5 complete

Next up: Identity & Verifiable Credentials with larzid

Continue to Session 5 โ†’

Related lessons

More in this course

Course home → · Browse all courses →