Larz โบ TOTP 2FA in Python
Two-factor auth in Python, without a library
Larz ships standards-based TOTP (RFC 6238) that works with Google Authenticator,
Authy and 1Password โ built on the standard library, no pyotp, no extra install.
pip install larz
import larz.auth as auth, larz.twofa as twofa
auth.enable(app); twofa.enable(app, issuer="MyApp")
uri, secret = app.twofa.begin(req.user) # render `uri` as a QR code
codes = app.twofa.activate(req.user, code) # one-time backup codes
@app.twofa_required
@app.get("/billing")
def billing(req): ...
What you get
- Standard TOTP โ any authenticator app scans the
otpauth://QR and generates the same 6-digit codes. - Backup codes โ one-time recovery codes, issued at enrolment and single-use.
- A route guard โ
@app.twofa_requiredsends users who haven't passed the challenge to your 2FA page; users without 2FA enabled pass straight through. - Clock-drift tolerance โ a configurable window absorbs a step of skew.
FAQ
Does it work with Google Authenticator?
Yes. Larz implements standard TOTP (RFC 6238), so Google Authenticator, Authy, 1Password and Microsoft Authenticator all work.
Do I need a third-party library?
No โ the TOTP code is built on Python's hmac and base64 modules. Larz stays zero-dependency.
What about lost devices?
Enrolment returns one-time backup codes; app.twofa.verify() accepts either a live TOTP code or an unused backup code.