AWS ApiGateway Lambda代理访问授权者 [英] AWS ApiGateway Lambda Proxy access Authorizer

查看:262
本文介绍了AWS ApiGateway Lambda代理访问授权者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ApiGateway中使用Lambda代理和Cognito用户池授权器。在Lambda函数中,我可以通过事件对象访问路径等变量。除此之外,我还想访问经过身份验证的用户的声明。在编写的文档中,我应该使用:

I´m using an Lambda Proxy and a Cognito User Pool Authorizer in my ApiGateway. In the Lambda function I can access the path etc. variables via the event object. In addition to that I want to access the claims of the authenticated user. In the documentation it is written, that I should use:

context.authorizer.claims.property

但是我的授权者为空,所以我得到

But I authorizer is null so I get

无法读取未定义的属性声明

有人有主意吗?

推荐答案

如果您指的是这部分内容文档 $ context.authorizer.claims 是集成映射模板的一部分。它与处理程序的上下文参数无关。

If you are referring to this part of the documentation, $context.authorizer.claims is part of the mapping template of the integration. It is not related to the context argument of the handler.

使用 Lambda代理集成,您正在使用直通映射模板。 I̶t̶s̶e̶e̶m̶s̶w̶h̶a̶t̶̶i̶t̶̶d̶o̶e̶s̶s̶您可能必须禁用 Lambda代理集成,并在映射模板中使用以下类似内容:

Using Lambda Proxy integration, you are using the passthrough mapping template. I̶t̶ ̶s̶e̶e̶m̶s̶ ̶w̶h̶a̶t̶ ̶i̶t̶ ̶d̶o̶e̶s̶ ̶n̶o̶t̶ ̶i̶n̶c̶l̶u̶d̶e̶ ̶w̶h̶a̶t̶ ̶y̶o̶u̶ ̶a̶r̶e̶ ̶l̶o̶o̶k̶i̶n̶g̶ ̶f̶o̶r̶ (see edit). You'll probably have to disable Lambda Proxy integration and use something like this in the mapping template:

{
    "identity" : {
        "sub" : "$context.authorizer.claims.sub",
        "email" : "$context.authorizer.claims.email"
    }
}

映射模板 build Lambda的 event 参数。这样您就可以通过 event 参数访问索赔的各个部分。

The mapping template "build" the event parameter of the Lambda. So you will be able to access to the parts of your claim via the event parameter.

exports.handler = (event, context, callback) => {
    // TODO implement
    callback(null, event.identity.email);
};






请注意,我略微修改了文档示例,以避免关于上下文的另一个困惑可能是:


Note that I slightly modified the documentation example to avoid another confusion about what context can be:


  • API网关中的映射模板变量

  • Lambda处理程序的第二个参数

  • 事件中的键文档的一些示例< =我将其重命名为 identity

  • the mapping template variable in API Gateway
  • the second argument of a handler in Lambda
  • a key of the event argument in some examples of the documentation <= I renamed it identity

编辑

doorstuck 指出,该信息可通过代理集成使用

这篇关于AWS ApiGateway Lambda代理访问授权者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆