简体   繁体   English

AWS Lambda-如何使用context.identity.cognitoIdentityId获取相关的Cognito用户池用户数据?

[英]AWS Lambda - How to use context.identity.cognitoIdentityId to get related Cognito User Pool user data?

From my web client I call my AWS Lambda functions directly via: 从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
    });
});

In my Lambda functions I verify there are values for context.identity.cognitoIdentityId and context.identity.cognitoIdentityPoolId as described in the documentation here . 在我的lambda函数我验证,如文档中描述的有用于context.identity.cognitoIdentityId和context.identity.cognitoIdentityPoolId值这里 The following is a very simplified Lambda function to show exactly the properties I am asking about: 以下是一个非常简化的Lambda函数,以准确显示我要询问的属性:

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

    callback(null, "success");
};

I figured with this information I could get the Cognito User data, including attributes utilizing this information but I am yet to find a way. 我用这些信息可以得到Cognito用户数据,包括利用此信息的属性,但我还没有找到解决方法。 This recent SO thread was the closest I have seen, but even they came up with a "work around" type solution. 这个最近的SO线程是我所见过的最接近的线程,但即使他们提出了“变通”类型的解决方案。

Does anyone know a way to get the Cognito User data utilizing the Lambda function context.identity data? 有谁知道利用Lambda函数context.identity数据获取Cognito用户数据的方法吗? Thanks! 谢谢!

Not sure if you solved this but, in your API Gateway, under Integration Request, You must set your Body Mapping Templates, if you use the "default" template for application/json, this will pass along the data to your app. 不确定是否解决了该问题,但是在API网关的“集成请求”下,您必须设置主体映射模板,如果将“默认”模板用于application / json,则会将数据传递给您的应用程序。

You'll see a "context" object it creates. 您将看到它创建的“上下文”对象。 Then in your lambda app: 然后在您的lambda应用中:

Node Code: 节点代码:

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);
 };

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM