fastapi-enciphers
Encrypted session middleware for FastAPI using enciphers.
Replaces Starlette's default signed cookie session with a fully encrypted one.
Version 2.0 note: updated to match
enciphers2.0's AEAD rewrite. Thestepparameter no longer exists — see CHANGELOG.md for exactly what changed and why.
Installation
pip install fastapi-enciphers
Usage
from fastapi import FastAPI, Request
from fastapi_enciphers import EnciphersMiddleware
from enciphers import Backend
app = FastAPI()
app.add_middleware(EnciphersMiddleware, backend=Backend.AES256_GCM, key=42)
@app.get("/login")
async def login(request: Request):
request.session["user_id"] = 1
return {"status": "logged in"}
@app.get("/profile")
async def profile(request: Request):
return {"user_id": request.session.get("user_id")}
Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
backend |
Backend |
Backend.AES256_GCM |
Backend.AES256_GCM or Backend.XCHACHA20_POLY1305 |
key |
int |
random | Secret key, a 128-bit value |
key_env |
str |
None | Environment variable for key |
session_cookie |
str |
"session" |
Cookie name |
max_age |
int |
1209600 | Cookie lifetime in seconds |
path |
str |
"/" |
Cookie path |
same_site |
str |
"lax" |
SameSite flag |
https_only |
bool |
False |
Secure flag |
domain |
str |
None | Cookie domain |
If
keyis not provided, a random 128-bit value is generated at startup — fine for local development, but every process in a real deployment needs to share the same key, or sessions won't be portable between them.
Warning: Do not use
EnciphersMiddlewaretogether with Starlette'sSessionMiddleware.
Session expiry
Whenever max_age is set (the default), every session token also
carries that same expiry bound inside the encrypted data itself, not
just in the cookie's own Max-Age attribute — a copy of the cookie
can't be replayed past that point even if a client ignores its
expiration. Setting max_age=None removes both.
License
Apache-2.0 — Copyright 2026 Mejlad Alsubaie
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_enciphers-2.0.0.tar.gz.
File metadata
- Download URL: fastapi_enciphers-2.0.0.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ac2b4b4da51da53abfa49d33af56e063051c3a92392c260e8c20c6e279f7138
|
|
| MD5 |
228f56533eb8f2aa8f23114d98479d81
|
|
| BLAKE2b-256 |
2dbe45fd3480fa3af288d779d855f004ae22818ccb110ffb72b0be9b11224512
|
Provenance
The following attestation bundles were made for fastapi_enciphers-2.0.0.tar.gz:
Publisher:
publish.yml on mjlad/fastapi-enciphers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_enciphers-2.0.0.tar.gz -
Subject digest:
4ac2b4b4da51da53abfa49d33af56e063051c3a92392c260e8c20c6e279f7138 - Sigstore transparency entry: 2333255249
- Sigstore integration time:
-
Permalink:
mjlad/fastapi-enciphers@2e46c0b0bb8042a607e5c76ee7a693f66f1efd11 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/mjlad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2e46c0b0bb8042a607e5c76ee7a693f66f1efd11 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_enciphers-2.0.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_enciphers-2.0.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f97fba2f89d1b5e76faa0763908a5f1cc413100e45f4d52897df10d9845920fd
|
|
| MD5 |
714a89b19cfbbde9b41a652dc66e979e
|
|
| BLAKE2b-256 |
d179f178e79a90299e498fa6f18759e3b25406276728eb9c6b6729c6420105d0
|
Provenance
The following attestation bundles were made for fastapi_enciphers-2.0.0-py3-none-any.whl:
Publisher:
publish.yml on mjlad/fastapi-enciphers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_enciphers-2.0.0-py3-none-any.whl -
Subject digest:
f97fba2f89d1b5e76faa0763908a5f1cc413100e45f4d52897df10d9845920fd - Sigstore transparency entry: 2333255275
- Sigstore integration time:
-
Permalink:
mjlad/fastapi-enciphers@2e46c0b0bb8042a607e5c76ee7a693f66f1efd11 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/mjlad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2e46c0b0bb8042a607e5c76ee7a693f66f1efd11 -
Trigger Event:
push
-
Statement type: