Skip to content

Commit 6ef91a1

Browse files
committed
🚧 Select the right digest algorithm
1 parent 03ae0bb commit 6ef91a1

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

fastapi/security/http.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from fastapi.openapi.models import HTTPBase as HTTPBaseModel
77
from fastapi.openapi.models import HTTPBearer as HTTPBearerModel
88
from fastapi.security.base import SecurityBase
9-
from fastapi.security.utils import get_authorization_scheme_param
9+
from fastapi.security.utils import get_authorization_scheme_param, get_digest_algorithm
1010
from pydantic import BaseModel
1111
from starlette.requests import Request
1212
from starlette.status import HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN
@@ -143,18 +143,27 @@ async def __call__(
143143
self, request: Request
144144
) -> Optional[HTTPAuthorizationCredentials]:
145145
authorization: str = request.headers.get("Authorization")
146-
algorithm: str = request.headers.get("")
147-
scheme, credentials = get_authorization_scheme_param(authorization)
148-
if not (authorization and scheme and credentials):
146+
scheme, param = get_authorization_scheme_param(authorization)
147+
algorithm: str = request.headers.get("algorithm", "MD5")
148+
if self.realm:
149+
unauthorized_headers = {"WWW-Authenticate": f'Digest realm="{self.realm}"'}
150+
else:
151+
unauthorized_headers = {"WWW-Authenticate": "Digest"}
152+
invalid_user_credentials_exc = HTTPException(
153+
status_code=HTTP_401_UNAUTHORIZED,
154+
detail="Invalid authentication credentials",
155+
headers=unauthorized_headers,
156+
)
157+
if not authorization and scheme.lower() != "digest":
149158
if self.auto_error:
150159
raise HTTPException(
151-
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
160+
status_code=HTTP_403_FORBIDDEN,
161+
detail="Invalid authentication credentials",
162+
headers=unauthorized_headers,
152163
)
153-
else:
154-
return None
155-
if scheme.lower() != "digest":
156-
raise HTTPException(
157-
status_code=HTTP_403_FORBIDDEN,
158-
detail="Invalid authentication credentials",
159-
)
160-
return HTTPAuthorizationCredentials(scheme=scheme, credentials=credentials)
164+
return None
165+
try:
166+
algorithm_func = get_digest_algorithm(algorithm)
167+
algorithm_func().hexdigest()
168+
except ValueError:
169+
...

fastapi/security/utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
from typing import Tuple
1+
import hashlib
2+
from typing import Callable, Tuple
3+
4+
from _typeshed import ReadableBuffer
25

36

47
def get_authorization_scheme_param(authorization_header_value: str) -> Tuple[str, str]:
58
if not authorization_header_value:
69
return "", ""
710
scheme, _, param = authorization_header_value.partition(" ")
811
return scheme, param
12+
13+
14+
# TODO(Marcelo): Decide the returned value.
15+
def get_digest_algorithm(algorithm: str):
16+
if algorithm in ("MD5", "MD5-sess"):
17+
return hashlib.md5
18+
if algorithm in ("SHA-256", "SHA-256-sess"):
19+
return hashlib.sha256
20+
if algorithm in ("SHA-512-256", "SHA-512-256-sess"):
21+
return hashlib.sha512
22+
raise ValueError("Algorithm is not valid")

0 commit comments

Comments
 (0)