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.
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.
| Attack | Password only | With 2FA |
|---|---|---|
| Password leaked in a breach | Account taken over | Still needs the phone code |
| Phishing the password | Compromised | Blocked without the code |
| Password reuse | Cascading breaches | Each still needs 2FA |
| Guessing/brute force | Possible | Insufficient 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/FalseAdd 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.
Next up: Identity & Verifiable Credentials with larzid
Continue to Session 5 โRelated lessons
Managing Secrets with larzvault
API keys, passwords, tokens - every app has secrets, and leaking them is a top cause of brโฆ
Security & Identity with the Larz StackIdentity & Verifiable Credentials with larzid
Beyond passwords lies self-certifying identity: cryptographic identities you control, and โฆ
Building with the Larz StackSecurity & Identity โ Crypto You Can Trust
Security code is where 'roll your own' is scariest โ so this family sticks to standard, RFโฆ