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.0.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.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fast_auth_tools-0.3.0.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.0.tar.gz
Algorithm Hash digest
SHA256 cd00fd22a452802a24c3bf6d74358525ebe266ec55b0d70e106748b6a3fc29b9
MD5 a516af0ce378903454f59a0ae980853b
BLAKE2b-256 c2a9301834d15c2459244d661ed97c7bd4b488775d3d56c6cf018cbdd17e35e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_auth_tools-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 688ce9b07efc7ed2ce42c6e413f359786604d24a26a293a394e5de79d8740ea9
MD5 a14e47b34b59ce51f9e275e82f3724af
BLAKE2b-256 afa87b9a3551b718e6f15e97cf3dc5d69d2d303fdeb8574ad9b4c73612e34be3

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