A simple FastAPI auth module with signup/signin routes
Project description
FastAPI Bearer Auth
A simple FastAPI auth module implementing OAuth2 with Password (and hashing), Bearer with JWT tokens, including user signup, signin routes.
Installing
Works on python3.6+
pip install fastapi_bearer_auth
Example of using
from fastapi import FastAPI
import fastapi_bearer_auth as fba
app = FastAPI(title='Test App')
app.include_router(fba.user_router, prefix='/user', tags=['User'])
# simple in-memory db
users = {}
@fba.handle_get_user_by_name
async def get_user_by_name(name):
return users.get(name)
@fba.handle_create_user
async def create_user(username, password):
if await get_user_by_name(username):
raise ValueError('Username {} exists'.format(username))
user = {
'username': username,
'password': await fba.call_config('get_password_hash', password),
}
users[username] = user
return user
Now head to http://127.0.0.1:8000/docs to test the API.
There's a simple command to achive this without writing any code:
uvicorn fastapi_bearer_auth.test:app
Customize
In addition to get_user_by_name(name)
and create_user(username, password)
, there're other functions can be override (with handle_
prefix):
authenticate(username, password)
verify_password(plain_password, hashed_password)
get_password_hash(password)
You can call all those functions with fba.call_config(name, *args, **kwargs)
.
Also some params:
ACCESS_TOKEN_EXPIRE_MINUTES
ALGORITHM
SECRET_KEY
Use something like fba.set_config({'SECRET_KEY': 'xxx', ...})
to change it.
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
Hashes for fastapi_bearer_auth-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | de9055b6d0f01c933c8b512bc1a036158e8d9432ce26a7d65ecaec1f572fd67e |
|
MD5 | 4fc379df0aef4e098a17a76784a8dc8d |
|
BLAKE2b-256 | e0bdd0f94b89cb15d5fe570fc2b50decbb45b7a0c3c9d8b37625c058da4d60d9 |
Hashes for fastapi_bearer_auth-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 624d7f9844806e9883ef1ee05d007f6e7a6f658587da6e6c3601f29bbe3c6661 |
|
MD5 | 058fe5f11cbfd1d23c603e3c30159308 |
|
BLAKE2b-256 | 6127af0ac99aaa2d9b64a78335615c1f17363f353a768a9e5da5475a59d5b872 |