Browse Source

Fix HTTPBasic realm compatibility and restore FastAPI header behavior

pull/14711/head
AnshMNSoni 6 months ago
parent
commit
ad09073385
  1. 6
      fastapi/dependencies/utils.py
  2. 1
      fastapi/security/http.py
  3. 6
      tests/test_security_http_base.py

6
fastapi/dependencies/utils.py

@ -5,7 +5,6 @@ from collections.abc import Coroutine, Mapping, Sequence
from contextlib import AsyncExitStack, contextmanager from contextlib import AsyncExitStack, contextmanager
from copy import copy, deepcopy from copy import copy, deepcopy
from dataclasses import dataclass from dataclasses import dataclass
from typing import get_type_hints
from typing import ( from typing import (
Annotated, Annotated,
Any, Any,
@ -14,6 +13,7 @@ from typing import (
Optional, Optional,
Union, Union,
cast, cast,
get_type_hints,
) )
import anyio import anyio
@ -284,7 +284,7 @@ def get_dependant(
for param_name, param in signature_params.items(): for param_name, param in signature_params.items():
annotation = param.annotation annotation = param.annotation
# Resolve ForwardRef inside Annotated without destroying metadata # Resolve ForwardRef inside Annotated without destroying metadata
if get_origin(annotation) is Annotated: if get_origin(annotation) is Annotated:
args = list(get_args(annotation)) args = list(get_args(annotation))
@ -294,7 +294,7 @@ def get_dependant(
args[0] = inner args[0] = inner
annotation = Annotated[inner, *args[1:]] annotation = Annotated[inner, *args[1:]]
param = param.replace(annotation=annotation) param = param.replace(annotation=annotation)
is_path_param = param_name in path_param_names is_path_param = param_name in path_param_names
param_details = analyze_param( param_details = analyze_param(
param_name=param_name, param_name=param_name,

1
fastapi/security/http.py

@ -125,7 +125,6 @@ class HTTPBasic(HTTPBase):
def make_authenticate_headers(self) -> dict[str, str]: def make_authenticate_headers(self) -> dict[str, str]:
return {"WWW-Authenticate": "Basic"} return {"WWW-Authenticate": "Basic"}
async def __call__( # type: ignore async def __call__( # type: ignore
self, request: Request self, request: Request

6
tests/test_security_http_base.py

@ -1,7 +1,7 @@
from fastapi import FastAPI, Security, Depends from fastapi import Depends, FastAPI, Security
from fastapi.security import HTTPBasic
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from fastapi.security import HTTPBasic
app = FastAPI() app = FastAPI()
@ -55,6 +55,7 @@ def test_openapi_schema():
}, },
} }
def test_http_basic_includes_realm(): def test_http_basic_includes_realm():
app = FastAPI() app = FastAPI()
security = HTTPBasic(realm="MyRealm") security = HTTPBasic(realm="MyRealm")
@ -68,4 +69,3 @@ def test_http_basic_includes_realm():
assert response.status_code == 401 assert response.status_code == 401
assert response.headers["WWW-Authenticate"] == 'Basic realm="MyRealm"' assert response.headers["WWW-Authenticate"] == 'Basic realm="MyRealm"'
Loading…
Cancel
Save