from fastapi.openapi.models import OpenIdConnect as OpenIdConnectModel from fastapi.security.base import SecurityBase from starlette.exceptions import HTTPException from starlette.requests import Request from starlette.status import HTTP_403_FORBIDDEN class OpenIdConnect(SecurityBase): def __init__(self, *, openIdConnectUrl: str, scheme_name: str = None): self.model = OpenIdConnectModel(openIdConnectUrl=openIdConnectUrl) self.scheme_name = scheme_name or self.__class__.__name__ async def __call__(self, request: Request) -> str: authorization: str = request.headers.get("Authorization") if not authorization: raise HTTPException( status_code=HTTP_403_FORBIDDEN, detail="Not authenticated" ) return authorization