2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
import 'package:amplify_auth_cognito_dart/src/jwt/src/cognito.dart' ;
5
- import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart' ;
5
+ import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart'
6
+ hide EnableSoftwareTokenMfaException;
6
7
import 'package:amplify_auth_cognito_dart/src/sdk/sdk_bridge.dart' ;
8
+ import 'package:amplify_auth_cognito_dart/src/sdk/sdk_exception.dart' ;
7
9
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart' ;
8
10
import 'package:amplify_auth_cognito_dart/src/state/state.dart' ;
9
11
import 'package:amplify_core/amplify_core.dart' ;
@@ -48,6 +50,7 @@ final class TotpSetupStateMachine
48
50
CognitoIdentityProviderClient get _cognitoIdp => expect ();
49
51
50
52
String ? _session;
53
+ TotpSetupDetails ? _details;
51
54
52
55
Future <void > _onInitiate (TotpSetupInitiate event) async {
53
56
final tokens = await manager.getUserPoolTokens ();
@@ -59,29 +62,58 @@ final class TotpSetupStateMachine
59
62
)
60
63
.result;
61
64
_session = response.session;
65
+ _details = TotpSetupDetails (
66
+ username: CognitoIdToken (tokens.idToken).username,
67
+ sharedSecret: response.secretCode! ,
68
+ );
62
69
emit (
63
70
TotpSetupState .requiresVerification (
64
- TotpSetupDetails (
65
- username: CognitoIdToken (tokens.idToken).username,
66
- sharedSecret: response.secretCode! ,
67
- ),
71
+ _details! ,
68
72
),
69
73
);
70
74
}
71
75
72
76
Future <void > _onVerify (TotpSetupVerify event) async {
73
77
final tokens = await manager.getUserPoolTokens ();
74
78
final accessToken = tokens.accessToken.raw;
75
- await _cognitoIdp
76
- .verifySoftwareToken (
77
- VerifySoftwareTokenRequest (
78
- accessToken: accessToken,
79
- session: _session,
80
- userCode: event.code,
81
- friendlyDeviceName: event.friendlyDeviceName,
79
+ try {
80
+ await _cognitoIdp
81
+ .verifySoftwareToken (
82
+ VerifySoftwareTokenRequest (
83
+ accessToken: accessToken,
84
+ session: _session,
85
+ userCode: event.code,
86
+ friendlyDeviceName: event.friendlyDeviceName,
87
+ ),
88
+ )
89
+ .result;
90
+ } on Exception catch (e, st) {
91
+ // Handle mismatch code exception that may occur during TOTP verification.
92
+ // See: https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_VerifySoftwareToken.html#API_VerifySoftwareToken_Errors
93
+ if (e is EnableSoftwareTokenMfaException ) {
94
+ assert (
95
+ _details != null ,
96
+ 'TotpSetupDetails should not be null. Please report this issue.' ,
97
+ );
98
+ logger.verbose (
99
+ 'Failed to verify TOTP code. Retrying...' ,
100
+ e,
101
+ );
102
+ emit (
103
+ TotpSetupState .requiresVerification (
104
+ _details! ,
82
105
),
83
- )
84
- .result;
106
+ );
107
+ return ;
108
+ }
109
+ logger.error (
110
+ 'Failed to verify TOTP code. Please try again.' ,
111
+ e,
112
+ st,
113
+ );
114
+ emit (TotpSetupState .failure (e, st));
115
+ }
116
+
85
117
try {
86
118
await _cognitoIdp.setMfaSettings (
87
119
accessToken: accessToken,
0 commit comments