Repository navigation
Expand file tree
/
Copy pathauth.py
More file actions
68 lines (57 loc) · 2.21 KB
/
Copy pathauth.py
File metadata and controls
68 lines (57 loc) · 2.21 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Authorization and authentication utilities.
"""
import json
import flask
import requests
from relation_engine_server.utils.config import get_config
from relation_engine_server.exceptions import MissingHeader, UnauthorizedAccess
def require_auth_token(roles=[]):
"""
Function that validates an authentication token in a flask request context.
If any roles are provided, the token holder must have *at least one* of the roles.
Raises some exception if any auth requirement is not met.
"""
config = get_config()
if not flask.request.headers.get("Authorization"):
# No authorization token was provided in the headers
raise MissingHeader("Authorization")
token = get_auth_header()
# Make an authorization request to the kbase auth2 server
headers = {"Authorization": token}
auth_url = config["auth_url"] + "/api/V2/me"
auth_resp = requests.get(auth_url, headers=headers)
if not auth_resp.ok:
print("-" * 80)
print(auth_resp.text)
raise UnauthorizedAccess(config["auth_url"], auth_resp.text)
auth_json = auth_resp.json()
if len(roles):
check_roles(
required=roles, given=auth_json["customroles"], auth_url=config["auth_url"]
)
def check_roles(required, given, auth_url):
for role in required:
if role in given:
return
raise UnauthorizedAccess(auth_url, "Missing role")
def get_auth_header():
return flask.request.headers.get("Authorization", "").replace("Bearer", "").strip()
def get_workspace_ids(auth_token):
"""Get a list of workspace IDs that the given username is allowed to access in
the workspace."""
if not auth_token:
return [] # anonymous users
config = get_config()
ws_url = config["workspace_url"]
# Make an admin request to the workspace (command is 'listWorkspaceIds')
payload = {
"method": "Workspace.list_workspace_ids",
"version": "1.1",
"params": [{"perm": "r"}],
}
headers = {"Authorization": auth_token}
resp = requests.post(ws_url, data=json.dumps(payload), headers=headers)
if not resp.ok:
raise UnauthorizedAccess(ws_url, resp.text)
return resp.json()["result"][0]["workspaces"]