3333SECRET_ID = os .getenv ("SECRET_ID" )
3434ENABLE_MFA = os .getenv ("ENABLE_MFA" )
3535SITE_URL = os .getenv ("SITE_URL" , API_BASE_URL )
36+ USER_ROLES_CLAIM = os .getenv ("USER_ROLES_CLAIM" , "cognito:groups" )
3637
3738try :
3839 if (not USER_POOL_ID or USER_POOL_ID == "" ) and SECRET_ID :
@@ -137,7 +138,7 @@ def authenticate(group):
137138 return auth_redirect ()
138139 except jose .exceptions .JWSSignatureError :
139140 return logout ()
140- if not disable_auth () and (group != "guest" ) and (group not in set (decoded .get ("cognito:groups" , []))):
141+ if not disable_auth () and (group != "guest" ) and (group not in set (decoded .get (USER_ROLES_CLAIM , []))):
141142 return auth_redirect ()
142143
143144
@@ -540,15 +541,24 @@ def get_instance_types():
540541 return {"instance_types" : sorted (instance_types , key = lambda x : x ["InstanceType" ])}
541542
542543
544+ def _get_user_roles (decoded ):
545+ print (os .environ .get ("USER_ROLES_CLAIM" ))
546+ return decoded [USER_ROLES_CLAIM ] if USER_ROLES_CLAIM in decoded else ["user" ]
547+
548+
549+
543550def get_identity ():
544551 if running_local ():
545- return {
"cognito:groups " : [
"user" ,
"admin" ],
"username" :
"username" ,
"attributes" : {
"email" :
"[email protected] " }}
552+ return {
"user_roles " : [
"user" ,
"admin" ],
"username" :
"username" ,
"attributes" : {
"email" :
"[email protected] " }}
546553
547554 access_token = request .cookies .get ("accessToken" )
548555 if not access_token :
549556 return {"message" : "No access token." }, 401
550557 try :
551558 decoded = jwt_decode (access_token , USER_POOL_ID )
559+ decoded ["user_roles" ] = _get_user_roles (decoded )
560+ decoded .pop (USER_ROLES_CLAIM )
561+
552562 username = decoded .get ("username" )
553563 if username :
554564 cognito = boto3 .client ("cognito-idp" )
@@ -559,7 +569,7 @@ def get_identity():
559569 return {"message" : "Signature expired." }, 401
560570
561571 if disable_auth ():
562- decoded ["cognito:groups " ] = ["user" , "admin" ]
572+ decoded ["user_roles " ] = ["user" , "admin" ]
563573
564574 return decoded
565575
0 commit comments