Update code to reflect latest changes in `master`

This commit is contained in:
Yurii Motov 2026-02-12 09:55:35 +01:00
parent 631448de4e
commit f3d23119f9
3 changed files with 9 additions and 13 deletions

View File

@ -667,7 +667,7 @@ class OAuth2ClientCredentials(OAuth2):
),
],
scheme_name: Annotated[
Optional[str],
str | None,
Doc(
"""
Security scheme name.
@ -677,7 +677,7 @@ class OAuth2ClientCredentials(OAuth2):
),
] = None,
scopes: Annotated[
Optional[Dict[str, str]],
dict[str, str] | None,
Doc(
"""
The OAuth2 scopes that would be required by the *path operations* that
@ -686,7 +686,7 @@ class OAuth2ClientCredentials(OAuth2):
),
] = None,
description: Annotated[
Optional[str],
str | None,
Doc(
"""
Security scheme description.
@ -734,16 +734,12 @@ class OAuth2ClientCredentials(OAuth2):
auto_error=auto_error,
)
async def __call__(self, request: Request) -> Optional[str]:
async def __call__(self, request: Request) -> str | None:
authorization = request.headers.get("Authorization")
scheme, param = get_authorization_scheme_param(authorization)
if not authorization or scheme.lower() != "bearer":
if self.auto_error:
raise HTTPException(
status_code=HTTP_401_UNAUTHORIZED,
detail="Not authenticated",
headers={"WWW-Authenticate": "Bearer"},
)
raise self.make_not_authenticated_error()
else:
return None
return param

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Annotated
from fastapi import FastAPI, Security
from fastapi.security import OAuth2ClientCredentials
@ -10,7 +10,7 @@ oauth2_scheme = OAuth2ClientCredentials(tokenUrl="token", auto_error=True)
@app.get("/items/")
async def read_items(token: Optional[str] = Security(oauth2_scheme)):
async def read_items(token: Annotated[str, Security(oauth2_scheme)]):
return {"token": token}

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Annotated
from fastapi import FastAPI, Security
from fastapi.security import OAuth2ClientCredentials
@ -10,7 +10,7 @@ oauth2_scheme = OAuth2ClientCredentials(tokenUrl="token", auto_error=False)
@app.get("/items/")
async def read_items(token: Optional[str] = Security(oauth2_scheme)):
async def read_items(token: Annotated[str | None, Security(oauth2_scheme)]):
return {"token": token}