Debian Codegraph @

docs_src/authentication_error_status_code/tutorial001_an_py310.py

raw
1 from typing import Annotated
2
3 from fastapi import Depends, FastAPI, HTTPException, status
4 from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
5
6 app = FastAPI()
7
8
9 class HTTPBearer403(HTTPBearer):
10 def make_not_authenticated_error(self) -> HTTPException:
11 return HTTPException(
12 status_code=status.HTTP_403_FORBIDDEN, detail="Not authenticated"
13 )
14
15
16 CredentialsDep = Annotated[HTTPAuthorizationCredentials, Depends(HTTPBearer403())]
17
18
19 @app.get("/me")
20 def read_me(credentials: CredentialsDep):
21 return {"message": "You are authenticated", "token": credentials.credentials}