A session middleware for Starlette and FastAPI
Project description
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()
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
Built Distribution
File details
Details for the file fastsession-0.2.0.tar.gz
.
File metadata
- Download URL: fastsession-0.2.0.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80ae4fe22f75d460b5a79199d8ed26eee6f208cad90e2a2388a2f5055fc16a79 |
|
MD5 | 22aa4b218f5864b48eac4a7481491514 |
|
BLAKE2b-256 | f5876b67ff4b600b2b3ff82bf0112df4a8b7a569686e4aa90a0da0450400a995 |
File details
Details for the file fastsession-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: fastsession-0.2.0-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52309b6a81e84011328c85f0be46825a22ab2005ab40fe31e85d589adaadcb8e |
|
MD5 | e532adb14c38f92940ad9f828b2a0ac2 |
|
BLAKE2b-256 | 601bc8bab56b53568224ac9b596198f747045b9af6c60deda4a90db6af7a5c7e |