|
8 | 8 | import json |
9 | 9 | import ssl |
10 | 10 | from collections.abc import Mapping |
| 11 | +from http import HTTPStatus |
11 | 12 | from typing import TYPE_CHECKING, Any, cast |
12 | 13 |
|
13 | 14 | if TYPE_CHECKING: |
|
49 | 50 | ) |
50 | 51 | from pyoverkiz.models import ServerConfig |
51 | 52 |
|
| 53 | +MIN_JWT_SEGMENTS = 2 |
| 54 | + |
52 | 55 |
|
53 | 56 | class BaseAuthStrategy(AuthStrategy): |
54 | 57 | """Base class for authentication strategies.""" |
@@ -110,13 +113,13 @@ async def _post_login(self, data: Mapping[str, Any]) -> None: |
110 | 113 | data=data, |
111 | 114 | ssl=self._ssl, |
112 | 115 | ) as response: |
113 | | - if response.status not in (200, 204): |
| 116 | + if response.status not in (HTTPStatus.OK, HTTPStatus.NO_CONTENT): |
114 | 117 | raise BadCredentialsError( |
115 | 118 | f"Login failed for {self.server.name}: {response.status}" |
116 | 119 | ) |
117 | 120 |
|
118 | 121 | # A 204 No Content response cannot have a body, so skip JSON parsing. |
119 | | - if response.status == 204: # noqa: PLR2004 |
| 122 | + if response.status == HTTPStatus.NO_CONTENT: |
120 | 123 | return |
121 | 124 |
|
122 | 125 | result = await response.json() |
@@ -419,7 +422,7 @@ def auth_headers(self, path: str | None = None) -> Mapping[str, str]: |
419 | 422 | def _decode_jwt_payload(token: str) -> dict[str, Any]: |
420 | 423 | """Decode the payload of a JWT token.""" |
421 | 424 | parts = token.split(".") |
422 | | - if len(parts) < 2: # noqa: PLR2004 |
| 425 | + if len(parts) < MIN_JWT_SEGMENTS: |
423 | 426 | raise InvalidTokenError("Malformed JWT received.") |
424 | 427 |
|
425 | 428 | payload_segment = parts[1] |
|
0 commit comments