Skip to content

Commit 0e6daaf

Browse files
committed
chore: do not use the complicated notebooks gitlab header
The gitlab credentials header from the notebooks is really complicated. We used it here just to get the access token expiry. I modified the gateway to now pass in an extra header value to indicate the gitlab token expiry.
1 parent 6c4468d commit 0e6daaf

File tree

1 file changed

+7
-27
lines changed
  • components/renku_data_services/authn

1 file changed

+7
-27
lines changed

components/renku_data_services/authn/gitlab.py

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
"""Gitlab authenticator."""
22

3-
import base64
43
import contextlib
5-
import json
6-
import re
74
import urllib.parse as parse
85
from contextlib import suppress
96
from dataclasses import dataclass
107
from datetime import datetime
11-
from typing import Any
128

139
import gitlab
1410
from sanic import Request
@@ -30,6 +26,7 @@ class GitlabAuthenticator:
3026
gitlab_url: str
3127

3228
token_field: str = "Gitlab-Access-Token"
29+
expires_at_field: str = "Gitlab-Access-Token-Expires-At"
3330

3431
def __post_init__(self) -> None:
3532
"""Properly set gitlab url."""
@@ -76,7 +73,12 @@ async def _get_gitlab_api_user(self, access_token: str, headers: Header) -> base
7673
if len(name_parts) >= 1:
7774
last_name = " ".join(name_parts)
7875

79-
_, _, _, expires_at = self.git_creds_from_headers(headers)
76+
expires_at: datetime | None = None
77+
expires_at_raw: str | None = headers.get(self.expires_at_field)
78+
if expires_at_raw is not None and len(expires_at_raw) > 0:
79+
with suppress(ValueError):
80+
expires_at = datetime.fromtimestamp(float(expires_at_raw))
81+
8082
return base_models.APIUser(
8183
id=str(user_id),
8284
access_token=access_token,
@@ -86,25 +88,3 @@ async def _get_gitlab_api_user(self, access_token: str, headers: Header) -> base
8688
full_name=full_name,
8789
access_token_expires_at=expires_at,
8890
)
89-
90-
@staticmethod
91-
def git_creds_from_headers(headers: Header) -> tuple[Any, Any, Any, datetime | None]:
92-
"""Extract git credentials from the encoded header sent by the gateway."""
93-
parsed_dict = json.loads(base64.decodebytes(headers["Renku-Auth-Git-Credentials"].encode()))
94-
git_url, git_credentials = next(iter(parsed_dict.items()))
95-
token_match = re.match(r"^[^\s]+\ ([^\s]+)$", git_credentials["AuthorizationHeader"])
96-
git_token = token_match.group(1) if token_match is not None else None
97-
git_token_expires_at_raw = git_credentials["AccessTokenExpiresAt"]
98-
git_token_expires_at_num: float | None = None
99-
with suppress(ValueError, TypeError):
100-
git_token_expires_at_num = float(git_token_expires_at_raw)
101-
git_token_expires_at: datetime | None = None
102-
if git_token_expires_at_num is not None and git_token_expires_at_num > 0:
103-
with suppress(ValueError):
104-
git_token_expires_at = datetime.fromtimestamp(git_token_expires_at_num)
105-
return (
106-
git_url,
107-
git_credentials["AuthorizationHeader"],
108-
git_token,
109-
git_token_expires_at,
110-
)

0 commit comments

Comments
 (0)