2017-09-29 76 views
0

从网页登录到Cognito的工作原理是,我同时获得访问令牌和id令牌。现在我想在登录时运行Lambda函数并访问用户的一些数据,但是在这里它失败了.. 我得到了InvalidLambdaResponseException: Invalid lambda trigger sourceAWS Java Lambda Cognito - 无效的lambda触发源

这是什么原因引发的任何想法?

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

你用什么Cognito LAMBDA触发?你有任何请求ID,Aws地区和时间戳可用? –

+0

@VasileiosLekakis在用户池/“我的池”/触发器中,我选择了“后验证”下的我的lambda函数。 Im使用javascript登录(请参阅最新的更新) –

回答

1

没有太多了解的你正在尝试做的具体细节,并根据你所得到的错误返回我认为triggerSource没有值中的一个值:

PreSignUp_SignUp,PostConfirmation_ConfirmSignUp,PostConfirmation_ConfirmForgotPasswor d,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

现在,这不工作的原因是CognitoEvent是一个模板(例如)CognitoSync服务,而不是您使用的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": {} 
}; 
+0

因此,我应该在请求中发送例如“PostAuthentication_Authentication”? ...这很有道理:) –

+0

不确定你的意思是“triggerSource没有值”。政策? LAMBDA?我仍然有相同的错误 –

+0

你可以尝试我们在文档中的后验证的例子,看看你是否得到相同的错误? –