in chalice/local.py [0:0]
def handle_request(self,
method: str,
path: str,
headers: HeaderType,
body: Optional[bytes]) -> ResponseType:
lambda_context = self._generate_lambda_context()
try:
lambda_event = self._generate_lambda_event(
method, path, headers, body)
except ValueError:
# API Gateway will return a different error on route not found
# depending on whether or not we have an authorization token in our
# request. Since we do not do that check until we actually find
# the authorizer that we will call we do not have that information
# available at this point. Instead we just check to see if that
# header is present and change our response if it is. This will
# need to be refactored later if we decide to more closely mirror
# how API Gateway does their auth and routing.
error_headers = {'x-amzn-RequestId': lambda_context.aws_request_id,
'x-amzn-ErrorType': 'UnauthorizedException'}
auth_header = headers.get('authorization')
if auth_header is None:
auth_header = headers.get('Authorization')
if auth_header is not None:
raise ForbiddenError(
error_headers,
(b'{"message": "Authorization header requires '
b'\'Credential\''
b' parameter. Authorization header requires \'Signature\''
b' parameter. Authorization header requires '
b'\'SignedHeaders\' parameter. Authorization header '
b'requires existence of either a \'X-Amz-Date\' or a'
b' \'Date\' header. Authorization=%s"}'
% auth_header.encode('ascii')))
raise ForbiddenError(
error_headers,
b'{"message": "Missing Authentication Token"}')
# This can either be because the user's provided an OPTIONS method
# *or* this is a preflight request, which chalice automatically
# responds to without invoking a user defined route.
if method == 'OPTIONS' and \
not self._has_user_defined_options_method(lambda_event):
# No options route was defined for this path. API Gateway should
# automatically generate our CORS headers.
options_headers = self._autogen_options_headers(lambda_event)
return {
'statusCode': 200,
'headers': options_headers,
'multiValueHeaders': {},
'body': None
}
# The authorizer call will be a noop if there is no authorizer method
# defined for route. Otherwise it will raise a ForbiddenError
# which will be caught by the handler that called this and a 403 or
# 401 will be sent back over the wire.
lambda_event, lambda_context = self._authorizer.authorize(
path, lambda_event, lambda_context)
response = self._app_object(lambda_event, lambda_context)
return response