@@ -3210,6 +3210,7 @@ def user_authentication_flow(
32103210 refresh_token = result ["AuthenticationResult" ]["RefreshToken" ]
32113211
32123212 # add mfa token
3213+ secret_code = None
32133214 if with_mfa :
32143215 resp = conn .associate_software_token (
32153216 AccessToken = result ["AuthenticationResult" ]["AccessToken" ]
@@ -3284,6 +3285,7 @@ def user_authentication_flow(
32843285 "client_id" : client_id ,
32853286 "client_secret" : client_secret ,
32863287 "secret_hash" : secret_hash ,
3288+ "secret_code" : secret_code ,
32873289 "id_token" : result ["AuthenticationResult" ]["IdToken" ],
32883290 "access_token" : result ["AuthenticationResult" ]["AccessToken" ],
32893291 "refresh_token" : refresh_token ,
@@ -4842,6 +4844,7 @@ def test_initiate_auth_USER_PASSWORD_AUTH_when_software_token_mfa_enabled():
48424844 password = result ["password" ]
48434845 client_id = result ["client_id" ]
48444846 secret_hash = result ["secret_hash" ]
4847+ secret_code = result ["secret_code" ]
48454848
48464849 result = conn .admin_get_user (UserPoolId = user_pool_id , Username = username )
48474850 assert result ["PreferredMfaSetting" ] == "SOFTWARE_TOKEN_MFA"
@@ -4856,12 +4859,15 @@ def test_initiate_auth_USER_PASSWORD_AUTH_when_software_token_mfa_enabled():
48564859 assert result ["ChallengeParameters" ] == {}
48574860 assert result ["Session" ] is not None
48584861
4862+ totp = pyotp .TOTP (secret_code )
4863+ user_code = totp .now ()
4864+
48594865 result = conn .respond_to_auth_challenge (
48604866 ClientId = client_id ,
48614867 ChallengeName = "SOFTWARE_TOKEN_MFA" ,
48624868 Session = result ["Session" ],
48634869 ChallengeResponses = {
4864- "SOFTWARE_TOKEN_MFA_CODE" : "123456" ,
4870+ "SOFTWARE_TOKEN_MFA_CODE" : user_code ,
48654871 "USERNAME" : username ,
48664872 "SECRET_HASH" : secret_hash ,
48674873 },
@@ -5371,65 +5377,6 @@ def test_admin_setting_mfa_totp_and_sms():
53715377 assert result ["PreferredMfaSetting" ] == ""
53725378
53735379
5374- @mock_aws
5375- def test_admin_initiate_auth_when_token_totp_masked ():
5376- conn = boto3 .client ("cognito-idp" , "us-west-2" )
5377-
5378- result = authentication_flow (conn , "ADMIN_NO_SRP_AUTH" )
5379- access_token = result ["access_token" ]
5380- user_pool_id = result ["user_pool_id" ]
5381- username = result ["username" ]
5382- client_id = result ["client_id" ]
5383- password = result ["password" ]
5384- resp = conn .associate_software_token (AccessToken = access_token )
5385- secret_code = resp ["SecretCode" ]
5386- totp = pyotp .TOTP (secret_code )
5387- user_code = totp .now ()
5388- conn .verify_software_token (AccessToken = access_token , UserCode = user_code )
5389-
5390- # Set MFA TOTP and SMS methods
5391- conn .admin_set_user_mfa_preference (
5392- Username = username ,
5393- UserPoolId = user_pool_id ,
5394- SoftwareTokenMfaSettings = {"Enabled" : True , "PreferredMfa" : True },
5395- SMSMfaSettings = {"Enabled" : True , "PreferredMfa" : False },
5396- )
5397- result = conn .admin_get_user (UserPoolId = user_pool_id , Username = username )
5398- assert len (result ["UserMFASettingList" ]) == 2
5399- assert result ["PreferredMfaSetting" ] == "SOFTWARE_TOKEN_MFA"
5400-
5401- # Initiate auth with TOTP
5402- result = conn .admin_initiate_auth (
5403- UserPoolId = user_pool_id ,
5404- ClientId = client_id ,
5405- AuthFlow = "ADMIN_NO_SRP_AUTH" ,
5406- AuthParameters = {
5407- "USERNAME" : username ,
5408- "PASSWORD" : password ,
5409- },
5410- )
5411-
5412- assert result ["ChallengeName" ] == "SOFTWARE_TOKEN_MFA"
5413- assert result ["Session" ] != ""
5414-
5415- # Respond to challenge with TOTP
5416- result = conn .admin_respond_to_auth_challenge (
5417- UserPoolId = user_pool_id ,
5418- ClientId = client_id ,
5419- ChallengeName = "SOFTWARE_TOKEN_MFA" ,
5420- Session = result ["Session" ],
5421- ChallengeResponses = {
5422- "SOFTWARE_TOKEN_MFA_CODE" : "123456" ,
5423- "USERNAME" : username ,
5424- },
5425- )
5426-
5427- assert result ["AuthenticationResult" ]["IdToken" ] != ""
5428- assert result ["AuthenticationResult" ]["AccessToken" ] != ""
5429- assert result ["AuthenticationResult" ]["RefreshToken" ] != ""
5430- assert result ["AuthenticationResult" ]["TokenType" ] == "Bearer"
5431-
5432-
54335380@mock_aws
54345381@mock .patch .dict (os .environ , {"MOTO_COGNITO_IDP_USER_POOL_ENABLE_TOTP" : "true" })
54355382def test_admin_initiate_auth_when_token_totp_enabled ():
0 commit comments