Skip to main content

FastAPI simple security

codecov Python Tests Linting

Ruff Code style: black pre-commit enabled Conventional Commits

API key based security package for FastAPI, focused on simplicity of use:

  • Full functionality out of the box, no configuration required
  • API key security with local sqlite 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
  • No dependencies, only requiring FastAPI and the python standard library

This module cannot be used for any kind of distributed deployment. It's goal is to help have some basic security features for simple one-server API deployments, mostly during development.

Installation

pip install fastapi_simple_security

Usage

Creating an application

from fastapi_simple_security import api_key_router, api_key_security
from fastapi import Depends, FastAPI

app = FastAPI()

app.include_router(api_key_router, prefix="/auth", tags=["_auth"])

@app.get("/secure", dependencies=[Depends(api_key_security)])
async def secure_endpoint():
    return {"message": "This is a secure endpoint"}

Resulting app is:

app

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.

secret

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.

secret_header

Then, you can use /auth/new to generate a new API key.

api key

And finally, you can use this API key to access the secure endpoint.

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_SIMPLE_SECURITY_HIDE_DOCS.

Configuration

Environment variables:

  • FASTAPI_SIMPLE_SECURITY_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_SIMPLE_SECURITY_HIDE_DOCS: Whether or not to hide the API key related endpoints from the documentation

  • FASTAPI_SIMPLE_SECURITY_DB_LOCATION: Location of the local sqlite database file

    • sqlite.db in the running directory by default
    • When running the app inside Docker, use a bind mount for persistence
  • FAST_API_SIMPLE_SECURITY_AUTOMATIC_EXPIRATION: Duration, in days, until an API key is deemed expired

    • 15 days by default

Contributing

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

Needed contributions

  • More options with sensible defaults
  • Logging per API key?
  • More back-end options for API key storage?

Release files for fastapi-simple-security 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-simple-security 1.3.0
File Size Uploaded
fastapi_simple_security-1.3.0.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-simple-security 1.3.0
File Interpreter ABI Platform
fastapi_simple_security-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.3 kB

Release files / fastapi_simple_security-1.3.0.tar.gz

Download URL fastapi_simple_security-1.3.0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
dbed69809665b3cb0097bf359064f3822aadab6dd2987d907e26efe5f84b836b
BLAKE2b-256 checksum
How to use checksums
bb14a0ae1aa7d0e2ea4ac4c91142c7cb0e13c2aeec755f984c4a698690389186
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.10.11 Darwin/22.5.0

Release files / fastapi_simple_security-1.3.0-py3-none-any.whl

Download URL fastapi_simple_security-1.3.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e8ac855fad3bb6c0fe02464f03e0a5d70d12ed4d48bb1c8010b96348c89a43c2
BLAKE2b-256 checksum
How to use checksums
2b1cff43a73c41fff01b07943f06f5bf09af76c9ccd87e4584fa50083392772a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.10.11 Darwin/22.5.0

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2

2 release files

1.1

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page