Skip to main content

Fast Auth Tools (for FastAPI)

Adds OAuth2 authentication to a FastAPI app with a single function

Install

pip install fast-auth-tools

Create User DB

fast-auth-user

If you are coming from v0.2.x you may need to run the db migration tool to update your user database:

fast-auth-migrate

Example

from fastapi import FastAPI, Depends

from fast_auth import fast_auth, logged_in_user, User


app = FastAPI()

fast_auth(app)  # adds /login and /refresh_token endpoints as well as setting CORS origins

# Example authenticated routes:
@app.get("/secure/get/", dependencies=[Depends(logged_in_user)])
async def must_be_logged_in():
    return {}

@app.post("/secure/post/")
async def get_user_object(user: User = Depends(logged_in_user)):
    print(f"password hash: {user.password}")
    return {
        "data": f"{user.username} is already logged in"
    }

Settings

name default description
cors_origins ["*"] allowed CORS origins
secret_key "SoMeThInG_-sUp3Rs3kREt!!" the key used to encrypt JWT
algorithm "HS256" the algorithm used to encrypt JWT
access_token_expire_days 5 the maximum number of days JWT will be valid
user_db_path ".../site_packages/fast_auth/users.sqlite3" the path to the sqlite database that holds username/encrypted password information
login_url "login" path to POST endpoint accepting username/password form data
token_refresh_url "refresh_token" path to GET endpoint that takes a valid JWT and returns a new JWT with maximum expiry

Loading Settings from a File

You can load initial settings from a YAML file by setting the SETTINGS_PATH environment variable to the path of your settings file. If the file is not found, or any of the settings are not included in the file, default settings will be used.

Example auth.yaml:

cors_origins:
  - "myapp.com"
  - "my-test-server.com"
secret_key: "your_secret_key"
algorithm: "HS256"
access_token_expire_days: 1
user_db_path: "./my_user_db.sqlite3"
login_url: "/auth/login"
token_refresh_url: "/auth/token"

Set the environment variable and run your application:

export SETTINGS_PATH=./auth.yaml
uvicorn main:app --reload

Setting Individual Settings Dynamically

If required, you can set individual settings from within your application:

from fastapi import FastAPI
from fast_auth import fast_auth, settings

app = FastAPI()

settings.user_db_path = "./my_user_db.sqlite3"
fast_auth(app)

Metadata

Release files for fast-auth-tools 0.3.1

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

Source distribution (sdist)

Source distribution for fast-auth-tools 0.3.1
File Size Uploaded
fast_auth_tools-0.3.1.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fast-auth-tools 0.3.1
File Interpreter ABI Platform
fast_auth_tools-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 18.0 kB

Release files / fast_auth_tools-0.3.1.tar.gz

Download URL fast_auth_tools-0.3.1.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f1406bf0800bb7f44044d412a4fb637b08d7ff6db60c15b8ea964a825a472906
BLAKE2b-256 checksum
How to use checksums
031332335027391385ea4a0aae44d4b5ed001e8f5ddc04724d50be5acba59f78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.6

Release files / fast_auth_tools-0.3.1-py3-none-any.whl

Download URL fast_auth_tools-0.3.1-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc042fb2a5dab1d56011ad76b503b7a1ecb98d913337d83100fcd98e2402efeb
BLAKE2b-256 checksum
How to use checksums
a06eb08388e25a14bcd88a09f7b805d87b048a6513df01f112560be6ddef876d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

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