diff --git a/chalice/app.py b/chalice/app.py index 4fe176bf0..8f1e6f188 100644 --- a/chalice/app.py +++ b/chalice/app.py @@ -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") diff --git a/chalice/local.py b/chalice/local.py index 0c13f31ac..0dd0961ab 100644 --- a/chalice/local.py +++ b/chalice/local.py @@ -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): @@ -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( @@ -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,