Authx
Ready-to-use and customizable Authentications and Oauth2 management for FastAPI ⚡
--- | Project | Status | |---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | CI | [ ![ CI ]( https://github.com/yezz123/authx/actions/workflows/ci.yml/badge.svg ) ]( https://github.com/yezz123/authx/actions/workflows/ci.yml ) [ ![ pre-commit.ci status ]( https://results.pre-commit.ci/badge/github/yezz123/authx/main.svg ) ]( https://results.pre-commit.ci/latest/github/yezz123/authx/main ) [ ![ Codecov ]( https://codecov.io/gh/yezz123/authx/branch/main/graph/badge.svg ) ]( https://codecov.io/gh/yezz123/authx ) | | Meta | [ ![ Package version ]( https://img.shields.io/pypi/v/authx?color=%2334D058&label=pypi%20package ) ]( https://pypi.org/project/authx ) [ ![ Downloads ]( https://static.pepy.tech/badge/authx ) ]( https://pepy.tech/project/authx ) [ ![ Pydantic Version 2 ]( https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json ) ]( https://pydantic.dev ) [ ![ Ruff ]( https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json ) ]( https://github.com/astral-sh/ruff ) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=yezz123_authx&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=yezz123_authx) | --- **Source Code**:from fastapi import FastAPI, Depends, HTTPException
from authx import AuthX, AuthXConfig, RequestToken
app = FastAPI()
config = AuthXConfig(
JWT_ALGORITHM = "HS256",
JWT_SECRET_KEY = "SECRET_KEY",
JWT_TOKEN_LOCATION = ["headers"],
)
auth = AuthX(config=config)
auth.handle_errors(app)
@app.get('/login')
def login(username: str, password: str):
if username == "xyz" and password == "xyz":
token = auth.create_access_token(uid=username)
return {"access_token": token}
raise HTTPException(401, detail={"message": "Invalid credentials"})
@app.get("/protected", dependencies=[Depends(auth.get_token_from_request)])
def get_protected(token: RequestToken = Depends()):
try:
auth.verify_token(token=token)
return {"message": "Hello world !"}
except Exception as e:
raise HTTPException(401, detail={"message": str(e)}) from e