问题描述
我需要根据其要求为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,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,});
cognitoUser.sendMFASelectionAnswer(mfaType,this);
引发错误
var challengeAnswer = prompt('Please input the TOTP code.',this);
}
引发错误
我什至尝试通过python启用它
response = client.set_user_mfa_preference(
SMSMfaSettings={
'Enabled': True|False,'PreferredMfa': True|False
},SoftwaretokenMfaSettings={
'Enabled': True|False,Accesstoken=token_
)
但是它说无效的访问令牌, token_ ='eqQwo59dnjwj *******'
解决方法
在通过boto3( Python )对cognito进行了详细研究之后,我找到了启用软件MFA的解决方案
- 将软件令牌与用户相关联
response = client.associate_software_token(
AccessToken=user_as_json['access_token'],)
返回一个密码。使用otpauth将密码转换为qrcode
- 验证从使用中收到的令牌
response = client.verify_software_token(
AccessToken='Access Token',UserCode=]'Code received from the user',FriendlyDeviceName='ABC'
)
- 设置用户MFA首选项
response_1 = client.set_user_mfa_preference(
SMSMfaSettings={
'Enabled': False,'PreferredMfa': False
},SoftwareTokenMfaSettings={
'Enabled': True,'PreferredMfa': True
},AccessToken='Access Token'
)
注意:只有在启用了任一MFA的情况下,才能设置首选MFA。
注意:可以同时启用两个MFA,但在以下位置只能将一个MFA设置为首选 时间