|
11 | 11 | from kubernetes.config import ConfigException |
12 | 12 |
|
13 | 13 | from configuration import configuration |
14 | | -from auth.utils import extract_user_token |
15 | 14 | from auth.interface import AuthInterface |
16 | 15 | from constants import DEFAULT_VIRTUAL_PATH |
17 | 16 |
|
@@ -227,18 +226,33 @@ class K8SAuthDependency(AuthInterface): # pylint: disable=too-few-public-method |
227 | 226 | def __init__(self, virtual_path: str = DEFAULT_VIRTUAL_PATH) -> None: |
228 | 227 | """Initialize the required allowed paths for authorization checks.""" |
229 | 228 | self.virtual_path = virtual_path |
| 229 | + self.skip_userid_check = False |
230 | 230 |
|
231 | | - async def __call__(self, request: Request) -> tuple[str, str, str]: |
| 231 | + async def __call__(self, request: Request) -> tuple[str, str, bool, str]: |
232 | 232 | """Validate FastAPI Requests for authentication and authorization. |
233 | 233 |
|
234 | 234 | Args: |
235 | 235 | request: The FastAPI request object. |
236 | 236 |
|
237 | 237 | Returns: |
238 | 238 | The user's UID and username if authentication and authorization succeed |
239 | | - user_id check is skipped with noop auth to allow consumers provide user_id |
| 239 | + user_id check should never be skipped with K8s authentication |
| 240 | + If user_id check should be skipped - always return False for k8s |
| 241 | + User's token |
240 | 242 | """ |
241 | | - token = extract_user_token(request.headers) |
| 243 | + authorization_header = request.headers.get("Authorization") |
| 244 | + if not authorization_header: |
| 245 | + raise HTTPException( |
| 246 | + status_code=401, detail="Unauthorized: No auth header found" |
| 247 | + ) |
| 248 | + |
| 249 | + token = _extract_bearer_token(authorization_header) |
| 250 | + if not token: |
| 251 | + raise HTTPException( |
| 252 | + status_code=401, |
| 253 | + detail="Unauthorized: Bearer token not found or invalid", |
| 254 | + ) |
| 255 | + |
242 | 256 | user_info = get_user_info(token) |
243 | 257 | if user_info is None: |
244 | 258 | raise HTTPException( |
@@ -267,4 +281,9 @@ async def __call__(self, request: Request) -> tuple[str, str, str]: |
267 | 281 | logger.error("API exception during SubjectAccessReview: %s", e) |
268 | 282 | raise HTTPException(status_code=403, detail="Internal server error") from e |
269 | 283 |
|
270 | | - return user_info.user.uid, user_info.user.username, token |
| 284 | + return ( |
| 285 | + user_info.user.uid, |
| 286 | + user_info.user.username, |
| 287 | + self.skip_userid_check, |
| 288 | + token, |
| 289 | + ) |
0 commit comments