Skip to content

Commit 396d0ec

Browse files
authored
fix(auth): verifyTotp throw EnableSoftwareTokenMfaException (#4558)
1 parent e964950 commit 396d0ec

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

packages/auth/amplify_auth_cognito/example/integration_test/mfa_totp_optional_test.dart

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
45
import 'package:amplify_auth_integration_test/amplify_auth_integration_test.dart';
56
import 'package:amplify_flutter/amplify_flutter.dart';
67
import 'package:amplify_integration_test/amplify_integration_test.dart';
@@ -117,9 +118,15 @@ void main() {
117118

118119
final totpSetupResult = await Amplify.Auth.setUpTotp();
119120

120-
await Amplify.Auth.verifyTotpSetup(
121-
'555555',
122-
);
121+
try {
122+
await Amplify.Auth.verifyTotpSetup('555555');
123+
fail('Expected to fail');
124+
} on AuthException catch (e) {
125+
check(
126+
e,
127+
because: 'Invalid TOTP code should fail verification',
128+
).isA<EnableSoftwareTokenMfaException>();
129+
}
123130

124131
check(
125132
await cognitoPlugin.fetchMfaPreference(),
@@ -131,9 +138,13 @@ void main() {
131138
),
132139
);
133140

134-
await Amplify.Auth.verifyTotpSetup(
135-
await generateTotpCode(totpSetupResult.sharedSecret),
136-
);
141+
try {
142+
await Amplify.Auth.verifyTotpSetup(
143+
await generateTotpCode(totpSetupResult.sharedSecret),
144+
);
145+
} on Exception catch (e) {
146+
fail('Expected to succeed, but got $e');
147+
}
137148

138149
check(await cognitoPlugin.fetchMfaPreference()).equals(
139150
const UserMfaPreference(

packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,12 +932,24 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
932932
defaultPluginOptions: const CognitoVerifyTotpSetupPluginOptions(),
933933
);
934934
final machine = _stateMachine.getOrCreate(TotpSetupStateMachine.type);
935-
await machine.dispatchAndComplete<TotpSetupState>(
935+
final state = await machine.dispatchAndComplete<TotpSetupState>(
936936
TotpSetupEvent.verify(
937937
code: totpCode,
938938
friendlyDeviceName: pluginOptions.friendlyDeviceName,
939939
),
940940
);
941+
942+
switch (state) {
943+
case TotpSetupRequiresVerification _:
944+
// TODO(equartey): Change to `CodeMismatchException` in next major version as breaking change
945+
throw const EnableSoftwareTokenMfaException(
946+
'The code provided was incorrect, try again',
947+
);
948+
case TotpSetupFailure(:final exception, :final stackTrace):
949+
Error.throwWithStackTrace(exception, stackTrace);
950+
default:
951+
return;
952+
}
941953
}
942954

943955
@override

packages/auth/amplify_auth_cognito_dart/lib/src/state/machines/totp_setup_state_machine.dart

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import 'package:amplify_auth_cognito_dart/src/jwt/src/cognito.dart';
55
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart'
6-
hide EnableSoftwareTokenMfaException;
6+
hide EnableSoftwareTokenMfaException, CodeMismatchException;
77
import 'package:amplify_auth_cognito_dart/src/sdk/sdk_bridge.dart';
88
import 'package:amplify_auth_cognito_dart/src/sdk/sdk_exception.dart';
99
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
@@ -76,6 +76,7 @@ final class TotpSetupStateMachine
7676
Future<void> _onVerify(TotpSetupVerify event) async {
7777
final tokens = await manager.getUserPoolTokens();
7878
final accessToken = tokens.accessToken.raw;
79+
7980
try {
8081
await _cognitoIdp
8182
.verifySoftwareToken(
@@ -87,16 +88,16 @@ final class TotpSetupStateMachine
8788
),
8889
)
8990
.result;
90-
} on Exception catch (e, st) {
91+
} on Exception catch (e) {
9192
// Handle mismatch code exception that may occur during TOTP verification.
9293
// See: https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_VerifySoftwareToken.html#API_VerifySoftwareToken_Errors
93-
if (e is EnableSoftwareTokenMfaException) {
94+
if (e is EnableSoftwareTokenMfaException || e is CodeMismatchException) {
9495
assert(
9596
_details != null,
9697
'TotpSetupDetails should not be null. Please report this issue.',
9798
);
9899
logger.verbose(
99-
'Failed to verify TOTP code. Retrying...',
100+
'Failed to verify TOTP code. Allowing retry...',
100101
e,
101102
);
102103
emit(
@@ -106,12 +107,7 @@ final class TotpSetupStateMachine
106107
);
107108
return;
108109
}
109-
logger.error(
110-
'Failed to verify TOTP code. Please try again.',
111-
e,
112-
st,
113-
);
114-
emit(TotpSetupState.failure(e, st));
110+
rethrow;
115111
}
116112

117113
try {

0 commit comments

Comments
 (0)