我需要根据 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*******'