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

fix: use correct key for cognito username #1892

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions chalice/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,8 @@ def authorize(self, raw_path, lambda_event, lambda_context):
and "authorization" in lambda_event["headers"]:
token = lambda_event["headers"]["authorization"]
claims = self._decode_jwt_payload(token)

try:
cognito_username = claims["cognito:username"]
cognito_username = claims["username"]
except KeyError:
# If a key error is raised when trying to get the cognito
# username then it is a machine-to-machine communication.
Expand All @@ -333,7 +332,7 @@ def authorize(self, raw_path, lambda_event, lambda_context):
# users to test their code local with a different cognito
# authorization flow.
warnings.warn(
'%s for machine-to-machine communicaiton is not '
'%s for machine-to-machine communication is not '
'supported in local mode. All requests made against '
'a route will be authorized to allow local testing.'
% authorizer.__class__.__name__
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ def test_can_understand_cognito_token(self, lambda_context_args,
authorizer = LocalGatewayAuthorizer(demo_app_auth)
path = '/cognito'
event = create_event(path, 'GET', {})
event["headers"]["authorization"] = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhYWFhYWFhYS1iYmJiLWNjY2MtZGRkZC1lZWVlZWVlZWVlZWUiLCJhdWQiOiJ4eHh4eHh4eHh4eHhleGFtcGxlIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsInRva2VuX3VzZSI6ImlkIiwiYXV0aF90aW1lIjoxNTAwMDA5NDAwLCJpc3MiOiJodHRw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gv8ytPuynV4yDpf_xo-znJ-ZMqJ5KMo5zJSj3VOxb0p2n-uFE8ssgiwcCh6tLMOjwd9lhXw-bCyhqtqheSUY7oEr6frMOme0EUaiGTI0-XDdezo0KX_C1WpZvc3LrLv5hfzFgUSPKnjVCzO98A" # noqa
context = LambdaContext(*lambda_context_args)
event, context = authorizer.authorize(path, event, context)
principal_id = event['requestContext']['authorizer']['principalId']
Expand All @@ -1033,7 +1033,7 @@ def test_does_authorize_unsupported_cognito_token(self,
warning = recorded_warnings[0]
assert issubclass(warning.category, UserWarning)
assert ('CognitoUserPoolAuthorizer for machine-to-machine '
'communicaiton is not supported in local mode. All requests '
'communication is not supported in local mode. All requests '
'made against a route will be authorized to allow local '
'testing.') in str(warning.message)

Expand Down