【问题标题】:AWS Lambda - How to use context.identity.cognitoIdentityId to get related Cognito User Pool user data?AWS Lambda - 如何使用 context.identity.cognitoIdentityId 获取相关的 Cognito 用户池用户数据?
【发布时间】:2019-02-16 23:06:59
【问题描述】:

从我的网络客户端,我直接通过以下方式调用我的 AWS Lambda 函数:

// Build the logins object for the credentials
var loginKey = "cognito-idp." + config.awsRegion + ".amazonaws.com/" + config.identity.UserPoolId;
var logins = {};
logins[loginKey] = jwtToken;

// Instantiate the credentials.
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: config.identity.IdentityPoolId,
    Logins: logins
});

// Must refresh the credentials prior to calling the function.
AWS.config.credentials.refresh(function(err) {
    ... // Error handling excluded

    var lambda = new AWS.Lambda({region: config.awsRegion, apiVersion: '2015-03-31'});
    var params = {
        FunctionName: functionName,
        InvocationType : 'RequestResponse',
        LogType : 'None',
        Payload: JSON.stringify(data)
    };

    lambda.invoke(params, function(err, data){
        ... // Data handling
    });
});

在我的 Lambda 函数中,我验证了 context.identity.cognitoIdentityId 和 context.identity.cognitoIdentityPoolId 的值,如文档 here 中所述。以下是一个非常简化的 Lambda 函数,可以准确显示我所询问的属性:

exports.handler = (event, context, callback) => {
    console.log(context.identity.cognitoIdentityId); // Has value
    console.log(context.identity.cognitoIdentityPoolId); // Has value

    callback(null, "success");
};

我认为有了这些信息,我可以获得 Cognito 用户数据,包括利用这些信息的属性,但我还没有找到方法。 This 最近的 SO 线程是我见过的最接近的,但即使他们想出了一个“变通”类型的解决方案。

有人知道使用 Lambda 函数 context.identity 数据获取 Cognito 用户数据的方法吗?谢谢!

【问题讨论】:

    标签: node.js amazon-web-services aws-lambda


    【解决方案1】:

    不确定您是否解决了这个问题,但是,在您的 API 网关中,在集成请求下,您必须设置您的身体映射模板,如果您使用应用程序/json 的“默认”模板,这会将数据传递给您的应用程序.

    您将看到它创建的“上下文”对象。然后在你的 lambda 应用中:

    节点代码:

    exports.handler = (event, context, callback) => {
    let what = event.context;
    let who = what['cognito-authentication-provider']; // or match what the template setup
    
    callback(null, "cognito provider: " + who);
     };
    

    希望这会有所帮助!

    【讨论】:

    • 这正是我正在寻找的......但目前尚不清楚如何在我的 sam 模板中设置身体映射模板。很难找到有用的例子。
    猜你喜欢
    • 2019-07-10
    • 2018-11-22
    • 2018-03-16
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 2019-05-18
    • 2021-01-22
    相关资源
    最近更新 更多