Simple Authentication Library for FastAPI
Project description
Fast Auth Tools (for FastAPI)
Add 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)
@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
Release history Release notifications | RSS feed
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.1.tar.gz
(6.2 kB
view details)
File details
Details for the file fast-auth-tools-0.2.1.tar.gz
.
File metadata
- Download URL: fast-auth-tools-0.2.1.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0262ec0de356ae06b630fd193dcab75e5ed5a263a57e16fea5ca2a3c774a3fb9 |
|
MD5 | b94e97592d9a4400c54dcf78e8ce9f95 |
|
BLAKE2b-256 | 8df65dcda4a814c19d6a8a44ece641b3005375bc38bc8486bcde0645f2884795 |