AWS Java Lambda Cognito - 无效的lambda触发源

时间:2017-09-29 12:11:29

标签: java amazon-web-services lambda aws-lambda amazon-cognito

从网页登录到Cognito,我获得了访问令牌和id令牌。现在我想在登录时运行Lambda函数并访问用户的一些数据,但这里失败了.. 我得到InvalidLambdaResponseException: Invalid lambda trigger source

关于导致这种情况的任何想法?

Java Lambda代码就是这样:

public class LambdaFunctionHandler implements RequestHandler<CognitoEvent, CognitoEvent> {

@Override
public CognitoEvent handleRequest(CognitoEvent event, Context context) 
{
    context.getLogger().log("Input: " + event);

   return event;
}

}

使用Javascript:

function loginCognito()
    {
        AWSCognito.config.region = 'us-east-1';
        var authenticationData = {
            Username : '***',
            Password : '***',
        };
        var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
        var poolData = { UserPoolId : 'us-east-1*********',
            ClientId : '*******************'
        };
        var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
        var userData = {
            Username : '***',
            Pool : userPool
        };
        var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
        cognitoUser.authenticateUser(authenticationDetails, 
        {
            onSuccess: function (result) {
                /* ... */
            },
            onFailure: function(err) {
                alert(err);
            }
        });
    }

1 个答案:

答案 0 :(得分:1)

如果不了解您尝试做的事情的具体细节,并根据您要回复的错误,我相信triggerSource没有其中一个值的值:

PreSignUp_SignUp,PostConfirmation_ConfirmSignUp,PostConfirmation_ConfirmForgotPassword,PreAuthentication_Authentication, PostAuthentication_Authentication,CustomMessage_SignUp,CustomMessage_AdminCreateUser,CustomMessage_ResendCode,CustomMessage_ForgotPassword,CustomMessage_UpdateUserAttribute,CustomMessage_VerifyUserAttribute,CustomMessage_Authentication,DefineAuthChallenge_Authentication,CreateAuthChallenge_Authentication,VerifyAuthChallengeResponse_Authentication

http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html#cognito-user-pools-lambda-trigger-syntax-shared

现在,这不起作用的原因是CognitoEventCognitoSync服务的模板(示例),而不是您使用的userPools。目前我们没有提供输入事件的JAVA示例。

要使其工作,您需要有一个可以序列化以下JSON的输入对象

{
  "version": 1,
  "triggerSource": "PostAuthentication_Authentication",
  "region": "<region>",
  "userPoolId": "<userPoolId>",
  "userName": "<userName>",
  "callerContext": {
      "awsSdk": "<calling aws sdk with version>",
      "clientId": "<apps client id>",
      ...
  },
  "request": {
      "userAttributes": {
          "phone_number_verified": true,
          "email_verified": true,
          ... //all custom attributes
      }
  },
  "response": {}
};