Skip to main content

quart-security

quart-security is a native async authentication extension for Quart.

It is designed as a practical replacement path for Flask-Security style session auth in Quart applications, without Flask shims and without Flask-Login.

What You Get

Core auth

  • Session-based login and logout
  • Email/password registration
  • Password change flow (including OAuth-style users that don’t know an initial random password)
  • current_user proxy
  • @auth_required("session") and @roles_required(...)

MFA

  • TOTP setup and verification
  • Recovery code generation and one-time consumption

WebAuthn (passkeys / security keys)

  • Credential registration
  • Passwordless sign-in (first factor)
  • Authenticated verification flow (step-up / second factor)
  • Credential deletion

Extension and compatibility surface

  • Quart extension pattern (Security(app, datastore))
  • Flask-Security-style endpoint naming through url_for_security()
  • Signals for auth lifecycle events
  • Overridable templates under templates/security/
  • Datastore hooks can be implemented as async methods or simple sync methods

Non-Goals (Current Scope)

This project intentionally focuses on session auth and MFA currently in active use:

  • No token-based API auth
  • No SMS/email OTP
  • No account locking workflow
  • No remember-me token system

Installation

Install from repository

uv add git+https://github.com/level09/quart-security.git

Local development

uv sync --group dev
uv run pytest -q

Package build backend: flit.

Quick Integration

from quart import Quart
from quart_security import Security, SQLAlchemyUserDatastore

import os

# Models must include the fields required by enabled features.
from myapp.models import db, User, Role, WebAuthnCredential


def create_app():
    app = Quart(__name__)

    app.config.update(
        SECRET_KEY=os.environ["SECRET_KEY"],
        SECURITY_POST_LOGIN_VIEW="/dashboard",
        SECURITY_POST_REGISTER_VIEW="/login",
    )

    db.init_app(app)
    datastore = SQLAlchemyUserDatastore(
        db,
        User,
        Role,
        webauthn_model=WebAuthnCredential,
    )

    Security(app, datastore)
    return app

Required Model Surface

Your user/role models are expected to provide the fields used by active features.

Minimum practical user fields:

  • fs_uniquifier
  • email
  • password
  • active
  • roles

For tracking / MFA / WebAuthn features:

  • last_login_at, current_login_at, last_login_ip, current_login_ip, login_count
  • tf_primary_method, tf_totp_secret, mf_recovery_codes
  • fs_webauthn_user_handle
  • relationship/association for stored WebAuthn credentials

Required for the default lockout policy:

  • failed_login_count
  • locked_until

These fields are required when login or MFA lockout is enabled. The built-in counter limits account-level guesses. Deploy an application or edge rate limiter for source IP and distributed abuse controls.

The database must enforce unique normalized emails, fs_uniquifier values, WebAuthn user handles, and credential IDs. Use a stable primary key separate from fs_uniquifier: this value changes when security settings change.

Key Configuration

The extension uses SECURITY_* keys for migration-friendly configuration.

Core:

  • SECURITY_PASSWORD_HASH (default: argon2 / argon2id; set to pbkdf2_sha512 to keep old behavior)
  • SECURITY_PASSWORD_SALT (legacy salted-hash verification only)
  • SECURITY_PASSWORD_LENGTH_MIN (default: 12)
  • SECURITY_PASSWORD_BREACH_CHECK (default: True) - HIBP k-anonymity check on register/change
  • SECURITY_PASSWORD_BREACH_COUNT_MIN (default: 1) - minimum breach count to reject
  • SECURITY_ARGON2_MEMORY_COST / SECURITY_ARGON2_TIME_COST / SECURITY_ARGON2_PARALLELISM - override argon2 params (production defaults: 19456/2/1)
  • SECURITY_LOGIN_MAX_ATTEMPTS (default: 5)
  • SECURITY_LOCKOUT_MINUTES (default: 15)
  • SECURITY_REGISTERABLE
  • SECURITY_CHANGEABLE
  • SECURITY_TRACKABLE
  • SECURITY_CSRF_PROTECT (default: True)
  • SECURITY_COOKIE_SECURE (default: True; sets SESSION_COOKIE_SECURE)
  • SECURITY_FRESHNESS (default: 60 minutes)

Password reset and email confirmation:

  • SECURITY_RECOVERABLE (default: False) - password reset by email at /reset
  • SECURITY_CONFIRMABLE (default: False) - self-registered accounts sign in only after confirming their email; needs a confirmed_at column
  • SECURITY_PUBLIC_URL (default: unset) - origin for links in email, e.g. https://app.example.com; required when either flow sends mail, and never taken from the request's Host header
  • SECURITY_RESET_PASSWORD_WITHIN (default: 1 hour)
  • SECURITY_CONFIRM_EMAIL_WITHIN (default: 1 day)

Existing pbkdf2_sha512 and bcrypt hashes continue to verify after the argon2 default change. They are transparently rehashed to argon2id on the user's next successful login.

Until then, a failed login for an account with a legacy hash takes a different time than one for an unknown email, which is checked against an argon2id dummy, so timing can reveal that the account exists. Apps that import hashes from another system should require a password reset rather than leave them in place.

2FA:

  • SECURITY_TWO_FACTOR
  • SECURITY_TOTP_ISSUER
  • SECURITY_MULTI_FACTOR_RECOVERY_CODES
  • SECURITY_MULTI_FACTOR_RECOVERY_CODES_N

Recovery codes are displayed only when generated and are stored as keyed hashes. Existing plaintext codes remain valid until their next successful use, when the remaining codes are migrated.

Accepted TOTP time steps, recovery codes, and WebAuthn challenges cannot be reused. Disabling an authenticator requires a current TOTP or unused recovery code. Wait for the next TOTP code if the current one was just used to sign in.

WebAuthn:

  • SECURITY_WEBAUTHN
  • SECURITY_WAN_ALLOW_AS_FIRST_FACTOR
  • SECURITY_WAN_ALLOW_AS_MULTI_FACTOR
  • SECURITY_WAN_RP_ID (optional override)
  • SECURITY_WAN_RP_NAME (optional override)
  • SECURITY_WAN_EXPECTED_ORIGIN (optional override)
  • SECURITY_WAN_REQUIRE_USER_VERIFICATION (default: True)

Routing:

  • SECURITY_POST_LOGIN_VIEW
  • SECURITY_POST_REGISTER_VIEW

A visit to /login?next=/path sends the user to /path after they sign in, through 2FA and passkey sign-in too. Only same-site paths are honored, the next visit to /login replaces the target, and a sign-in finishing more than 10 minutes after the form was shown ignores it.

Registration answers the same way whether or not the email already has an account. For an existing email nothing is created and user_register_conflict fires with the existing user. On its own that is not enough: someone can register an email and try to sign in with the password they chose, which works only if the account is new. Turn on SECURITY_CONFIRMABLE to close that gap.

Password reset and email confirmation

With SECURITY_RECOVERABLE, /reset asks for an email and always gives the same answer. The link goes to the address stored on the account, works once, expires after SECURITY_RESET_PASSWORD_WITHIN, and stops working if the account's email or password changes first. Opening it only shows the form, so mail scanners cannot spend it. A reset rotates fs_uniquifier, which signs out every session, clears a lockout, confirms the email, fires password_changed and sends a notice.

With SECURITY_CONFIRMABLE (and SECURITY_REGISTERABLE), a new account stores confirmed_at = NULL and cannot sign in until its owner opens the emailed link and presses the button on that page. The button is there because mail scanners open links: confirming on GET would let a scanner activate an account someone else registered with that address, with that person's password. Until then a login fails with the same message as a wrong password or a locked account, and /confirm sends a fresh link. Registering an existing email sends the owner a notice instead. Accounts created any other way should set confirmed_at; a column default does not work, because SQLAlchemy fills it in even when registration sets None.

Only token hashes are stored. Lookups, token writes, account creation and mail for these flows run after the response through app.add_background_task, so response time does not depend on whether an email has an account. The same goes for counting failed logins. As a result, user_registered and user_register_conflict fire after the response when confirmation is on, in an app context with no request.

Mail goes through send_email and the MAIL_* settings by default. Pass Security(app, datastore, send_mail=fn), where fn is async (subject, recipient, body), to use something else. Bodies come from security/email/*.txt templates, which the app can override.

Route Map

Core:

  • /login
  • /register
  • /logout (POST only)
  • /change
  • /reset, /reset/<token> (with SECURITY_RECOVERABLE)
  • /confirm, /confirm/<token> (with SECURITY_CONFIRMABLE and SECURITY_REGISTERABLE)

2FA:

  • /tf-setup
  • /tf-validate
  • /tf-select
  • /mf-recovery-codes
  • /mf-recovery

WebAuthn:

  • /wan-register
  • /wan-register-response
  • /wan-signin
  • /wan-signin-response
  • /wan-verify
  • /wan-verify-response
  • /wan-delete

Template Overrides

Default templates are intentionally simple and framework-neutral.

Override by placing templates with the same names under your app’s templates/security/ directory.

Public API

from quart_security import (
    Security,
    SQLAlchemyUserDatastore,
    current_user,
    auth_required,
    roles_required,
    UserMixin,
    RoleMixin,
    hash_password,
    verify_password,
    user_authenticated,
    user_logged_out,
    password_changed,
    tf_profile_changed,
    user_registered,
    user_register_conflict,
    url_for_security,
)

Testing

Project tests cover:

  • password hashing/validation
  • auth and role decorators
  • register/login/logout/change-password
  • TOTP and recovery code flows
  • WebAuthn register/sign-in/verify/delete route behavior

Run:

uv run pytest -q

Run this in a HTTPS staging environment with production-like hostnames and real browser prompts:

  1. Register a passkey from /wan-register and verify it is persisted with expected name, usage, and sign_count.
  2. Complete passwordless sign-in from /wan-signin with the same credential.
  3. Complete authenticated verify flow from /wan-verify while already signed in.
  4. Delete credential from /wan-delete and confirm subsequent passkey auth fails for that credential.
  5. Repeat step 1 and step 2 with a second authenticator type (for example platform passkey + hardware key) to validate device portability assumptions.

Notes for Production

  • Run behind HTTPS for WebAuthn in non-local environments.
  • Set explicit WebAuthn RP values (SECURITY_WAN_RP_ID, SECURITY_WAN_EXPECTED_ORIGIN) when behind proxies or multiple domains.
  • Keep CSRF protection enabled unless you have a deliberate replacement.

Version 2.0.0 migration

This update requires a shared quart_security_state table. It stores expiring authentication records and verification state. Cookie contents contain opaque references, not pending authenticator secrets. All workers must use the same database. Existing login cookies are rejected after this update.

Create the table through your application's database migration before deploying the new library. For example, in an Alembic migration:

from alembic import op
from quart_security import SecurityState


def upgrade():
    SecurityState.__table__.create(op.get_bind())

This includes the expiry index. Startup checks that the table is present. Protect database access and backups because pending TOTP secrets are stored there. Expired records are removed when new state is written. Maintenance jobs can also call await security.state_store.purge_expired() in an application context and close the datastore after the job.

For a session factory, use async_sessionmaker(engine, expire_on_commit=False). The library retains the session through commits and closes factory-owned sessions at the end of each HTTP request or WebSocket connection. Read-only and failed requests also close their sessions. A supplied db.session or session instance remains owned by the host application; the host must close or roll it back. Outside a request, close factory-owned sessions with await datastore.close(). Do not share one session across concurrent tasks.

Password and MFA profile changes rotate the user's fs_uniquifier, which rejects older authenticated and pending login sessions. Logout revokes the current authentication record, including copied cookies for that session. Password changes keep the requesting session authenticated. Passkey registration and deletion also revoke older sessions. A secondary passkey cannot perform passwordless sign-in. A primary passkey uses its own user verification and does not require the account's TOTP code.

Password helpers now use the current application's settings. Outside an app context, pass app=app, for example hash_password(password, app=app).

Cookies default to Secure, HttpOnly, and SameSite=Lax. For local HTTP development only, set SECURITY_COOKIE_SECURE=False. An explicit SameSite value is preserved. Use a strong random secret key and explicit RP/origin settings behind proxies. The host still needs source-IP and global rate limits; account lockout does not limit registration or anonymous challenge generation. Lockout fields are required when SECURITY_LOGIN_MAX_ATTEMPTS is greater than zero. Setting it to zero explicitly disables account lockout.

Custom datastores require a shared state_store passed to Security. Its async methods must implement this contract and propagate storage errors:

Method Contract
put(payload, ttl=seconds, token=None) Persist a dictionary with an expiry and return an unpredictable reference.
get(token) Return unexpired state or None.
pop(token) Atomically consume unexpired state; exactly one caller receives it.
claim(token, ttl=seconds) Atomically reserve a key; return False while another unexpired claim exists.

A process-local dictionary is suitable only for tests. Custom datastores must also provide record_auth_failure(user, max_attempts=..., lockout_minutes=...) as an atomic increment/lock update and replace_recovery_codes(user, expected, remaining) as an atomic conditional replacement returning a boolean. rotate_uniquifier(user, expected, replacement) must atomically check the old value and persist both the new value and staged profile changes. Return False and roll back staged changes when the old value no longer matches. Concurrent profile updates return HTTP 409; the user must sign in again. Persist these changes before returning. The extension fails at initialization if an enabled control lacks its required hook or SQLAlchemy model fields.

Dependencies now include the Pillow, bcrypt, and SQLAlchemy asyncio extras and patched minimum versions of aiosmtplib, cryptography, and cbor2. The release workflow runs lint and tests before building and publishing. CI tests Python 3.11 through 3.14.

TOTP encryption and key rotation

Version 2.0.1 encrypts enrolled TOTP seeds and pending setup secrets with authenticated Fernet encryption. Legacy plaintext seeds remain readable and are encrypted on successful login or an authenticated request. Hosts should encrypt dormant records during upgrade using encrypt_totp_secret(value, app=app). This requires no schema change for a 255-character seed column.

By default, the key is derived from SECRET_KEY with a separate purpose label. Keep that key stable. For independent rotation, configure SECURITY_TOTP_ENCRYPTION_KEYS as a nonempty list of Fernet keys. The first key encrypts new data; all keys can decrypt existing data. Retain old keys while rotating existing seeds with encrypt_totp_secret, including active pending setup states until their five-minute expiry. Remove old keys only after all data and required backups have been handled. Never rotate SECRET_KEY without preserving the derived MFA key if using the default. derive_totp_encryption_key(old_secret_key) supplies that key for an explicit keyring. Protect the keyring separately from database backups.

Metadata

Release files for quart-security 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for quart-security 2.1.0
File Size Uploaded Provenance Actions
quart_security-2.1.0.tar.gz 44.3 kB With provenance Details for quart_security-2.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for quart-security 2.1.0
File Interpreter ABI Platform Actions
quart_security-2.1.0-py3-none-any.whl Python 3 none any Details for quart_security-2.1.0-py3-none-any.whl

Total release size: 96.1 kB

Release files / quart_security-2.1.0.tar.gz

Download URL quart_security-2.1.0.tar.gz
Size 44.3 kB
Tags Source
SHA-256 checksum
How to use checksums
feac78f60628d32c32fe857e4cc058cbc9a0d5ef58dfcd86a78fd9a4ba481754
BLAKE2b-256 checksum
How to use checksums
d7c8e6866fd4c47580392ad5b938c922feb28aa566c6575a5ccd7a152d587596
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

Release files / quart_security-2.1.0-py3-none-any.whl

Download URL quart_security-2.1.0-py3-none-any.whl
Size 51.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d22066564fb5712e684b3753354094f0c5c3b9bc31bcc46adbb33864e3da3aef
BLAKE2b-256 checksum
How to use checksums
7cefca026d315a0626d4145a27cdd9cce04e4d83d3f433392f2215fa2dc9ae0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

Security for quart-security 2.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for quart_security-2.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page