From 95b95dddddb7c1a95636108d320f83532df18c05 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 1 Jul 2026 14:46:38 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/security/open_id_connect_url.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/fastapi/security/open_id_connect_url.py b/fastapi/security/open_id_connect_url.py index a6fec2db6e..8b6ca9a9f8 100644 --- a/fastapi/security/open_id_connect_url.py +++ b/fastapi/security/open_id_connect_url.py @@ -35,9 +35,10 @@ DOCS = { 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). - """) + """), } + class OpenIdConnect(SecurityBase): """ OpenID Connect authentication class. An instance of it would be used as a @@ -57,11 +58,15 @@ class OpenIdConnect(SecurityBase): description: Annotated[str | None, DOCS["desc"]] = None, auto_error: Annotated[bool, DOCS["auto"]] = True, ): - self.model = OpenIdConnectModel(openIdConnectUrl=openIdConnectUrl, description=description) + self.model = OpenIdConnectModel( + openIdConnectUrl=openIdConnectUrl, description=description + ) self.scheme_name = scheme_name or self.__class__.__name__ self.auto_error = auto_error - def make_not_authenticated_error(self, detail: str = "Not authenticated") -> HTTPException: + def make_not_authenticated_error( + self, detail: str = "Not authenticated" + ) -> HTTPException: return HTTPException( status_code=HTTP_401_UNAUTHORIZED, detail=detail, @@ -70,11 +75,13 @@ class OpenIdConnect(SecurityBase): async def __call__(self, request: Request) -> str | None: authorization = request.headers.get("Authorization") - + # Case 1: Header is entirely missing if not authorization: if self.auto_error: - raise self.make_not_authenticated_error("Missing 'Authorization' header in request.") + raise self.make_not_authenticated_error( + "Missing 'Authorization' header in request." + ) return None # Case 2: Header exists but uses the wrong protocol/scheme (e.g., Basic, APIKey, or raw token)