mirror of https://github.com/tiangolo/fastapi.git
Update code to reflect latest changes in `master`
This commit is contained in:
parent
631448de4e
commit
f3d23119f9
|
|
@ -667,7 +667,7 @@ class OAuth2ClientCredentials(OAuth2):
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
scheme_name: Annotated[
|
scheme_name: Annotated[
|
||||||
Optional[str],
|
str | None,
|
||||||
Doc(
|
Doc(
|
||||||
"""
|
"""
|
||||||
Security scheme name.
|
Security scheme name.
|
||||||
|
|
@ -677,7 +677,7 @@ class OAuth2ClientCredentials(OAuth2):
|
||||||
),
|
),
|
||||||
] = None,
|
] = None,
|
||||||
scopes: Annotated[
|
scopes: Annotated[
|
||||||
Optional[Dict[str, str]],
|
dict[str, str] | None,
|
||||||
Doc(
|
Doc(
|
||||||
"""
|
"""
|
||||||
The OAuth2 scopes that would be required by the *path operations* that
|
The OAuth2 scopes that would be required by the *path operations* that
|
||||||
|
|
@ -686,7 +686,7 @@ class OAuth2ClientCredentials(OAuth2):
|
||||||
),
|
),
|
||||||
] = None,
|
] = None,
|
||||||
description: Annotated[
|
description: Annotated[
|
||||||
Optional[str],
|
str | None,
|
||||||
Doc(
|
Doc(
|
||||||
"""
|
"""
|
||||||
Security scheme description.
|
Security scheme description.
|
||||||
|
|
@ -734,16 +734,12 @@ class OAuth2ClientCredentials(OAuth2):
|
||||||
auto_error=auto_error,
|
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")
|
authorization = request.headers.get("Authorization")
|
||||||
scheme, param = get_authorization_scheme_param(authorization)
|
scheme, param = get_authorization_scheme_param(authorization)
|
||||||
if not authorization or scheme.lower() != "bearer":
|
if not authorization or scheme.lower() != "bearer":
|
||||||
if self.auto_error:
|
if self.auto_error:
|
||||||
raise HTTPException(
|
raise self.make_not_authenticated_error()
|
||||||
status_code=HTTP_401_UNAUTHORIZED,
|
|
||||||
detail="Not authenticated",
|
|
||||||
headers={"WWW-Authenticate": "Bearer"},
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
return param
|
return param
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from typing import Optional
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import FastAPI, Security
|
from fastapi import FastAPI, Security
|
||||||
from fastapi.security import OAuth2ClientCredentials
|
from fastapi.security import OAuth2ClientCredentials
|
||||||
|
|
@ -10,7 +10,7 @@ oauth2_scheme = OAuth2ClientCredentials(tokenUrl="token", auto_error=True)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/items/")
|
@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}
|
return {"token": token}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from typing import Optional
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import FastAPI, Security
|
from fastapi import FastAPI, Security
|
||||||
from fastapi.security import OAuth2ClientCredentials
|
from fastapi.security import OAuth2ClientCredentials
|
||||||
|
|
@ -10,7 +10,7 @@ oauth2_scheme = OAuth2ClientCredentials(tokenUrl="token", auto_error=False)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/items/")
|
@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}
|
return {"token": token}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue