Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

fastapi-flask-auth

Lightweight FastAPI dependencies and authenticator that uses Flask session cookies for access control.

Why would you want to base your FastAPI application's authentication on session cookies created by a Flask application?

Well, imagine that you have a Flask application that handles authentication (probably with flask-login) among other tasks and you'd like to use FastAPI for some new routes, or maybe you just want to offload some work from Flask to FastAPI for convenience or performance reasons. In such a scenario, you probably don't want the client to authenticate at both server applications. What you can do instead is put both server applications behind a reverse proxy, let Flask handle authentication and do its job as before, and use Flask's session cookies for authentication in your FastAPI application with this library.

Installation & Usage

You can install the library from PyPI with pip install fastapi-flask-auth.

You will also need to install a Flask session decoder. If you're looking for a lightweight, zero-dependency decoder, give flask-session-decoder a try. You can do this manually with pip install flask-session-decoder or you can install fastapi-flask-auth together with its default decoder dependency simply with pip install fastapi-flask-auth[decoder].

With both fastapi-flask-auth and flask-session-decoder in place, you can set up the authenticator for your FastAPI application like this:

from fastapi_flask_auth import FlaskSessionAuthenticator
from flask_session_decoder import FlaskSessionDecoder

decoder = FlaskSessionDecoder(secret_key="the-secret-key-of-the-flask-app-that-created-the-cookie")
flask_auth = FlaskSessionAuthenticator(decoder=decoder)

Then, you can use the authenticator's FastAPI dependencies in your routes like this:

from fastapi import Depends, FastAPI

app = FastAPI()

@app.get("/get-session-cookie")
def get_session_cookie(cookie: dict | None = Depends(flask_auth.get_session_cookie)):
    ...

@app.get("/requires-session-cookie")
def requires_session_cookie(cookie: dict = Depends(flask_auth.requires_session_cookie)):
    ...

@app.get("/get-user-id")
def get_user_id(user_id: str | None = Depends(flask_auth.get_user_id)):
    ...

@app.get("/requires-session-cookie")
def requires_user_id(user_id: str = Depends(flask_auth.requires_user_id)):
    ...

Dependencies

The only dependency of this library is FastAPI.

The default decoder dependency is flask-session-decoder, which has no further dependencies.

Development

Use black for code formatting and mypy for static code analysis.

License - MIT

The library is open-sourced under the conditions of the MIT license.

Metadata

Release files for fastapi-flask-auth 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-flask-auth 0.1.0
File Size Uploaded
fastapi-flask-auth-0.1.0.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-flask-auth 0.1.0
File Interpreter ABI Platform
fastapi_flask_auth-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.9 kB

Release files / fastapi-flask-auth-0.1.0.tar.gz

Download URL fastapi-flask-auth-0.1.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
830077150415809fa5097cb1cfaa94e1677fd2f770ef02002a290001ead48f55
BLAKE2b-256 checksum
How to use checksums
da6059d4f8beca8187135a8f9d685e8d8a9eca1ebaf5e6290b1607a7bb03b905
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release files / fastapi_flask_auth-0.1.0-py3-none-any.whl

Download URL fastapi_flask_auth-0.1.0-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c6a5997c9de4d2586b84ab663ae723d8e81a63dbd44fc758c61f495daefce380
BLAKE2b-256 checksum
How to use checksums
352005a868ca9efc9220a75fbdf9651b2450c4cf31634cee24d1b6eff466db66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page