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

python -m fast_auth.create_sqlite_db ./my_user_db.sqlite3

db path argument is optional and will default to .../site_packages/fast_auth/users.sqlite3

Example

import os

from fastapi import FastAPI, Depends

from fast_auth import fast_auth, logged_in_user, User
from fast_auth import settings as auth_settings

auth_settings.user_db_path = "./my_user_db.sqlite3" # if the path was changed
auth_settings.secret_key = os.getenv("FAST_AUTH_SECRET_KEY")
auth_settings.cors_origins = ["myapp.com", "my-test-server.com"]

app = FastAPI()
fast_auth(app)


# 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 alogrithm 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

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.2.3.tar.gz (6.9 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.2.3-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fast_auth_tools-0.2.3.tar.gz
  • Upload date:
  • Size: 6.9 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.2.3.tar.gz
Algorithm Hash digest
SHA256 590fb44c9ca278c1a5c6b9fd4290476012e5a4819ec61e2fd9b149775bfae25c
MD5 eee8ef4c2ea3ee140484be17bf70ec37
BLAKE2b-256 bc5abd01942f22b44b9429bd49b2f63aee58a53547e7b762aa6b7d678d0e0065

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_auth_tools-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b2e0861c8721193701461e9a0a0d24039fc26c625744665cce88baab0f40b3fd
MD5 325876fbda6ef606cffe6e77561278cb
BLAKE2b-256 30ba6114539aa21a5eada3148c2d0035c93115184ce1063757adbf61ced17afc

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