-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathact.py
More file actions
64 lines (46 loc) · 1.89 KB
/
Copy pathact.py
File metadata and controls
64 lines (46 loc) · 1.89 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
"""
Helpers for working with the `act` claim on verified access token claims.
"""
from collections.abc import Mapping
from typing import Any, Optional
from .errors import VerifyAccessTokenError
INVALID_ACT_CLAIM_MESSAGE = "Invalid act claim"
def get_current_actor(claims: Mapping[str, Any]) -> Optional[str]:
"""
Return the current actor from the outermost `act.sub`, if present.
Only the outermost `act.sub` should be used for authorization decisions.
Nested `act` values represent prior actors and are informational.
"""
if not isinstance(claims, Mapping):
raise VerifyAccessTokenError(INVALID_ACT_CLAIM_MESSAGE)
act_claim = claims.get("act")
if act_claim is None:
return None
if not isinstance(act_claim, Mapping):
raise VerifyAccessTokenError(INVALID_ACT_CLAIM_MESSAGE)
sub = act_claim.get("sub")
if not isinstance(sub, str) or not sub.strip():
raise VerifyAccessTokenError(INVALID_ACT_CLAIM_MESSAGE)
return sub
def get_delegation_chain(claims: Mapping[str, Any]) -> list[str]:
"""
Return the delegation chain from newest actor to oldest actor.
The first entry is the current actor (outermost `act.sub`). Later entries are
prior actors from nested `act` values and are typically most useful for audit
and attribution.
"""
if not isinstance(claims, Mapping):
raise VerifyAccessTokenError(INVALID_ACT_CLAIM_MESSAGE)
current = claims.get("act")
if current is None:
return []
chain: list[str] = []
while current is not None:
if not isinstance(current, Mapping):
raise VerifyAccessTokenError(INVALID_ACT_CLAIM_MESSAGE)
sub = current.get("sub")
if not isinstance(sub, str) or not sub.strip():
raise VerifyAccessTokenError(INVALID_ACT_CLAIM_MESSAGE)
chain.append(sub)
current = current.get("act")
return chain