Skip to content

Commit 4a5d9ed

Browse files
chore(auth): add email otp mfa enums and types (#5237)
chore(auth): add email otp mfa enums and types (#5237)
1 parent a0a8b6e commit 4a5d9ed

File tree

166 files changed

+1209
-285
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+1209
-285
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474

7575
## Generated SDK files
7676
packages/**/lib/src/sdk/src/** linguist-generated
77+
packages/auth/amplify_auth_cognito_dart/lib/src/sdk/sdk_exception.dart linguist-generated
7778

7879
## Generated Swift Plugins
7980
packages/amplify_datastore/ios/internal/** linguist-generated

packages/amplify_core/doc/lib/auth.dart

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,38 @@ Future<void> resendSignUpCode(String username) async {
104104
}
105105
// #enddocregion resend-signup-code
106106

107-
// #docregion handle-signin, handle-confirm-signin-sms, handle-confirm-signin-new-password, handle-confirm-signin-custom-challenge, handle-confirm-signin-reset-password, handle-confirm-signin-confirm-signup, handle-confirm-signin-done, handle-confirm-signin-mfa-selection, handle-confirm-signin-totp-setup, handle-confirm-signin-totp-code
107+
// #docregion handle-signin, handle-confirm-signin-sms, handle-confirm-signin-new-password, handle-confirm-signin-custom-challenge, handle-confirm-signin-reset-password, handle-confirm-signin-confirm-signup, handle-confirm-signin-done, handle-confirm-signin-mfa-selection, handle-confirm-signin-totp-setup, handle-confirm-signin-totp-code, handle-confirm-signin-email-code, handle-confirm-signin-mfa-setup-selection, handle-confirm-signin-email-setup
108108
Future<void> _handleSignInResult(SignInResult result) async {
109109
switch (result.nextStep.signInStep) {
110-
// #enddocregion handle-signin, handle-confirm-signin-sms, handle-confirm-signin-new-password, handle-confirm-signin-custom-challenge, handle-confirm-signin-reset-password, handle-confirm-signin-confirm-signup, handle-confirm-signin-done, handle-confirm-signin-mfa-selection, handle-confirm-signin-totp-setup, handle-confirm-signin-totp-code
110+
// #enddocregion handle-signin, handle-confirm-signin-sms, handle-confirm-signin-new-password, handle-confirm-signin-custom-challenge, handle-confirm-signin-reset-password, handle-confirm-signin-confirm-signup, handle-confirm-signin-done, handle-confirm-signin-mfa-selection, handle-confirm-signin-totp-setup, handle-confirm-signin-totp-code, handle-confirm-signin-email-code, handle-confirm-signin-mfa-setup-selection, handle-confirm-signin-email-setup
111111
// #docregion handle-confirm-signin-mfa-selection
112112
case AuthSignInStep.continueSignInWithMfaSelection:
113113
final allowedMfaTypes = result.nextStep.allowedMfaTypes!;
114114
final selection = await _promptUserPreference(allowedMfaTypes);
115115
return _handleMfaSelection(selection);
116116
// #enddocregion handle-confirm-signin-mfa-selection
117+
// #docregion handle-confirm-signin-mfa-setup-selection
118+
case AuthSignInStep.continueSignInWithMfaSetupSelection:
119+
final allowedMfaTypes = result.nextStep.allowedMfaTypes!;
120+
if (allowedMfaTypes.length == 1) {
121+
return _handleMfaSelection(allowedMfaTypes.first);
122+
}
123+
final selection = await _promptUserPreference(allowedMfaTypes);
124+
safePrint('Selected MFA type: $selection');
125+
return _handleMfaSelection(selection);
126+
// #enddocregion handle-confirm-signin-mfa-setup-selection
117127
// #docregion handle-confirm-signin-totp-setup
118128
case AuthSignInStep.continueSignInWithTotpSetup:
119129
final totpSetupDetails = result.nextStep.totpSetupDetails!;
120130
final setupUri = totpSetupDetails.getSetupUri(appName: 'MyApp');
121131
safePrint('Open URI to complete setup: $setupUri');
122132
// #enddocregion handle-confirm-signin-totp-setup
133+
// #docregion handle-confirm-signin-email-setup
134+
case AuthSignInStep.continueSignInWithEmailMfaSetup:
135+
safePrint(
136+
'Enter the email address you want to use for two-factor authentication',
137+
);
138+
// #enddocregion handle-confirm-signin-email-setup
123139
// #docregion handle-confirm-signin-totp-code
124140
case AuthSignInStep.confirmSignInWithTotpMfaCode:
125141
safePrint('Enter a one-time code from your registered Authenticator app');
@@ -129,6 +145,11 @@ Future<void> _handleSignInResult(SignInResult result) async {
129145
final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!;
130146
_handleCodeDelivery(codeDeliveryDetails);
131147
// #enddocregion handle-confirm-signin-sms
148+
// #docregion handle-confirm-signin-email
149+
case AuthSignInStep.confirmSignInWithEmailMfaCode:
150+
final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!;
151+
_handleCodeDelivery(codeDeliveryDetails);
152+
// #enddocregion handle-confirm-signin-email
132153
// #docregion handle-confirm-signin-new-password
133154
case AuthSignInStep.confirmSignInWithNewPassword:
134155
safePrint('Enter a new password to continue signing in');
@@ -158,10 +179,10 @@ Future<void> _handleSignInResult(SignInResult result) async {
158179
case AuthSignInStep.done:
159180
safePrint('Sign in is complete');
160181
// #enddocregion handle-confirm-signin-done
161-
// #docregion handle-signin, handle-confirm-signin-sms, handle-confirm-signin-new-password, handle-confirm-signin-custom-challenge, handle-confirm-signin-reset-password, handle-confirm-signin-confirm-signup, handle-confirm-signin-done, handle-confirm-signin-mfa-selection, handle-confirm-signin-totp-setup, handle-confirm-signin-totp-code
182+
// #docregion handle-signin, handle-confirm-signin-sms, handle-confirm-signin-new-password, handle-confirm-signin-custom-challenge, handle-confirm-signin-reset-password, handle-confirm-signin-confirm-signup, handle-confirm-signin-done, handle-confirm-signin-mfa-selection, handle-confirm-signin-totp-setup, handle-confirm-signin-totp-code, handle-confirm-signin-email-code, handle-confirm-signin-mfa-setup-selection, handle-confirm-signin-email-setup
162183
}
163184
}
164-
// #enddocregion handle-signin, handle-confirm-signin-sms, handle-confirm-signin-new-password, handle-confirm-signin-custom-challenge, handle-confirm-signin-reset-password, handle-confirm-signin-confirm-signup, handle-confirm-signin-done, handle-confirm-signin-mfa-selection, handle-confirm-signin-totp-setup, handle-confirm-signin-totp-code
185+
// #enddocregion handle-signin, handle-confirm-signin-sms, handle-confirm-signin-new-password, handle-confirm-signin-custom-challenge, handle-confirm-signin-reset-password, handle-confirm-signin-confirm-signup, handle-confirm-signin-done, handle-confirm-signin-mfa-selection, handle-confirm-signin-totp-setup, handle-confirm-signin-totp-code, handle-confirm-signin-email-code, handle-confirm-signin-mfa-setup-selection, handle-confirm-signin-email-setup
165186

166187
// #docregion signin
167188
Future<void> signInUser(String username, String password) async {

packages/amplify_core/lib/src/config/amplify_outputs/auth/auth_outputs.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/amplify_core/lib/src/config/amplify_outputs/auth/mfa.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ enum MfaMethod {
1111
sms,
1212

1313
@JsonValue('TOTP')
14-
totp;
14+
totp,
15+
16+
@JsonValue('EMAIL')
17+
email;
1518

1619
/// The value to pass to `Amplify.Auth.confirmSignIn` when
1720
/// selecting an MFA type.

packages/amplify_core/lib/src/config/auth/cognito/auth.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/amplify_core/lib/src/config/auth/cognito/mfa.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ enum MfaType {
99
sms,
1010

1111
@JsonValue('TOTP')
12-
totp;
12+
totp,
13+
14+
@JsonValue('EMAIL')
15+
email;
1316

1417
/// The value to pass to `Amplify.Auth.confirmSignIn` when
1518
/// selecting an MFA type.
@@ -21,6 +24,7 @@ extension ToMfaMethod on MfaType {
2124
return switch (this) {
2225
MfaType.sms => MfaMethod.sms,
2326
MfaType.totp => MfaMethod.totp,
27+
MfaType.email => MfaMethod.email,
2428
};
2529
}
2630
}

packages/amplify_core/lib/src/types/auth/sign_in/auth_next_sign_in_step.g.dart

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/amplify_core/lib/src/types/auth/sign_in/auth_sign_in_step.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,27 @@ enum AuthSignInStep {
1010
/// an MFA method.
1111
continueSignInWithMfaSelection,
1212

13+
/// The sign-in is not complete and the user must select an MFA method to setup.
14+
continueSignInWithMfaSetupSelection,
15+
1316
/// The sign-in is not complete and a TOTP authenticator app must be
1417
/// registered before continuing.
1518
continueSignInWithTotpSetup,
1619

20+
/// The sign-in is not complete and an Email MFA must be set up before
21+
/// continuing.
22+
continueSignInWithEmailMfaSetup,
23+
1724
/// The sign-in is not complete and must be confirmed with an SMS code.
1825
confirmSignInWithSmsMfaCode,
1926

2027
/// The sign-in is not complete and must be confirmed with a TOTP code
2128
/// from a registered authenticator app.
2229
confirmSignInWithTotpMfaCode,
2330

31+
/// The sign-in is not complete and must be confirmed with an email code.
32+
confirmSignInWithEmailMfaCode,
33+
2434
/// The sign-in is not complete and must be confirmed with the user's new
2535
/// password.
2636
confirmSignInWithNewPassword,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void main() {
3535
signInRes.nextStep.signInStep,
3636
because: 'MFA is required, and TOTP is chosen when '
3737
'no phone number is registered',
38-
).equals(AuthSignInStep.continueSignInWithTotpSetup);
38+
).equals(AuthSignInStep.continueSignInWithMfaSetupSelection);
3939

4040
final sharedSecret = signInRes.nextStep.totpSetupDetails!.sharedSecret;
4141
final setupRes = await Amplify.Auth.confirmSignIn(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void main() {
3434
signInRes.nextStep.signInStep,
3535
because:
3636
"TOTP MFA is automatically enabled when it's the only option",
37-
).equals(AuthSignInStep.continueSignInWithTotpSetup);
37+
).equals(AuthSignInStep.continueSignInWithMfaSetupSelection);
3838

3939
final sharedSecret = signInRes.nextStep.totpSetupDetails!.sharedSecret;
4040
final setupRes = await Amplify.Auth.confirmSignIn(

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,20 +885,22 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
885885
/// {@template amplify_core.amplify_auth_category.update_mfa_preference}
886886
/// Updates the MFA preference for the current user.
887887
///
888-
/// If [sms] or [totp] is `null`, the preference for that MFA type is left
889-
/// unchanged. Setting either [sms] or [totp] to [MfaPreference.preferred]
888+
/// If [sms], [totp], or [email] is `null`, the preference for that MFA type is left
889+
/// unchanged. Setting either [sms], [totp], or [email] to [MfaPreference.preferred]
890890
/// will mark the other as not preferred.
891891
/// {@endtemplate}
892892
Future<void> updateMfaPreference({
893893
MfaPreference? sms,
894894
MfaPreference? totp,
895+
MfaPreference? email,
895896
}) async {
896897
final tokens = await _stateMachine.getUserPoolTokens();
897898
final accessToken = tokens.accessToken.raw;
898899
return _cognitoIdp.setMfaSettings(
899900
accessToken: accessToken,
900901
sms: sms,
901902
totp: totp,
903+
email: email,
902904
);
903905
}
904906

packages/auth/amplify_auth_cognito_dart/lib/src/flows/constants.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ abstract class CognitoConstants {
1111
/// The `USERNAME` parameter.
1212
static const challengeParamUsername = 'USERNAME';
1313

14+
/// The `EMAIL` parameter.
15+
static const challengeParamEmail = 'EMAIL';
16+
1417
/// The `SRP_A` parameter.
1518
static const challengeParamSrpA = 'SRP_A';
1619

@@ -57,6 +60,9 @@ abstract class CognitoConstants {
5760
/// The `SMS_MFA_CODE` parameter.
5861
static const challengeParamSmsMfaCode = 'SMS_MFA_CODE';
5962

63+
/// The `EMAIL_OTP_CODE` parameter.
64+
static const challengeParamEmailMfaCode = 'EMAIL_OTP_CODE';
65+
6066
/// The `SOFTWARE_TOKEN_MFA_CODE` parameter.
6167
static const challengeParamSoftwareTokenMfaCode = 'SOFTWARE_TOKEN_MFA_CODE';
6268

packages/auth/amplify_auth_cognito_dart/lib/src/sdk/cognito_identity.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated with smithy-dart 0.3.1. DO NOT MODIFY.
1+
// Generated with smithy-dart 0.3.2. DO NOT MODIFY.
22
// ignore_for_file: avoid_unused_constructor_parameters,deprecated_member_use_from_same_package,non_constant_identifier_names,require_trailing_commas
33

44
/// # Amazon Cognito Identity

packages/auth/amplify_auth_cognito_dart/lib/src/sdk/cognito_identity_provider.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated with smithy-dart 0.3.1. DO NOT MODIFY.
1+
// Generated with smithy-dart 0.3.2. DO NOT MODIFY.
22
// ignore_for_file: avoid_unused_constructor_parameters,deprecated_member_use_from_same_package,non_constant_identifier_names,require_trailing_commas
33

44
/// # Amazon Cognito Identity Provider
@@ -68,6 +68,7 @@ export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/
6868
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/device_remembered_status_type.dart';
6969
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/device_secret_verifier_config_type.dart';
7070
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/device_type.dart';
71+
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/email_mfa_settings_type.dart';
7172
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/enable_software_token_mfa_exception.dart';
7273
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/expired_code_exception.dart';
7374
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/forbidden_exception.dart';
@@ -99,6 +100,7 @@ export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/
99100
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/mfa_option_type.dart';
100101
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/new_device_metadata_type.dart';
101102
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/not_authorized_exception.dart';
103+
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/password_history_policy_violation_exception.dart';
102104
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/password_reset_required_exception.dart';
103105
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/resend_confirmation_code_request.dart';
104106
export 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/resend_confirmation_code_response.dart';

0 commit comments

Comments
 (0)