Skip to content

Commit

Permalink
AREG-68 fix the user service keycloak service
Browse files Browse the repository at this point in the history
  • Loading branch information
D-GopalKrishna committed May 15, 2024
1 parent fd7bcf8 commit 3efb340
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from cloudharness.applications import get_configuration
import typing
import re
from api.services.keycloak_service import KeycloakService

# from cloudharness.models import User as CHUser # Cloudharness 2.0.0

Expand Down Expand Up @@ -164,13 +163,3 @@ def associate_orcid_id(userid: str, orcid: str):

def validate_orcid_id( orcid: str) -> bool:
return re.match(r'^https://orcid.org/\d{4}-\d{4}-\d{4}-\d{3}[0-9X]$', orcid)

def check_if_user_is_admin():
"""
Function that is used to validate the token and check if the user is an admin
"""
auth = KeycloakService()
if auth.current_user_has_realm_role("administrator"):
return {}
raise Exception("User is not an admin")

13 changes: 12 additions & 1 deletion applications/portal/backend/api/services/user_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import jwt
from cloudharness.middleware import get_authentication_token
from api.services.keycloak_service import KeycloakService

class UnrecognizedUser(Exception):
pass
Expand All @@ -10,4 +11,14 @@ def get_current_user_id() -> str:
token = get_authentication_token().replace("Bearer ", "")
return jwt.decode(token, options={"verify_signature": False}, algorithms='RS256')['sub']
except Exception as e:
raise UnrecognizedUser()
raise UnrecognizedUser()

def check_if_user_is_admin():
"""
Function that is used to validate the token and check if the user is an admin
"""
auth = KeycloakService()
if auth.current_user_has_realm_role("administrator"):
return {}
raise Exception("User is not an admin")

0 comments on commit 3efb340

Please sign in to comment.