Skip to main content

A must have if you are using Auth0 with FastAPI

Project description

Auth0 FastAPI

This library provides a simple and elegant way to validate Auth0 tokens in your API and keep your endpoints protected.

Benefits of using this library:

  • Token validation, ensure the access tokens are properly structured, and signed.
  • Support of scopes for permission based authorization.
  • Automatic swagger UI support, just enter your token and test your endpoints.
  • Super easy to use.

Installation

pip install auth0_fastapi

Example usage

from fastapi import FastAPI, Depends, Security
from auth0_fastapi import Auth0JWTBearerTokenValidator

auth = Auth0JWTBearerTokenValidator(
    domain="<your-auth0-domain>",
    audience="<your-auth0-api-audience>",
    issuer="<your-auth0-issuer>"
)

app = FastAPI()

@app.get("/public")
def get_public():
    return {"message": "Anyone can access"}

@app.get("/protected", dependencies=[Security(auth.get_authenticated_user)])
def get_protected():
    return {"message": "Any authenticated user can access"}


@app.get("/secured", dependencies=[Security(auth.get_authenticated_user, scopes=['read:secure'])])
def get_secured():
    return {"message": "Only users with permission 'read:secure' can access"}

@app.get("/get-token-payload")
def get_token_payload(token_payload=Security(auth.get_authenticated_user)):
    return {"message": token_payload}

Disclaimer

This is not an official plugin, it is however used in production by my own applications and follows best practices.

Please report any issues you encounter, happy coding!

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

auth0_fastapi-0.0.3.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

auth0_fastapi-0.0.3-py3-none-any.whl (4.3 kB view hashes)

Uploaded Python 3

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