具有多个登录选项的 aws 认知池

sur*_*yan 7 amazon-web-services aws-cognito

我有一个移动应用程序,想使用 AWS Cognito 池进行用户管理(注册和登录)。我想为用户提供以下 3 个选项来登录我的应用程序

  1. 用户名密码
  2. 带有 OTP 登录的电话号码 - 在登录屏幕上,用户输入他的电话号码,Cognito 应发送 OTP 代码,并在验证时允许登录
  3. 谷歌连接登录

在注册过程中,用户将设置用户名、密码并添加经过验证的电话号码,并且可以选择将他们的谷歌连接添加到他们的个人资料中。

如何为这种场景设置 Cognito 池,用户可以选择上述 3 个选项中的任何一个来登录应用程序?

sur*_*yan 5

我找到了一种设置 Cognito 以允许多个登录选项的方法。 如下设置 Cognito 1. 选择使用电话号码作为用户名 2. 使其成为强制性和可验证的。3. 这将使 phone_number 作为登录别名。

使用 CUSTOM_CHALLENGE 选项配置带有 OTP 的电话号码登录。

基本上,我们需要在 Cognito 中配置 3 个触发器来向用户注册号码发送 OTP。1. 登录定义身份验证挑战触发器——定义 CUSTOM_CHALLENGE 2. 登录创建身份验证挑战触发器——创建使用 SNS 服务生成 OTP 和发送短信的逻辑 3. 登录验证身份验证挑战触发器——验证收到的 OTP,生成的 OTP 将在上下文中可用,因此无需保存在任何数据库中。

触发器#1 - 定义身份验证挑战

exports.handler = (event, context, callback) => {

    if (event.request.session.length == 0){

        event.response.issueTokens = false;
        event.response.failAuthentication = false;
        event.response.challengeName = 'CUSTOM_CHALLENGE';

    } else if(event.request.session.length == 1 
        && event.request.session[0].challengeName == 'CUSTOM_CHALLENGE' 
        && event.request.session[0].challengeResult == true){

        event.response.issueTokens = true;
        event.response.failAuthentication = false;

    } else {

        event.response.issueTokens = false;
        event.response.failAuthentication = true;
    }

     // Return to Amazon Cognito
    callback(null, event);
}
Run Code Online (Sandbox Code Playgroud)

触发器#2 - 创建身份验证挑战,确保此 lambda 具有 SNS 角色

var AWS = require("aws-sdk");
exports.handler = (event, context, callback) => {
    if (event.request.session.length == 0 && event.request.challengeName == 'CUSTOM_CHALLENGE') {

        //create the code 
        var answer = Math.random().toString(10).substr(2,6);

        //send the code via Amazon SNS Global SMS
        var sns = new AWS.SNS();
        sns.publish({
              Message: 'your verification code is '+answer,
              PhoneNumber: event.request.userAttributes.phone_number
            }, function(err, data) {
                if (err){ 

                    console.log(err, err.stack); // an error occurred
                    return;
                }
                console.log('SMS Sent');           // successful response
        });

        //set the return parameters **including the correct answer**

        event.response.publicChallengeParameters = {};
        event.response.privateChallengeParameters = {};
        event.response.privateChallengeParameters.answer = answer;
        event.response.challengeMetadata = 'PASSWORDLESS_CHALLENGE';
    }
    //Return to Amazon Cognito
    callback(null, event);

}
Run Code Online (Sandbox Code Playgroud)

触发器#3 - 验证身份验证质询响应

exports.handler = (event, context, callback) => {
    if (event.request.privateChallengeParameters.answer == event.request.challengeAnswer) {
        event.response.answerCorrect = true;
    } else {
        event.response.answerCorrect = false;
    }
    // Return to Amazon Cognito
    callback(null, event);
}
Run Code Online (Sandbox Code Playgroud)