代码之家  ›  专栏  ›  技术社区  ›  Web User

如何在非代理Lambda集成中仅传递AWS API网关请求授权头?

  •  0
  • Web User  · 技术社区  · 4 年前

    我无法将从请求的授权头派生的特定JWT授权声明信息发送到API网关REST API端点(使用非代理Lambda集成)。端点由Cognito用户池授权人保护,该授权人以Amazon作为身份提供者登录。从API Gateway的Cloudwatch执行日志中,我可以看到JWT授权人声明:

    (***) Cognito User Pool Authorizer claims from JWT: {
        at_hash=***,
        sub=***,
        cognito:groups=us-east-1_***_LoginWithAmazon,
        email_verified=false,
        iss=https://cognito-idp.us-east-1.amazonaws.com/us-east-1_***,
        cognito:username=loginwithamazon_amzn1.account.***,
        nonce=***,
        origin_jti=***,
        aud=***,
        identities={
            "dateCreated":"***",
            "userId":"amzn1.account.***",
            "providerName":"LoginWithAmazon",
            "providerType":"LoginWithAmazon",
            "issuer":null,
            "primary":"true"
        },
        token_use=id,
        auth_time=***,
        name=***,
        exp=*** UTC 2022,
        iat=*** UTC 2022,
        jti=***,
        email=***
    }
    
    (***) Method request path: {}
    (***) Method request query string: {}
    (***) Method request headers: {Authorization=******[TRUNCATED]
    (***) Method request body before transformations: 
    (***) Endpoint request URI: https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:***:function:***/invocations
    (***) Endpoint request headers: {x-amzn-lambda-integration-tag=***, Authorization=***, X-Amz-Date=***, x-amzn-apigateway-api-id=***, X-Amz-Source-Arn=arn:aws:execute-api:us-east-1:***:***/***/GET/myapi, Accept=application/json, User-Agent=AmazonAPIGateway_***, X-Amz-Security-Token=*** [TRUNCATED]
    (***) Method request body after transformations: 
    

    我定义了以下映射模板来组成一个Lambda函数事件,其中包含API请求主体、参数、头和一个上下文,该上下文保存来自JWT声明对象的特定信息片段:

    ##  See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
    ##  This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload
    #set($allParams = $input.params())
    {
    "body-json" : $input.json('$'),
    "params" : {
    #foreach($type in $allParams.keySet())
        #set($params = $allParams.get($type))
    "$type" : {
        #foreach($paramName in $params.keySet())
        "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
            #if($foreach.hasNext),#end
        #end
    }
        #if($foreach.hasNext),#end
    #end
    },
    "stage-variables" : {
    #foreach($key in $stageVariables.keySet())
    "$key" : "$util.escapeJavaScript($stageVariables.get($key))"
        #if($foreach.hasNext),#end
    #end
    },
    "context" : {
        "account-id" : "$context.identity.accountId",
        "api-id" : "$context.apiId",
        "caller" : "$context.identity.caller",
        "http-method" : "$context.httpMethod",
        "request-id" : "$context.requestId",
        "resource-id" : "$context.resourceId",
        "resource-path" : "$context.resourcePath",
        "userId": "$context.authorizer.claims.identities.userId",
        "name": "$context.authorizer.claims.name",
        "email": "$context.authorizer.claims.email",
        "email_verified": "$context.authorizer.claims.email_verified"
        }
    }
    

    当我将事件对象的JSON转储记录到Cloudwatch时,我看到一个空的JSON对象 {} .是我的映射模板造成了问题,还是我的API网关配置导致了这种行为?

    0 回复  |  直到 4 年前
        1
  •  0
  •   Web User    4 年前

    事实证明,每次更改VTL映射模板时,我都应该部署API!修改模板后只点击[Save]按钮不允许更改生效。AWS文档应该更新,因为重新部署API的时间并不明显。

    而且,由于 identities JWT authorizer声明中的关键是一个字符串,我需要在 userId 所有物

    "userId": "$util.parseJson($context.authorizer.claims.identities).userId",
    
    推荐文章