22A module with utility functions to support authorizing the REST servers using the FastAPI framework.
33"""
44
5- from typing import Any , AsyncIterator , Optional
5+ from typing import Any , AsyncIterator , Callable , Optional
66
77from fastapi import HTTPException
88from fastapi .requests import Request
@@ -26,48 +26,78 @@ def _extract_project_from_request(request: Request) -> Optional[str]:
2626 return None
2727
2828
29- async def inject_user_details (request : Request ) -> AsyncIterator [Any ]:
30- """
31- Extract the authorization token from a user request, propagate user details
32- to the security manager, and scope permissions to the request project when
33- available (query param ``project``).
29+ def build_auth_dependency (
30+ scope_to_query_project : bool = False ,
31+ ) -> Callable [[Request ], AsyncIterator [Any ]]:
32+ """Build the FastAPI auth dependency that authenticates the caller and
33+ sets up the security-manager context for the request.
34+
35+ Args:
36+ scope_to_query_project: Controls which project's permissions are
37+ checked for the request.
38+
39+ ``False`` (default, safe): the request is authorized against the
40+ server's own project (``SecurityManager._project``, i.e.
41+ ``store.project``). Use this for every server that serves a single
42+ project and always acts on its own project regardless of the
43+ request, such as the feature server. The caller-supplied
44+ ``?project=`` query parameter must NOT be able to change which
45+ project's permissions are enforced here, otherwise a user with
46+ rights in some other project (or a non-existent one, which has no
47+ permissions and therefore grants all authenticated users access)
48+ could read and write this project's data.
49+
50+ ``True``: the request is authorized against the project named in
51+ the ``project`` query parameter. Use this ONLY for servers that
52+ serve many projects and act on the requested project, i.e. the
53+ registry REST server and anything that mounts its routes (UI
54+ server). There the requested project is also the project whose
55+ data is touched, so checking its permissions is correct.
3456
35- Yields so project context is reset after the request completes.
57+ Yields so any project context is reset after the request completes.
3658 """
37- sm = get_security_manager ()
38- current_user = None
39- project_token = None
40- if sm is not None :
41- try :
42- auth_manager = get_auth_manager ()
43- access_token = auth_manager .token_extractor .extract_access_token (
44- request = request
45- )
46- if not access_token :
47- raise HTTPException (
48- status_code = 401 , detail = "Missing authentication token"
59+
60+ async def inject_user_details (request : Request ) -> AsyncIterator [Any ]:
61+ sm = get_security_manager ()
62+ current_user = None
63+ project_token = None
64+ if sm is not None :
65+ try :
66+ auth_manager = get_auth_manager ()
67+ access_token = auth_manager .token_extractor .extract_access_token (
68+ request = request
4969 )
70+ if not access_token :
71+ raise HTTPException (
72+ status_code = 401 , detail = "Missing authentication token"
73+ )
5074
51- current_user = (
52- await auth_manager .token_parser .user_details_from_access_token (
53- access_token = access_token
75+ current_user = (
76+ await auth_manager .token_parser .user_details_from_access_token (
77+ access_token = access_token
78+ )
5479 )
55- )
56-
57- sm .set_current_user (current_user )
58- sm .set_current_request_token (access_token )
59- project = _extract_project_from_request (request )
60- if project is not None :
61- project_token = sm .set_current_project (project )
62- except HTTPException :
63- raise
64- except Exception :
65- raise HTTPException (
66- status_code = 401 , detail = "Invalid or expired access token"
67- )
68-
69- try :
70- yield current_user
71- finally :
72- if sm is not None and project_token is not None :
73- sm .reset_current_project (project_token )
80+
81+ sm .set_current_user (current_user )
82+ sm .set_current_request_token (access_token )
83+ if scope_to_query_project :
84+ project = _extract_project_from_request (request )
85+ if project is not None :
86+ project_token = sm .set_current_project (project )
87+ except HTTPException :
88+ raise
89+ except Exception :
90+ raise HTTPException (
91+ status_code = 401 , detail = "Invalid or expired access token"
92+ )
93+
94+ try :
95+ yield current_user
96+ finally :
97+ if sm is not None and project_token is not None :
98+ sm .reset_current_project (project_token )
99+
100+ return inject_user_details
101+
102+
103+ inject_user_details = build_auth_dependency (scope_to_query_project = False )
0 commit comments