Skip to main content

Authava FastAPI Client

The Authava FastAPI client provides seamless integration with FastAPI applications, making it easy to add authentication to your Python applications.

GitHub Repository

The FastAPI client is available on GitHub at:

https://github.com/authava/fastapi

Installation

Install the Authava FastAPI client using pip:

pip install authava-fastapi

Configuration

Configure the Authava FastAPI client in your application:

from fastapi import FastAPI
from authava.fastapi import AuthavaAuth

app = FastAPI()

auth = AuthavaAuth(
domain="auth.yourdomain.com",
api_audience="your-api-audience",
issuer="https://auth.yourdomain.com/",
algorithms=["RS256"],
)

Basic Usage

Use the Authava authentication in your FastAPI routes:

from fastapi import FastAPI, Depends, HTTPException
from authava.fastapi import AuthavaAuth, get_current_user

app = FastAPI()

auth = AuthavaAuth(
domain="auth.yourdomain.com",
api_audience="your-api-audience",
issuer="https://auth.yourdomain.com/",
algorithms=["RS256"],
)

@app.get("/api/public")
def public_endpoint():
return {"message": "This is a public endpoint"}

@app.get("/api/private")
def private_endpoint(user=Depends(auth.get_current_user)):
return {"message": "This is a private endpoint", "user": user}

@app.get("/api/admin")
def admin_endpoint(user=Depends(auth.get_current_user_with_roles(["admin"]))):
return {"message": "This is an admin endpoint", "user": user}

Features

  • Dependency Injection: Easy-to-use dependency injection for authentication
  • JWT Validation: Automatic validation of JWT tokens
  • Role-Based Access Control: Support for role-based authorization
  • Middleware: FastAPI middleware for protecting routes
  • Customizable Configuration: Flexible configuration options

Advanced Configuration

For more advanced configuration options and detailed documentation, please refer to the GitHub repository.