pre-commit-ci-lite[bot]
6 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with
7 additions and
9 deletions
-
fastapi/dependencies/utils.py
-
fastapi/security/http.py
-
tests/test_security_http_base.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 |
|
|
|
|
|
@ -145,16 +145,14 @@ class HTTPBasic(HTTPBase): |
|
|
scheme_name: Optional[str] = None, |
|
|
scheme_name: Optional[str] = None, |
|
|
description: Optional[str] = None, |
|
|
description: Optional[str] = None, |
|
|
auto_error: bool = True, |
|
|
auto_error: bool = True, |
|
|
): |
|
|
): |
|
|
self.model = HTTPBaseModel(scheme="basic", description=description) |
|
|
self.model = HTTPBaseModel(scheme="basic", description=description) |
|
|
self.scheme_name = scheme_name or self.__class__.__name__ |
|
|
self.scheme_name = scheme_name or self.__class__.__name__ |
|
|
self.auto_error = auto_error |
|
|
self.auto_error = auto_error |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
) -> Optional[HTTPBasicCredentials]: |
|
|
) -> Optional[HTTPBasicCredentials]: |
|
|
|
|
|
@ -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"' |
|
|
|
|
|
|