Skip to content

Commit 71ab87f

Browse files
Make virtual_lab_id optional in header context
1 parent d795939 commit 71ab87f

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

app/dependencies/auth.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ def _check_user_info(
128128

129129
user_info_response = deserialize_response(response, model_class=UserInfoResponse)
130130

131+
if project_context.virtual_lab_id is None and project_context.project_id is not None:
132+
project_context.virtual_lab_id = user_info_response.virtual_lab_from_project_id(
133+
project_context.project_id
134+
)
135+
131136
is_authorized = user_info_response.is_authorized_for(
132137
virtual_lab_id=project_context.virtual_lab_id,
133138
project_id=project_context.project_id,

app/schemas/auth.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
from app.errors import AuthErrorReason
1010
from app.logger import L
1111

12+
PROJECT_REGEX = re.compile(
13+
r"/proj/(?P<vlab>[0-9a-fA-F-]+)/(?P<proj>[0-9a-fA-F-]+)/(?P<role>admin|member)"
14+
)
15+
1216

1317
class CacheKey(BaseModel):
1418
"""Cache key for UserContext."""
@@ -106,16 +110,18 @@ def is_authorized_for(self, virtual_lab_id: UUID | None, project_id: UUID | None
106110
]
107111
)
108112

113+
def virtual_lab_from_project_id(self, project_id: UUID) -> UUID | None:
114+
for s in self.groups:
115+
if (match := PROJECT_REGEX.match(s)) and match.group("proj") == str(project_id):
116+
return UUID(match.group("vlab"))
117+
return None
118+
109119
def user_project_ids(self) -> list[UUID]:
110120
"""Return the the list if project_ids the user is authorized for."""
111-
pattern = r"/proj/[0-9a-fA-F-]+/([0-9a-fA-F-]+)/(admin|member)"
112-
113121
project_ids: set[UUID] = set()
114-
115122
for s in self.groups:
116-
match = re.match(pattern, s)
117-
if match:
118-
project_ids.add(UUID(match.group(1)))
123+
if match := PROJECT_REGEX.match(s):
124+
project_ids.add(UUID(match.group("proj")))
119125

120126
return list(project_ids)
121127

tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
ADMIN_SUB_ID,
5656
AUTH_HEADER_ADMIN,
5757
AUTH_HEADER_USER_1,
58-
AUTH_HEADER_USER_2,
5958
AUTH_HEADER_USER_1_IDS,
59+
AUTH_HEADER_USER_2,
6060
PROJECT_HEADERS,
6161
PROJECT_ID,
6262
TOKEN_ADMIN,
6363
TOKEN_USER_1,
64-
TOKEN_USER_2,
6564
TOKEN_USER_1_IDS,
65+
TOKEN_USER_2,
6666
UNRELATED_PROJECT_HEADERS,
6767
UNRELATED_PROJECT_ID,
6868
UNRELATED_VIRTUAL_LAB_ID,
@@ -204,7 +204,7 @@ def _override_check_user_info(
204204
user_context_user_1,
205205
user_context_user_2,
206206
user_context_no_project,
207-
user_context_only_token_ids
207+
user_context_only_token_ids,
208208
):
209209
# map (token, project-id) to the expected user_context
210210
mapping = {

tests/test_auth.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
PROJECT_CONTEXTS = [
2424
OptionalProjectContext(virtual_lab_id=None, project_id=None),
2525
OptionalProjectContext(virtual_lab_id=VIRTUAL_LAB_ID, project_id=PROJECT_ID),
26+
OptionalProjectContext(virtual_lab_id=None, project_id=PROJECT_ID),
2627
]
2728

2829

0 commit comments

Comments
 (0)