Skip to main content

Google Cloud Firestore session storage backend for aiohttp-session

Project description

aiohttp-session-firestore

CI PyPI Python License

Google Cloud Firestore session storage backend for aiohttp-session.

Drop-in, async, server-side sessions — the cookie holds only an opaque key while all session data lives in Firestore.


Installation

pip install aiohttp-session-firestore

Quick start

from aiohttp import web
from aiohttp_session import setup, get_session
from google.cloud.firestore_v1 import AsyncClient

from aiohttp_session_firestore import FirestoreStorage

async def handler(request: web.Request) -> web.Response:
    session = await get_session(request)
    session["visits"] = session.get("visits", 0) + 1
    return web.Response(text=f"Visits: {session['visits']}")

def create_app() -> web.Application:
    app = web.Application()
    firestore_client = AsyncClient()
    storage = FirestoreStorage(firestore_client, max_age=86400)
    setup(app, storage)
    app.router.add_get("/", handler)
    return app

if __name__ == "__main__":
    web.run_app(create_app())

Configuration

FirestoreStorage accepts every parameter that aiohttp_session.AbstractStorage does, plus a few Firestore-specific ones:

Parameter Type Default Description
client AsyncClient (required) Firestore async client instance
collection_name str "aiohttp_sessions" Firestore collection for session documents
key_factory (() -> str) | None None Callable that produces new session keys. None uses Firestore auto-generated IDs.
cookie_name str "AIOHTTP_SESSION" Name of the HTTP cookie
max_age int | None None Session lifetime in seconds (None = browser session)
secure bool | None None Secure cookie flag — set to True in production
httponly bool True HttpOnly cookie flag
samesite str | None None SameSite cookie attribute ("Lax", "Strict", or "None")
domain str | None None Cookie domain
path str "/" Cookie path
encoder (object) -> str Firestore-aware json.dumps Session data encoder (handles DatetimeWithNanoseconds)
decoder (str) -> Any json.loads Session data decoder

Production recommendations

storage = FirestoreStorage(
    client,
    max_age=86400,       # 24-hour sessions
    secure=True,         # HTTPS only
    httponly=True,        # default — prevents JS access
    samesite="Lax",      # CSRF protection
)

Session expiration & Firestore TTL

When max_age is set, each session document is written with an expire field containing a UTC datetime. The library checks this field on every read and treats expired documents as missing immediately.

For automatic cleanup of expired documents, configure a Firestore TTL policy on the expire field:

gcloud firestore fields ttls update expire \
    --collection-group=aiohttp_sessions \
    --enable-ttl

Note: Firestore's TTL deletion is best-effort and may take up to 72 hours. The server-side expiration check in load_session ensures correctness regardless of TTL policy timing.

Document structure

Each session is stored as a Firestore document:

aiohttp_sessions/{firestore-auto-id}
├── data: '{"created": 1700000000, "session": {"user": "alice"}}'
└── expire: 2024-11-15T12:00:00Z   (only when max_age is set)

The data field contains a JSON-encoded string (controlled by the encoder/decoder parameters). The expire field is a native Firestore Timestamp, compatible with TTL policies.

Firestore costs

Each request that touches the session incurs:

Operation Firestore cost
Load (no cookie) 0 reads (short-circuits before Firestore call)
Load (cookie, doc exists) 1 document read
Load (cookie, doc missing) 1 document read
Save (non-empty session) 1 document write
Save (empty new session) 0 writes (skipped)
Save (empty existing session) 1 document delete

The library avoids unnecessary writes — new sessions that remain empty are never persisted.

Default encoder

The default encoder is a Firestore-aware json.dumps that automatically converts datetime objects (including Firestore's DatetimeWithNanoseconds) to millisecond Unix timestamps. This prevents serialization errors when session data contains values that originated from Firestore queries. To override, pass a custom encoder.

Limitations

  • Session data must be JSON-serializable (or serializable by your custom encoder). Avoid storing large blobs; Firestore documents are limited to 1 MiB.
  • Eventual consistency: Firestore in Datastore mode uses eventual consistency for some queries. This library reads by document ID (strongly consistent in both Native and Datastore mode).
  • No built-in encryption: Firestore encrypts data at rest (Google-managed keys). If you need app-level encryption, provide a custom encoder/decoder pair that encrypts before writing and decrypts after reading.
  • Sensitive data: Avoid storing secrets (passwords, tokens) in sessions. Sessions are intended for user state, not credential storage.

Development

# Clone and install dev dependencies
git clone https://github.com/dcgudeman/aiohttp-session-firestore.git
cd aiohttp-session-firestore
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"

# Run checks
ruff check .
ruff format --check .
mypy aiohttp_session_firestore
pytest --cov

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp_session_firestore-0.1.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiohttp_session_firestore-0.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_session_firestore-0.1.0.tar.gz.

File metadata

File hashes

Hashes for aiohttp_session_firestore-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ccb6db5ce468a22add32124e023764d63d54ef1087e284131eb824de75c470d9
MD5 479a5e68bb4f8e2f63724fb175d09558
BLAKE2b-256 5ef05a868bb834824488dd4c0b71f288297a46f509b773c25cd01631a0217b1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiohttp_session_firestore-0.1.0.tar.gz:

Publisher: release.yml on dcgudeman/aiohttp-session-firestore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aiohttp_session_firestore-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_session_firestore-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ceb2aafd34ffac0148caa082bc0f5b0223e79498e35f1619aa1b4d081284fab
MD5 3845be5a0c691f6c071d1960de51d9f9
BLAKE2b-256 3c0bec31c6133885532b124d0e2a1572a862000c26352b6c60217a04c9f5d719

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiohttp_session_firestore-0.1.0-py3-none-any.whl:

Publisher: release.yml on dcgudeman/aiohttp-session-firestore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page