2017-09-29 90 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

你可以嘗試我們在文檔中的後驗證的例子,看看你是否得到相同的錯誤? –