Giter VIP home page Giter VIP logo

fastapi-key-auth's Introduction


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 - [email protected]
Project Link: https://github.com/iwpnd/fastapi-key-auth

fastapi-key-auth's People

Contributors

dependabot[bot] avatar francbartoli avatar iwpnd avatar mendylanda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fastapi-key-auth's Issues

Error when installing with FastAPI 0.71 & Starlette 0.17.1

Descroption:
When using pip to install the current version of FastAPI-Key-Auth install fails with the following error message:

ERROR: fastapi-key-auth 0.7.0 has requirement fastapi<0.71.0,>=0.70.0, but you'll have fastapi 0.71.0 which is incompatible.
ERROR: fastapi-key-auth 0.7.0 has requirement starlette<0.17.0,>=0.16.0, but you'll have starlette 0.17.1 which is incompatible.

suggested Fix
Update dependency to allow for newer version

Cannot install with recent FastAPI

I'm currently getting an issue with installing with the lastest FastAPI. This looks to be very similar to #60

I see that Dependabot has already updated the dependencies. Any chance you could roll a new release? TIA.

ERROR: Cannot install -r requirements.txt (line 4) and fastapi==0.75.2 because these package versions have conflicting dependencies.

The conflict is caused by:
    The user requested fastapi==0.75.2
    fastapi-key-auth 0.7.1 depends on fastapi<0.72.0 and >=0.71.0

To fix this you could try to:
1. loosen the range of package versions you've specified
2. remove package versions to allow pip attempt to solve the dependency conflict

Where do I put the API KEY ?

Where in the project do i put the correct API KEY?

http post http://localhost:8001/vehicle x-api-key:test

Where in the project do i put 'test'?

➜  ~ echo '{ "data": { "type": "Feature", "geometry": {"type": "Point", "coordinates": [13.37, 52.25]}, "properties": {"id": "truck"}}}' \
      | http post http://localhost:8000/vehicle x-api-key:test
HTTP/1.1 401 Unauthorized
content-length: 25
content-type: application/json
date: Mon, 27 Jun 2022 00:55:07 GMT
server: uvicorn

{
    "detail": "unauthorized"
}

I dont know how to get the header validated, where do i put the word 'test'?

Doesn't install with recent FastAPI

Another FastAPI release means the starlette dependency is incompatible again:

#0 45.50 ERROR: Cannot install -r requirements.txt (line 3) and -r requirements.txt (line 4) because these package versions have conflicting dependencies.
#0 45.50 
#0 45.50 The conflict is caused by:
#0 45.50     fastapi 0.95.0 depends on starlette<0.27.0 and >=0.26.1
#0 45.50     fastapi-key-auth 0.10.1 depends on starlette<0.26.0 and >=0.25.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.