-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from aws-samples/wip/tmscarla/configurable-oi…
…dc-roles feat: make user roles claim in the jwt configurable, using 'cognito:groups' as default
- Loading branch information
Showing
10 changed files
with
55 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
SECRET_ID = os.getenv("SECRET_ID") | ||
ENABLE_MFA = os.getenv("ENABLE_MFA") | ||
SITE_URL = os.getenv("SITE_URL", API_BASE_URL) | ||
USER_ROLES_CLAIM = os.getenv("USER_ROLES_CLAIM", "cognito:groups") | ||
|
||
try: | ||
if (not USER_POOL_ID or USER_POOL_ID == "") and SECRET_ID: | ||
|
@@ -137,7 +138,7 @@ def authenticate(group): | |
return auth_redirect() | ||
except jose.exceptions.JWSSignatureError: | ||
return logout() | ||
if not disable_auth() and (group != "guest") and (group not in set(decoded.get("cognito:groups", []))): | ||
if not disable_auth() and (group != "guest") and (group not in set(decoded.get(USER_ROLES_CLAIM, []))): | ||
return auth_redirect() | ||
|
||
|
||
|
@@ -540,15 +541,24 @@ def get_instance_types(): | |
return {"instance_types": sorted(instance_types, key=lambda x: x["InstanceType"])} | ||
|
||
|
||
def _get_user_roles(decoded): | ||
print(os.environ.get("USER_ROLES_CLAIM")) | ||
return decoded[USER_ROLES_CLAIM] if USER_ROLES_CLAIM in decoded else ["user"] | ||
|
||
|
||
|
||
def get_identity(): | ||
if running_local(): | ||
return {"cognito:groups": ["user", "admin"], "username": "username", "attributes": {"email": "[email protected]"}} | ||
return {"user_roles": ["user", "admin"], "username": "username", "attributes": {"email": "[email protected]"}} | ||
|
||
access_token = request.cookies.get("accessToken") | ||
if not access_token: | ||
return {"message": "No access token."}, 401 | ||
try: | ||
decoded = jwt_decode(access_token, USER_POOL_ID) | ||
decoded["user_roles"] = _get_user_roles(decoded) | ||
decoded.pop(USER_ROLES_CLAIM) | ||
|
||
username = decoded.get("username") | ||
if username: | ||
cognito = boto3.client("cognito-idp") | ||
|
@@ -559,7 +569,7 @@ def get_identity(): | |
return {"message": "Signature expired."}, 401 | ||
|
||
if disable_auth(): | ||
decoded["cognito:groups"] = ["user", "admin"] | ||
decoded["user_roles"] = ["user", "admin"] | ||
|
||
return decoded | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pytest | ||
from unittest import mock | ||
from api.PclusterApiHandler import _get_user_roles | ||
|
||
|
||
@mock.patch("api.PclusterApiHandler.USER_ROLES_CLAIM", "user_roles") | ||
def test_user_roles(): | ||
user_roles = ["user", "admin"] | ||
|
||
_test_decoded_with_user_roles_claim(decoded={"user_roles": user_roles}, user_roles=user_roles) | ||
_test_decoded_without_user_roles_claim(decoded={}) | ||
|
||
|
||
|
||
def _test_decoded_with_user_roles_claim(decoded, user_roles): | ||
assert _get_user_roles(decoded) == user_roles | ||
|
||
|
||
def _test_decoded_without_user_roles_claim(decoded): | ||
assert _get_user_roles(decoded) == ["user"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const USER_ROLES_CLAIM = "user_roles" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ boto3 | |
requests | ||
python-jose | ||
pyyaml | ||
pytest |