Async Auth Middleware for FastAPI/Starlette
Project description
auth-middleware
Async Auth Middleware for FastAPI/Starlette.
Installation
Using pip:
pip install auth-middleware
How to use it
Auth Middleware follows the middleware protocol and, therefore, should be added as a middleware to your FastApi or Starlette application.
The steps, using FastAPI:
from fastapi import FastAPI, Depends
from starlette.requests import Request
from starlette.responses import Response
# Step 1: import the functions to control authentication
from auth_middleware.functions import require_groups, require_user
# Step 2: import the Middleware to use
from auth_middleware.jwt_auth_middleware import JwtAuthMiddleware
# Step 3: import the auth provider
from auth_middleware.providers.cognito import CognitoProvider
app: FastAPI = FastAPI()
# Step 4: Add Middleware with a Cognito auth Provider
app.add_middleware(JwtAuthMiddleware, auth_provider=CognitoProvider())
@app.get("/",
dependencies=[
# Step 5: add the authorization dependencies you want: require_user or requiere_groups
# Depends(require_groups(["customer", "administrator"])),
Depends(require_user()),
],)
async def root(request: Request):
# Step 6: user information will be available in the request.state.current_user object
return {"message": f"Hello {request.state.current_user.name}"}
Then set the environment variables (or your .env file)
AWS_COGNITO_USER_POOL_ID=your_cognito_user_pool_id
AWS_COGNITO_USER_POOL_REGION=your_cognito_user_pool_region
Call the method sending the id_token provided by Cognito:
curl -X GET http://localhost:8000/ -H "Authorization: Bearer MY_ID_TOKEN"
Middleware configuration
TODO
The User property
TODO
Control authentication and authorization
TODO
Authentication providers
Amazon Cognito
TODO
Azure Entra ID
TODO
Google Idp
TODO
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
auth_middleware-0.1.1.tar.gz
(9.0 kB
view details)
Built Distribution
File details
Details for the file auth_middleware-0.1.1.tar.gz
.
File metadata
- Download URL: auth_middleware-0.1.1.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.2 Linux/6.5.0-1017-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4a64be51ab8980577e9a243693e5b1942585b29a20c637d71db2880908f1f72 |
|
MD5 | 2c517b17e4b68f7a75d5ebfa63fa5d79 |
|
BLAKE2b-256 | cfe65a7edf5ce851b7d9d729d4010d8f3ada0e18a1167e87de73594ac62501d7 |
File details
Details for the file auth_middleware-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: auth_middleware-0.1.1-py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.2 Linux/6.5.0-1017-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 754ca4978756b2c9be57cfd70b88ec2cc95ab1f97106bd9ddb9cdaaed8224205 |
|
MD5 | e496cf6b12a7a190673b39b936491164 |
|
BLAKE2b-256 | 77a12cb71c0eb08f785159e42822b0c330aa8e574c51fea04187d630c5770843 |