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

Bug fix: Local authorizer not respecting header argument passed to it #1977

Open
wants to merge 5 commits 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
1 change: 1 addition & 0 deletions chalice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def __repr__(self) -> str:
class Authorizer(object):
name: str = ''
scopes: List[str] = []
config: Optional['BuiltinAuthConfig'] = None

def to_swagger(self) -> Dict[str, Any]:
raise NotImplementedError("to_swagger")
Expand Down
15 changes: 11 additions & 4 deletions chalice/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ def authorize(self,
authorizer = route_entry.authorizer
if not authorizer:
return lambda_event, lambda_context

auth_header = authorizer.config.header.lower()\
if authorizer.config else "authorization"

# If authorizer is Cognito then try to parse the JWT and simulate an
# APIGateway validated request
if isinstance(authorizer, CognitoUserPoolAuthorizer):
Expand Down Expand Up @@ -353,8 +357,10 @@ def authorize(self,
)
return lambda_event, lambda_context
arn = self._arn_builder.build_arn(method, raw_path)
auth_event = self._prepare_authorizer_event(arn, lambda_event,
lambda_context)
auth_event = self._prepare_authorizer_event(arn,
lambda_event,
lambda_context,
auth_header)
auth_result = authorizer(auth_event, lambda_context)
if auth_result is None:
raise InvalidAuthorizerError(
Expand Down Expand Up @@ -417,13 +423,14 @@ def _update_lambda_event(self, lambda_event: EventType,

def _prepare_authorizer_event(self, arn: str,
lambda_event: EventType,
lambda_context: LambdaContext) -> EventType:
lambda_context: LambdaContext,
auth_header: str = 'authorization') -> EventType:
"""Translate event for an authorizer input."""
authorizer_event = lambda_event.copy()
authorizer_event['type'] = 'TOKEN'
try:
authorizer_event['authorizationToken'] = authorizer_event.get(
'headers', {})['authorization']
'headers', {})[auth_header]
except KeyError:
raise NotAuthorizedError(
{'x-amzn-RequestId': lambda_context.aws_request_id,
Expand Down