AWS Lambda - 如何使用 context.identity.cognitoIdentityId 获取相关的 C



从我的 Web 客户端,我直接通过以下方式调用我的 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 的值,如此处文档中所述。下面是一个非常简单的 Lambda 函数,用于准确显示我所询问的属性:

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

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

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

不确定您是否解决了这个问题,但在 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);
 };

希望这有帮助!

最新更新