2

我需要根据 Cognito 用户的要求为他们启用 MFA。我尝试了 SMS MFA 并且效果很好,但是当涉及到软件 MFA (SOFTWARE_TOKEN_MFA)时,我找不到任何合适的文档或示例来说明如何通过代码启用它。通过 Javascript 或 python (Boto3) 在此处输入图像描述

上述图片代表了我对 Cognito 用户池的 MFA 设置。我尝试了一些 javascript 示例,但有些函数抛出了错误


cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function(result) {
        var accessToken = result.getAccessToken().getJwtToken();
    },

    onFailure: function(err) {
        alert(err.message || JSON.stringify(err));
    },

    mfaSetup: function(challengeName, challengeParameters) {
        cognitoUser.associateSoftwareToken(this);
    },

    associateSecretCode: function(secretCode) {
        var challengeAnswer = prompt('Please input the TOTP code.', '');
        cognitoUser.verifySoftwareToken(challengeAnswer, 'My TOTP device', this);
    },

    selectMFAType: function(challengeName, challengeParameters) {
        var mfaType = prompt('Please select the MFA method.', ''); // valid values for mfaType is "SMS_MFA", "SOFTWARE_TOKEN_MFA"
        cognitoUser.sendMFASelectionAnswer(mfaType, this);
    },

    totpRequired: function(secretCode) {
        var challengeAnswer = prompt('Please input the TOTP code.', '');
        cognitoUser.sendMFACode(challengeAnswer, this, 'SOFTWARE_TOKEN_MFA');
    },

    mfaRequired: function(codeDeliveryDetails) {
        var verificationCode = prompt('Please input verification code', '');
        cognitoUser.sendMFACode(verificationCode, this);
    },
});

cognitoUser.sendMFASelectionAnswer(mfaType, this);
抛出错误

        var challengeAnswer = prompt('Please input the TOTP code.', '');
        cognitoUser.verifySoftwareToken(challengeAnswer, 'My TOTP device', this);
    }

抛出错误

我什至尝试了同样的方法来从 python 启用它

response = client.set_user_mfa_preference(
                SMSMfaSettings={
                    'Enabled': True|False,
                    'PreferredMfa': True|False
                },
                SoftwareTokenMfaSettings={
                    'Enabled': True|False,
                    'PreferredMfa': True|False
                },
                AccessToken=token_
            )

但它说无效的访问令牌,token_ = 'eqQwo59dnjwj*******'

4

1 回答 1

2

在使用 boto3 ( Python ) 对 cognito 进行了详细研究后,我找到了启用软件 MFA 的解决方案

  1. 将软件令牌与用户关联
response = client.associate_software_token(
         AccessToken=user_as_json['access_token'],
    )

返回一个密码。使用otpauth将密码变成二维码

  1. 验证从使用中收到的令牌
response = client.verify_software_token(
            AccessToken=user_as_json['access_token'],
            UserCode='Code received from the user',
            FriendlyDeviceName='ABC'
        )
  1. 设置用户 MFA 首选项
response_1 = client.set_user_mfa_preference(
            SMSMfaSettings={
                'Enabled': False,
                'PreferredMfa': False
            },
            SoftwareTokenMfaSettings={
                'Enabled': True,
                'PreferredMfa': True
            },
            AccessToken='Access Token'
        )

注意:只有启用了其中一个 MFA 才能设置首选 MFA。

注意:两个 MFA 都可以启用,但一次只能设置一个为首选

于 2020-09-17T06:15:41.687 回答