Enhanced and flexible ASGI session middleware for Starlette applications
Project description
Starlette Session Middleware ·

Purpose
The purpose of this project is to provide an enhanced, more flexible ASGI session middleware
Getting started
In the project root, you will want to create and activate a Python virtual environment in a folder called .venv.
On Fedora 38 this can be done by running python3.9 -m venv .venv && source .venv/bin/activate. You will then want to
pip install all of the dependencies for local development. This can be done by running pip install -r requirements.txt
in your Python 3.9 virtual environment. After that, you will want to run pre-commit install to install all of the
pre-commit hooks. This ensures that we reduce unneeded pipeline failures.
Running the Tests
You can run the tests by running python -m pytest -n auto --cov. This will use pytest-xdist to parallelize the tests and provide a code
coverage report by using pytest-cov.
Usage
FastAPI
In order to use a JWT persisted with a cookie and passed through a cookie, you would create the middleware as follows.
from fastapi import FastAPI
from starlette_session.middleware import SessionMiddleware
from starlette_session.middleware.codecbackends.jwt import JwtCodecBackend
from starlette_session.middleware.storagebackends.cookie import CookieStorageBackend
from starlette_session.middleware.authorizationbackends.cookie import CookieAuthorizationBackend
app = FastAPI()
app.add_middleware(
SessionMiddleware,
codec_backend=JwtCodecBackend(key="somesuperdupersecurekey")
storage_backend=CookieStorageBackend()
authorization_backend=CookieAuthorizationBackend()
)
In order to use a JWT persisted with a cookie and passed through the Authorization header, you would create the middleware as follows.
from fastapi import FastAPI
from starlette_session.middleware import SessionMiddleware
from starlette_session.middleware.codecbackends.jwt import JwtCodecBackend
from starlette_session.middleware.storagebackends.cookie import CookieStorageBackend
from starlette_session.middleware.authorizationbackends.authorizationheader import AuthorizationHeaderAuthorizationBackend
app = FastAPI()
app.add_middleware(
SessionMiddleware,
codec_backend=JwtCodecBackend(key="somesuperdupersecurekey")
storage_backend=CookieStorageBackend()
authorization_backend=AuthorizationHeaderAuthorizationBackend()
)
Project details
Release history Release notifications | RSS feed
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 starlette_session_middleware-0.1.6.tar.gz.
File metadata
- Download URL: starlette_session_middleware-0.1.6.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cc5c14da6e1bd3a5de158b038a5cb13f3a1c1291a9415d961578ba3d5c6ba93
|
|
| MD5 |
c4ea1f62c7c9c2532c0553ed299da60c
|
|
| BLAKE2b-256 |
6e25f5f8bf823af9f1bbdceedf55ecccc2021057cf3f532aaa04e8c024a42f6e
|
Provenance
The following attestation bundles were made for starlette_session_middleware-0.1.6.tar.gz:
Publisher:
ci-cd.yml on lannuttia/starlette-session-middleware
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
starlette_session_middleware-0.1.6.tar.gz -
Subject digest:
8cc5c14da6e1bd3a5de158b038a5cb13f3a1c1291a9415d961578ba3d5c6ba93 - Sigstore transparency entry: 796096818
- Sigstore integration time:
-
Permalink:
lannuttia/starlette-session-middleware@a896f510b0cd4b52e1dd74dba6e48a149289d3d0 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/lannuttia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci-cd.yml@a896f510b0cd4b52e1dd74dba6e48a149289d3d0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file starlette_session_middleware-0.1.6-py3-none-any.whl.
File metadata
- Download URL: starlette_session_middleware-0.1.6-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8315901cb5ea79a3195767247a934bf98a931a360c8559a9dd74014db7ba0e9
|
|
| MD5 |
3fa9f6faed3ca88109e63d954cd02c80
|
|
| BLAKE2b-256 |
37592da284ad68fffa161f36db49fdb70414a6acd7a2c63b3c053bff2dd7fbbb
|
Provenance
The following attestation bundles were made for starlette_session_middleware-0.1.6-py3-none-any.whl:
Publisher:
ci-cd.yml on lannuttia/starlette-session-middleware
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
starlette_session_middleware-0.1.6-py3-none-any.whl -
Subject digest:
e8315901cb5ea79a3195767247a934bf98a931a360c8559a9dd74014db7ba0e9 - Sigstore transparency entry: 796096833
- Sigstore integration time:
-
Permalink:
lannuttia/starlette-session-middleware@a896f510b0cd4b52e1dd74dba6e48a149289d3d0 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/lannuttia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci-cd.yml@a896f510b0cd4b52e1dd74dba6e48a149289d3d0 -
Trigger Event:
push
-
Statement type: