Skip to main content

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

fastsession-0.1.2.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

fastsession-0.1.2-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file fastsession-0.1.2.tar.gz.

File metadata

  • Download URL: fastsession-0.1.2.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for fastsession-0.1.2.tar.gz
Algorithm Hash digest
SHA256 36f33caf27a6ab4889fd90100a1f5f0a013be12a0e8f3cba37a5417e4367b0b4
MD5 b8eadd78fde5599a7cc61e3bfe970ae7
BLAKE2b-256 57e497ee449e73fbf004c3c9a1f9efa5cfb5ccb302b737bf5103dbea0700c9ac

See more details on using hashes here.

File details

Details for the file fastsession-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fastsession-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for fastsession-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6b576e8b53f721b4b8de2f519423a3038eddcda04f14b6de537ee75ca133033d
MD5 9ad2c23e0cba8b6daf92bcbec884a4cf
BLAKE2b-256 89d1245345b1fcb5fb66f0f3265fd46a154ea889b19a25e58911a07b26cc2591

See more details on using hashes here.

Supported by

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