From 7eb0fad53dc02f247754259876efa2ac84b1e806 Mon Sep 17 00:00:00 2001 From: Saed SayedAhmed Date: Wed, 7 Sep 2022 17:33:08 -0400 Subject: [PATCH] Fix local authorizer not respecting `header` argument passed to it. Adds the ability to locally use the authorizer `header` argument by using the `BuiltInAuthConfig` attribute in the wrapped authorizer. --- chalice/app.py | 1 + chalice/local.py | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/chalice/app.py b/chalice/app.py index de6726bd6..3410ed040 100644 --- a/chalice/app.py +++ b/chalice/app.py @@ -223,6 +223,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") diff --git a/chalice/local.py b/chalice/local.py index 6cdb0bee2..b5c3302f9 100644 --- a/chalice/local.py +++ b/chalice/local.py @@ -315,6 +315,10 @@ def authorize(self, raw_path, lambda_event, lambda_context): 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): @@ -356,8 +360,10 @@ def authorize(self, raw_path, lambda_event, lambda_context): ) 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( @@ -417,14 +423,15 @@ def _update_lambda_event(self, lambda_event, auth_result): lambda_event['requestContext']['authorizer'] = auth_context return lambda_event - def _prepare_authorizer_event(self, arn, lambda_event, lambda_context): - # type: (str, EventType, LambdaContext) -> EventType + def _prepare_authorizer_event(self, arn, lambda_event, lambda_context, + auth_header='authorization'): + # type: (str, EventType, LambdaContext, str) -> 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,