-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathrest.py
More file actions
46 lines (38 loc) · 1.38 KB
/
rest.py
File metadata and controls
46 lines (38 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
A module with utility functions to support authorizing the REST servers using the FastAPI framework.
"""
from typing import Any
from fastapi import HTTPException
from fastapi.requests import Request
from feast.permissions.auth.auth_manager import (
get_auth_manager,
)
from feast.permissions.security_manager import get_security_manager
async def inject_user_details(request: Request) -> Any:
"""
A function to extract the authorization token from a user request, extract the user details and propagate them to the
current security manager, if any.
"""
sm = get_security_manager()
current_user = None
if sm is not None:
try:
auth_manager = get_auth_manager()
access_token = auth_manager.token_extractor.extract_access_token(
request=request
)
if not access_token:
raise HTTPException(
status_code=401, detail="Missing authentication token"
)
current_user = (
await auth_manager.token_parser.user_details_from_access_token(
access_token=access_token
)
)
sm.set_current_user(current_user)
except Exception:
raise HTTPException(
status_code=401, detail="Invalid or expired access token"
)
return current_user