Authx
Ready-to-use and customizable Authentications and Oauth2 management for FastAPI ⚡
--- | Project | Status | |---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | CI | [  ]( https://github.com/yezz123/authx/actions/workflows/ci.yml ) [  ]( https://results.pre-commit.ci/latest/github/yezz123/authx/main ) [  ]( https://codecov.io/gh/yezz123/authx ) | | Meta | [  ]( https://pypi.org/project/authx ) [  ]( https://pepy.tech/project/authx ) [  ]( https://pydantic.dev ) [  ]( https://github.com/astral-sh/ruff ) [](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