Skip to main content

Simple Authentication Library for FastAPI

Project description

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)

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

fast_auth_tools-0.3.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fast_auth_tools-0.3.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file fast_auth_tools-0.3.1.tar.gz.

File metadata

  • Download URL: fast_auth_tools-0.3.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for fast_auth_tools-0.3.1.tar.gz
Algorithm Hash digest
SHA256 f1406bf0800bb7f44044d412a4fb637b08d7ff6db60c15b8ea964a825a472906
MD5 d848f3811774dd520c9fbbc2bf2fbf3c
BLAKE2b-256 031332335027391385ea4a0aae44d4b5ed001e8f5ddc04724d50be5acba59f78

See more details on using hashes here.

File details

Details for the file fast_auth_tools-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fast_auth_tools-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc042fb2a5dab1d56011ad76b503b7a1ecb98d913337d83100fcd98e2402efeb
MD5 ccab751badda1fb165955a7d87646579
BLAKE2b-256 a06eb08388e25a14bcd88a09f7b805d87b048a6513df01f112560be6ddef876d

See more details on using hashes here.

Supported by

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