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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fast_auth_tools-0.2.4.tar.gz.
File metadata
- Download URL: fast_auth_tools-0.2.4.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa3e14743a1a6043f43355c1a8003f90d18bd180c1f8d6da6d85de91eafdb80b
|
|
| MD5 |
92fd26a5131f9d0fdb5f98b37c03d7fe
|
|
| BLAKE2b-256 |
a9fc14704f9b8a6133b4594bc42b057b68ce6f9d2e80fd96530afbae6f2d9643
|
File details
Details for the file fast_auth_tools-0.2.4-py3-none-any.whl.
File metadata
- Download URL: fast_auth_tools-0.2.4-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3644e1391622022b40ba75bf48a6bafcef2b6c92cc946436c680ab470058d599
|
|
| MD5 |
f1ea79f967b0b44c993be82415b216c9
|
|
| BLAKE2b-256 |
5941b6cb80aa4d5efd23efba5ee89311297e38135b23dfa091578a328432ba31
|