Security model¶
What the library takes care of, and what stays with you. To report a vulnerability, see the security policy.
What the library handles¶
- Tampering: cookies are signed (HMAC-SHA256), JWTs and ID tokens are verified, OAuth
stateis checked against CSRF. - Confusion between sources: a cookie signed for one cookie name is never accepted under another, a token no
MultiAuthsource takes never falls back to the cookie sent alongside it, a JWKS-backedJWTValidatorrequires anaudience, and a JWT whosetyportoken_useclaim marks it as an ID or refresh token is refused as not an access token. Providers that leave their ID tokens unmarked are told apart byaudience, or by thetypheader you askJWTValidatorfor. - Downgrades: OAuth, OIDC and JWKS URLs must be HTTPS, and
HS*algorithms are refused against a provider's keys. - Cross-site requests: a cookie source refuses requests sent from another origin unless you pass
csrf=False. - Stolen tokens: a DPoP-bound token is refused without a fresh proof signed by its key.
- Forged webhooks:
HMACSignatureAuthchecks the sender's signature over the raw body, and refuses an old timestamp when the sender sends one. - Replayed one-time codes:
totp_verifyrefuses a code from a time step already used, given thelast_stepyou stored atomically. - Open redirects: the post-login destination is checked, relative paths only by default.
- Timing attacks: secrets, tokens and signatures are compared in constant time.
- Misconfiguration: short secrets, plain-HTTP endpoints, scopes a validator cannot check, and cookie settings a browser would drop are refused loudly instead of being skipped.
What stays with you¶
- Your validator. The library hands you a credential; deciding whether it is valid is your code.
- Storage. Hash passwords with argon2, scrypt or bcrypt.
hash_token(SHA-256) is for random tokens only. - HTTPS for your own app.
- TOTP secrets. Encrypt them at rest: unlike a password, a TOTP secret cannot be stored as a hash. See the two-factor recipe.
- Rate limiting on login, one-time code and token endpoints, with failed codes capped per account. Refusals are logged so you can act on them.
- CSRF on cookie-authenticated routes where you pass
csrf=False. Either way, keepGETroutes free of side effects.
Good to know¶
- Signed is not encrypted. Anyone holding a cookie or a JWT can read its content. Never put a secret in it.
- Signed cookies are not revocable on their own. A stolen cookie stays valid until it expires. Turn on
session_id=Truewhen you need to end a session. - ID tokens are untrusted until validated with
oauth_validate_id_token. - Introspection answers are cached. A token revoked at the provider keeps working until its cached answer expires,
cache_ttlseconds at most. - The discovery URL is configuration. Never build it from request input.
- Sources and validators can be shared across threads. One instance can serve several threads, each with its own event loop, as on a free-threaded Python build. Requests on one loop share a provider fetch in flight; another loop makes its own. Your validators and
replay_checkrun concurrently too, so keep them thread-safe.
Status codes¶
| Status | Meaning |
|---|---|
| 401 | no credential, or an invalid one; carries a WWW-Authenticate challenge where the scheme has one |
401 insufficient_user_authentication | valid, but authenticated too long ago or too weakly (step-up) |
| 403 | authenticated, but not allowed, or sent from another origin (CSRF) |
| 503 | a provider's keys cannot be fetched and none are cached, or its introspection endpoint is down; clients should retry |