Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add roles/groups of jwt token #743

Draft
wants to merge 26 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions tdp_core/security/store/alb_security_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,39 @@


class ALBSecurityStore(BaseStore):
def __init__(self, cookie_name: Optional[str], signout_url: Optional[str]):
def __init__(
self, cookie_name: Optional[str], signout_url: Optional[str], token_user_attr: Optional[str], token_roles_attr: Optional[str]
):
self.cookie_name = cookie_name
self.signout_url: Optional[str] = signout_url
self.signout_url = signout_url
self.token_user_attr = token_user_attr
self.token_roles_attr = token_roles_attr

def load_from_request(self, req):
if "X-Amzn-Oidc-Identity" in req.headers and "X-Amzn-Oidc-Accesstoken" in req.headers and "X-Amzn-Oidc-Data" in req.headers:
try:
roles = []
# Get token data from header
_log.debug(f"headers: {req.headers}")
encoded = req.headers["X-Amzn-Oidc-Data"]
_log.debug(f"X-Amzn-Oidc-Data: {encoded}")
_log.debug(f"X-Amzn-Oidc-Accesstoken: {req.headers['X-Amzn-Oidc-Accesstoken']}")
_log.debug(f"X-Amzn-Oidc-Identity: {req.headers['X-Amzn-Oidc-Identity']}")
# Try to decode the oidc data jwt
user = jwt.decode(encoded, options={"verify_signature": False})
user_data = jwt.decode(encoded, options={"verify_signature": False})
_log.debug(f"user data: {user_data}")
# Create new user from given attributes
email = user["email"]
return User(id=email, roles=[])
user = user_data[self.token_user_attr]
_log.debug("user: %s", user)
if self.token_roles_attr:
roles = user_data[self.token_roles_attr]
_log.debug("roletype: %s", type(roles))
if not roles:
roles = []
elif type(roles) == str:
roles = [roles]
_log.debug("roles: %s", roles)
return User(id=user, roles=roles)
except Exception:
_log.exception("Error in load_from_request")
return None
Expand Down Expand Up @@ -54,6 +73,8 @@ def create():
return ALBSecurityStore(
manager.settings.tdp_core.security.store.alb_security_store.cookie_name,
manager.settings.tdp_core.security.store.alb_security_store.signout_url,
manager.settings.tdp_core.security.store.alb_security_store.token_user_attr,
manager.settings.tdp_core.security.store.alb_security_store.token_roles_attr,
)

return None
2 changes: 2 additions & 0 deletions tdp_core/settings/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class AlbSecurityStoreSettings(BaseModel):
enable: bool = False
cookie_name: Optional[str] = None
signout_url: Optional[str] = None
token_user_attr: Optional[str] = "email"
token_roles_attr: Optional[str] = None


class NoSecurityStoreSettings(BaseModel):
Expand Down