AWS Compute Blog
Using Enhanced Request Authorizers in Amazon API Gateway
Recently, AWS introduced a new type of authorizer in Amazon API Gateway, enhanced request authorizers. Previously, custom authorizers received only the bearer token included in the request and the ARN of the API Gateway method being called. Enhanced request authorizers receive all of the headers, query string, and path parameters as well as the request context. This enables you to make more sophisticated authorization decisions based on parameters such as the client IP address, user agent, or a query string parameter alongside the client bearer token.
Enhanced request authorizer configuration
From the API Gateway console, you can declare a new enhanced request authorizer by selecting the Request option as the AWS Lambda event payload:
Just like normal custom authorizers, API Gateway can cache the policy returned by your Lambda function. With enhanced request authorizers, however, you can also specify the values that form the unique key of a policy in the cache. For example, if your authorization decision is based on both the bearer token and the IP address of the client, both values should be part of the unique key in the policy cache. The identity source parameter lets you specify these values as mapping expressions:
- The bearer token appears in the Authorization header
- The client IP address is stored in the sourceIp parameter of the request context.
Using enhanced request authorizers with Swagger
You can also define enhanced request authorizers in your Swagger (Open API) definitions. In the following example, you can see that all of the options configured in the API Gateway console are available as custom extensions in the API definition. For example, the identitySource field is a comma-separated list of mapping expressions.
securityDefinitions:
IpAuthorizer:
type: "apiKey"
name: "IpAuthorizer"
in: "header"
x-amazon-apigateway-authtype: "custom"
x-amazon-apigateway-authorizer:
authorizerResultTtlInSeconds: 300
identitySource: "method.request.header.Authorization, context.identity.sourceIp"
authorizerUri: "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:XXXXXXXXXX:function:py-ip-authorizer/invocations"
type: "request"
After you have declared your authorizer in the security definitions section, you can use it in your API methods:
---
swagger: "2.0"
info:
title: "request-authorizer-demo"
basePath: "/dev"
paths:
/hello:
get:
security:
- IpAuthorizer: []
...
Enhanced request authorizer Lambda functions
Enhanced request authorizer Lambda functions receive an event object that is similar to proxy integrations. It contains all of the information about a request, excluding the body.
{
"methodArn": "arn:aws:execute-api:us-east-1:XXXXXXXXXX:xxxxxx/dev/GET/hello",
"resource": "/hello",
"requestContext": {
"resourceId": "xxxx",
"apiId": "xxxxxxxxx",
"resourcePath": "/hello",
"httpMethod": "GET",
"requestId": "9e04ff18-98a6-11e7-9311-ef19ba18fc8a",
"path": "/dev/hello",
"accountId": "XXXXXXXXXXX",
"identity": {
"apiKey": "",
"sourceIp": "58.240.196.186"
},
"stage": "dev"
},
"queryStringParameters": {},
"httpMethod": "GET",
"pathParameters": {},
"headers": {
"cache-control": "no-cache",
"x-amzn-ssl-client-hello": "AQACJAMDAAAAAAAAAAAAAAAAAAAAAAAAAAAA…",
"Accept-Encoding": "gzip, deflate",
"X-Forwarded-For": "54.240.196.186, 54.182.214.90",
"Accept": "*/*",
"User-Agent": "PostmanRuntime/6.2.5",
"Authorization": "hello"
},
"stageVariables": {},
"path": "/hello",
"type": "REQUEST"
}
The following enhanced request authorizer snippet is written in Python and compares the source IP address against a list of valid IP addresses. The comments in the code explain what happens in each step.
...
VALID_IPS = ["58.240.195.186", "201.246.162.38"]
def lambda_handler(event, context):
# Read the client’s bearer token.
jwtToken = event["headers"]["Authorization"]
# Read the source IP address for the request form
# for the API Gateway context object.
clientIp = event["requestContext"]["identity"]["sourceIp"]
# Verify that the client IP address is allowed.
# If it’s not valid, raise an exception to make sure
# that API Gateway returns a 401 status code.
if clientIp not in VALID_IPS:
raise Exception('Unauthorized')
# Only allow hello users in!
if not validate_jwt(userId):
raise Exception('Unauthorized')
# Use the values from the event object to populate the
# required parameters in the policy object.
policy = AuthPolicy(userId, event["requestContext"]["accountId"])
policy.restApiId = event["requestContext"]["apiId"]
policy.region = event["methodArn"].split(":")[3]
policy.stage = event["requestContext"]["stage"]
# Use the scopes from the bearer token to make a
# decision on which methods to allow in the API.
policy.allowMethod(HttpVerb.GET, '/hello')
# Finally, build the policy.
authResponse = policy.build()
return authResponse
...
Conclusion
API Gateway customers build complex APIs, and authorization decisions often go beyond the simple properties in a JWT token. For example, users may be allowed to call the “list cars” endpoint but only with a specific subset of filter parameters. With enhanced request authorizers, you have access to all request parameters. You can centralize all of your application’s access control decisions in a Lambda function, making it easier to manage your application security.