FastSession
FastSession is a session management library for FastAPI.
It provides a middleware, FastSessionMiddleware,
that helps you manage user sessions effectively in your FastAPI applications.
Features
Only the session ID is stored as a browser cookie. (Similar to Java Servlet and Node.js express session)
The session ID can be shared only through signed and confidential communication channels, and since no session contents are stored in the browser, an extremely secure session system can be built.
- Session ID generation and session data storage.
- Session cookie management with signature verification for enhanced security.
- In-memory store for session data enabled.
Installation
Use the package manager PIP to install FastSession.
pip install fastsession
Usage
Here is a basic usage example:
import uvicorn
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastsession import FastSessionMiddleware, MemoryStore
HOST = 'localhost'
PORT = 18080
app = FastAPI()
app.add_middleware(FastSessionMiddleware,
secret_key="my-secret-key", # Key for cookie signature
store=MemoryStore(), # Store for session saving
http_only=True, # True: Cookie cannot be accessed from client-side scripts such as JavaScript
secure=False, # True: Requires Https
max_age=0,
# When 0 is specified, it is only effective while the browser is active. If a value greater than 0 is specified, the session will continue for the specified time even after closing the browser
session_cookie="sid", # Name of the session cookie
session_object="session" # Attribute name of the Session manager under request.state
)
@app.get("/session_test")
async def session_test(request: Request):
# get session manager
session_mgr = request.state.session
# get session store (dictionary)
session = session_mgr.get_session()
# get session id
session_id = session_mgr.get_session_id()
print(f"sessionID:{session_id}")
if "test_counter" not in session:
session["test_counter"] = 0
session["test_counter"] += 1
return {"test_counter": session['test_counter']}
app.mount("/", StaticFiles(directory="html", html=True), name="public")
def start_server():
uvicorn.run(app, host=HOST, port=PORT)
def main():
start_server()
if __name__ == "__main__":
main()
Release files for fastsession 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastsession-0.3.0.tar.gz | 15.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastsession-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.4 kB
Release files / fastsession-0.3.0.tar.gz
| Download URL | fastsession-0.3.0.tar.gz |
|---|---|
| Size | 15.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba3542d367e411875d187edfb82837729874382188824ce0c8bdad5adf8dfdde
|
|
BLAKE2b-256 checksum How to use checksums |
49372a99bcb66249dc552e7e7ce0c8c6cae7d8b4b8146e0c2d4ab0e826b37b53
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.9
|
Release files / fastsession-0.3.0-py3-none-any.whl
| Download URL | fastsession-0.3.0-py3-none-any.whl |
|---|---|
| Size | 13.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2b288d9d23c8cd51bf8103c406bc1f137615f73ecf3b7e4975d4c1648d8362d8
|
|
BLAKE2b-256 checksum How to use checksums |
16fb2d7f16257a8ae017185826391ee6c56b48877b361b95fa1cd9360ee5183d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.9
|