openid connect authentication class an instance of it would be used as a dependency the openid connect url security scheme name it will be included in the generated openapi e g visible at docs security scheme description it will be included in the generated openapi e g visible at docs by default if no http auhtorization header is provided required for openid connect authentication it will automatically cancel the request and send the client an error if autoerror is set to false when the http ization header is not available instead of erroring out the dependency result will be none this is useful when you want to have optional authentication it is also useful when you want to have authentication that can be provided in one of multiple optional ways for example with openid connect or in a cookie type ignore attr defined openid connect authentication class an instance of it would be used as a dependency the openid connect url security scheme name it will be included in the generated openapi e g visible at docs security scheme description it will be included in the generated openapi e g visible at docs by default if no http auhtorization header is provided required for openid connect authentication it will automatically cancel the request and send the client an error if auto_error is set to false when the http ization header is not available instead of erroring out the dependency result will be none this is useful when you want to have optional authentication it is also useful when you want to have authentication that can be provided in one of multiple optional ways for example with openid connect or in a cookie
from typing import Optional

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
from typing_extensions import Annotated, Doc  


class OpenIdConnect(SecurityBase):
    

    def __init__(
        self,
        *,
        openIdConnectUrl: Annotated[
            str,
            Doc(
                
            ),
        ],
        scheme_name: Annotated[
            Optional[str],
            Doc(
                
            ),
        ] = None,
        description: Annotated[
            Optional[str],
            Doc(
                
            ),
        ] = None,
        auto_error: Annotated[
            bool,
            Doc(
                
            ),
        ] = True,
    ):
        self.model = OpenIdConnectModel(
            openIdConnectUrl=openIdConnectUrl, description=description
        )
        self.scheme_name = scheme_name or self.__class__.__name__
        self.auto_error = auto_error

    async def __call__(self, request: Request) -> Optional[str]:
        authorization = request.headers.get("Authorization")
        if not authorization:
            if self.auto_error:
                raise HTTPException(
                    status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
                )
            else:
                return None
        return authorization