Skip to main content

API key validation Middleware

Project description


FastAPI-key-auth

Secure your FastAPI endpoints using API keys.
Report Bug · Request Feature

Table of Contents

  1. About The Project
  2. Getting Started
  3. Usage
  4. License
  5. Contact

About The Project

On deployment inject API keys authorized to use your service. Every call to a private endpoint of your service has to include a header['x-api-key'] attribute that is validated against the API keys in your environment. If it is present, a request is authorized. If it is not FastAPI return 401 Unauthorized. Use this either as a middleware, or as Dependency.

Built With

Getting Started

Installation

  1. Clone and install
    git clone https://github.com/iwpnd/fastapi-key-auth.git
    poetry install
    
  2. Install with pip
    pip install fastapi-key-auth
    
  3. Install with poetry
    poetry add fastapi-key-auth
    

Usage

As Middleware:

from fastapi import FastAPI
from fastapi_key_auth import AuthorizerMiddleware

app = FastAPI()

app.add_middleware(AuthorizerMiddleware, public_paths=["/ping"], key_pattern="API_KEY_")

# optional use regex startswith
app.add_middleware(AuthorizerMiddleware, public_paths=["/ping", "^/users"])

As Dependency

from fastapi import FastAPI, Depends
from fastapi_key_auth import AuthorizerDependency

authorizer = AuthorizerDependency(key_pattern="API_KEY_")

# either globally or in a router
app = FastAPI(dependencies=[Depends(authorizer)])

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Benjamin Ramser - @imwithpanda - ahoi@iwpnd.pw
Project Link: https://github.com/iwpnd/fastapi-key-auth

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

fastapi_key_auth-0.15.4.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

fastapi_key_auth-0.15.4-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_key_auth-0.15.4.tar.gz.

File metadata

  • Download URL: fastapi_key_auth-0.15.4.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for fastapi_key_auth-0.15.4.tar.gz
Algorithm Hash digest
SHA256 772ba47706e1c52051f457714ca8978579cce078d257e4e1007d81aebeb1a84c
MD5 913b1b7a722ccbcadb46b87c2c34eecf
BLAKE2b-256 e3b25e3ff99886e97f2a5e03ba3820b7c1a58eb8f6a3916bedfa7c0b0845e63c

See more details on using hashes here.

File details

Details for the file fastapi_key_auth-0.15.4-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_key_auth-0.15.4-py3-none-any.whl
Algorithm Hash digest
SHA256 94c6ca4310389fc71073cd176d735a95295b54e878f9239699ac5d5ce48b0fa5
MD5 06365b62cbd79f7b0b1173220cb2d7ef
BLAKE2b-256 c9a6c622a09699e4b39a9f95aa6a1a99fb30be428afee2be59b5b2da4554afe3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page