fastapi_auth2
API key based Authentication package for FastAPI, focused on simplicity and ease of use:
- Full functionality out of the box, no configuration required
- API key security with local
sqliteorpostgresdatabase backend, working with both header and query parameters - Default 15 days deprecation for generated API keys
- Key creation, revocation, renewing, and usage logs handled through administrator endpoints
- Username, Email address and password(hashing + salting) verification features.
- Lightweight, minimal dependencies required.
Updates
- Added partial support for mongodb database backend.
- Added support for environment variables through .env files.
- Added
example.envfile to show how to use environment variables. - Updated
README.mdto reflect changes. - Updated documentation to reflect changes.
- Working on adding support for
mysqldatabase backend.
Installation
pip install fastapi_auth2
Usage
Creating an application
from fastapi import Depends, FastAPI
from fastapi_auth import api_key_router, api_key_security
app = FastAPI(
description="FastAPI Auth is a package that provides authentication based API security with FastAPI and Postgres Database, SQLite Database or MongoDB Database",
title="FastAPI Auth Example",
version=1.0,
)
app.include_router(api_key_router, prefix="/auth", tags=["_auth"])
@app.get("/unsecure")
async def unsecure_endpoint():
return {"message": "This is a unsecure endpoint"}
@app.get("/secure", dependencies=[Depends(api_key_security)])
async def secure_endpoint():
return {"message": "This is a secure endpoint"}
Resulting app is:
API key creation through docs
Start your API and check the logs for the automatically generated secret key if you did not provide one through environment variables.
Go to /docs on your API and inform this secret key in the Authorize/Secret header box.
All the administrator endpoints only support header security to make sure the secret key is not inadvertently
shared when sharing an URL.
Then, you can use /auth/new to generate a new API key.
And finally, you can use this API key to access the secure endpoint.
API key creation in python
You can of course automate API key acquisition through python with requests and directly querying the endpoints.
If you do so, you can hide the endpoints from your API documentation with the environment variable
FASTAPI_AUTH_HIDE_DOCS.
Configuration
Environment variables:
-
FASTAPI_AUTH_SECRET: Secret administrator key- Generated automatically on server startup if not provided
- Allows generation of new API keys, revoking of existing ones, and API key usage view
- It being compromised compromises the security of the API
-
FASTAPI_AUTH_HIDE_DOCS: Whether or not to hide the API key related endpoints from the documentation -
FASTAPI_AUTH_DB_LOCATION: Location of the local sqlite database filesqlite.dbin the running directory by default- When running the app inside Docker, use a bind mount for persistence
-
FASTAPI_AUTH_AUTOMATIC_EXPIRATION: Duration, in days, until an API key is deemed expired- 15 days by default
-
DATABASE_MODE: If set topostgres, the package will use a postgres database instead of sqlite -
URI: Location of the postgres databasepostgresql://postgres:postgres@localhost:5432/postgresby default- Only used if
DEV_MODEis set toFalse
Contributing
See CONTIBUTING.md for more information.
Setting up python environment
poetry install
poetry shell
Setting up pre-commit hooks
pre-commit install
Running tests
pytest
Running the dev environment
The attached docker image runs a test app on localhost:8080 with secret key TEST_SECRET. Run it with:
docker-compose build && docker-compose up
TODO
- Add more tests
- Add more database backends
- Add more authentication methods
Metadata
Release files for fastapi-auth2 1.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_auth2-1.0.6.tar.gz | 16.7 kB | Without provenance | Details for fastapi_auth2-1.0.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_auth2-1.0.6-py3-none-any.whl | Python 3 | none | any | Details for fastapi_auth2-1.0.6-py3-none-any.whl |
Total release size: 38.2 kB
Release files / fastapi_auth2-1.0.6.tar.gz
| Download URL | fastapi_auth2-1.0.6.tar.gz |
|---|---|
| Size | 16.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
417bf5e6ccb911ae40b4da257af34303333275fdb6499ff48129ca24467d1fd8
|
|
BLAKE2b-256 checksum How to use checksums |
0359e0027edf7557bb765db077212e2dfad0597ad9cf149fb1a9a2e3c54db343
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.10
|
Release files / fastapi_auth2-1.0.6-py3-none-any.whl
| Download URL | fastapi_auth2-1.0.6-py3-none-any.whl |
|---|---|
| Size | 21.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
66567ab1349993b072adf2d110b9a116f65d73a444d06d436488affbbe612513
|
|
BLAKE2b-256 checksum How to use checksums |
9834a66583e07a34ae9a1d4889c3e15841268b41b98aef514156d427690ceb60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.10
|
Security for fastapi-auth2 1.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.