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

Long Lived Token - Bug #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 4 additions & 9 deletions fabric_cm/credmgr/token/token_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def encode(self, private_key: str, validity_in_seconds: int, kid: str, pass_phra
self._add_fabric_claims()

if not Utils.is_short_lived(lifetime_in_hours=int(validity_in_seconds/3600)) and \
not self._validate_lifetime(validity=validity_in_seconds, project_id=self.project_id,
roles=self.claims.get(self.ROLES)):
not self._validate_lifetime(validity=validity_in_seconds, project=self.claims[self.PROJECTS][0]):
raise TokenError(f"User {self.claims[self.EMAIL]} is not authorized to create long lived tokens!")

code, token_or_exception = JWTManager.encode_and_sign_with_private_key(validity=validity_in_seconds,
Expand All @@ -107,19 +106,15 @@ def encode(self, private_key: str, validity_in_seconds: int, kid: str, pass_phra
self.encoded = True
return self.token

def _validate_lifetime(self, *, validity: int, roles: dict, project_id: str):
def _validate_lifetime(self, *, validity: int, project: dict):
"""
Set the claims for the Token by adding membership, project and scope
"""
if validity == CONFIG_OBJ.get_token_life_time():
return True

llt_role = f"{project_id}-{CONFIG_OBJ.get_llt_role_suffix()}"

# User doesn't have the role to create Long lived tokens
for role in roles:
if llt_role in role.values():
return True
if project.get("memberships") and project.get("memberships").get("is_token_holder"):
return True

return False

Expand Down