我需要为认知用户提供MFA,以满足他们的需求。我尝试过SMS MFA &它工作得很好,但是当涉及到Software (SOFTWARE_TOKEN_MFA)时,我找不到任何关于如何通过代码启用它的适当文档或示例。通过Javascript或python (Boto3)

上面提到的图片代表了我的认知用户池的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*’
发布于 2020-09-17 06:15:41
在详细研究了boto3 (Python)的认知之后,我找到了一个启用软件MFA的解决方案
response = client.associate_software_token(
AccessToken=user_as_json['access_token'],
)返回一个秘密代码。使用otpauth将秘密代码转换为qrcode。
接收的令牌
response = client.verify_software_token(
AccessToken=user_as_json['access_token'],
UserCode='Code received from the user',
FriendlyDeviceName='ABC'
)response_1 = client.set_user_mfa_preference(
SMSMfaSettings={
'Enabled': False,
'PreferredMfa': False
},
SoftwareTokenMfaSettings={
'Enabled': True,
'PreferredMfa': True
},
AccessToken='Access Token'
)注意:只有当启用了首选的MFA时,才能设置它。
注意:可以同时启用和MFA,但一次只能将一个设置为首选。
https://stackoverflow.com/questions/63885420
复制相似问题