AWS API Gateway Custom Authorizer with Proxy setup - Add Custom Headers to Request

折月煮酒 提交于 2019-12-10 20:21:33

问题


What I have:

  1. AWS API gateway setup as a proxy (/{proxy+})
  2. A custom Auth function which authorizes the incoming request for this proxy setup.
  3. The custom auth function is passing the additional information I want to pass along to the request via the "context" object, like so:

    { "principalId": "yyyyyyyy", "policyDocument": { "Version": "2012-10-17", "Statement": [ { "Action": "execute-api:Invoke", "Effect": "Allow|Deny", "Resource": "some arn" } ] }, "context": { "customInfo1": "hello", "customInfo2": "world" } }

What I need:

  1. I need to pass the custom information passed in the context object above into custom headers into the request as it passes along to the target function.

What I know:

  1. If this wasn't a proxy, I could have used a mapping template to get the desired result.

回答1:


If you check this document, you will find you can create a custom Model to map from body to header and vice versa. You can then assign this model under Method Request -> Request Body.




回答2:


Figured it out, AWS passes this to Lambda when configured as a proxy:

{
    "resource": "/{proxy+}",
    "path": "/echo",
    "httpMethod": "POST",
    "headers": {
        "Accept-Type": "application/json",
        "Authorization": "Bearer xxx",
        "CloudFront-Forwarded-Proto": "https",
        "CloudFront-Is-Desktop-Viewer": "true",
        "CloudFront-Is-Mobile-Viewer": "false",
        "CloudFront-Is-SmartTV-Viewer": "false",
        "CloudFront-Is-Tablet-Viewer": "false",
        "CloudFront-Viewer-Country": "IN",
        "Content-Type": "application/json",
        "Host": "yyy.execute-api.us-east-1.amazonaws.com",
        "User-Agent": "Fiddler",
        "Via": "1.1 aaa.cloudfront.net (CloudFront)",
        "X-Amz-Cf-Id": "uuu",
        "X-Amzn-Trace-Id": "Root=1-58e5w17a-58ff31a846954e0f2aa7cd2c",
        "X-Forwarded-For": "115.112.36.246, 54.182.242.113",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https"
    },
    "queryStringParameters": null,
    "pathParameters": {
        "proxy": "echo"
    },
    "stageVariables": null,
    "requestContext": {
        "accountId": "1234567890",
        "resourceId": "1t2w8a",
        "stage": "dev",
        "authorizer": {
            "customKey": "1",
            "eee": "1",
            "principalId": "2",
            "otherkey": "hello",
            "somekey": "1,2"
        },
        "requestId": "qqq",
        "identity": {
            "cognitoIdentityPoolId": null,
            "accountId": null,
            "cognitoIdentityId": null,
            "caller": null,
            "apiKey": null,
            "sourceIp": "aaa.bbb.qq.www",
            "accessKey": null,
            "cognitoAuthenticationType": null,
            "cognitoAuthenticationProvider": null,
            "userArn": null,
            "userAgent": "Fiddler",
            "user": null
        },
        "resourcePath": "/{proxy+}",
        "httpMethod": "POST",
        "apiId": "123"
    },
    "body": "{\"ola\": \"\"}",
    "isBase64Encoded": false
}

In the requestContext section above, all the keys that I passed via my custom authorizer is already present.



来源:https://stackoverflow.com/questions/43222052/aws-api-gateway-custom-authorizer-with-proxy-setup-add-custom-headers-to-reque

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!