|
6 | 6 | from fastapi.openapi.models import HTTPBase as HTTPBaseModel |
7 | 7 | from fastapi.openapi.models import HTTPBearer as HTTPBearerModel |
8 | 8 | 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 |
10 | 10 | from pydantic import BaseModel |
11 | 11 | from starlette.requests import Request |
12 | 12 | from starlette.status import HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN |
@@ -143,18 +143,27 @@ async def __call__( |
143 | 143 | self, request: Request |
144 | 144 | ) -> Optional[HTTPAuthorizationCredentials]: |
145 | 145 | 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": |
149 | 158 | if self.auto_error: |
150 | 159 | 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, |
152 | 163 | ) |
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 | + ... |
0 commit comments