Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(core): updated asyncConfig to use AmplifyOutputs #4995

Merged
merged 23 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8fb9e66
feat(core): add AmplifyOutputs types based on the CLI Gen2 client con…
NikaHsn May 22, 2024
b9b3515
chore: map AmplifyConfig to AmplifyOutputs (#4935)
Jordan-Nelson May 30, 2024
76ae8e0
chore: support multiple graphql or rest APIs (#4982)
Jordan-Nelson Jun 5, 2024
e7d77ea
chore(core): updated asyncConfig to use AmplifyOutputs
tyllark Jun 7, 2024
4e7f792
feat(core): add AmplifyOutputs types based on the CLI Gen2 client con…
NikaHsn May 22, 2024
f6cc0a4
chore: map AmplifyConfig to AmplifyOutputs (#4935)
Jordan-Nelson May 30, 2024
bbf6e72
chore: support multiple graphql or rest APIs (#4982)
Jordan-Nelson Jun 5, 2024
aa7140a
chore(core): updated exports to expose MFA/Password Policies/Identity…
tyllark Jun 12, 2024
1113935
chore(auth): updated unit test to use AmplifyOutputs
tyllark Jun 12, 2024
7bb4159
chore(authenticator): updated to use AmplifyOutputs type
tyllark Jun 12, 2024
9e57209
Merge branch 'main' into feat/config-gen2
tyllark Jun 12, 2024
272f211
Merge branch 'feat/config-gen2' into chore/config-gen2-asyncConfig
tyllark Jun 12, 2024
cacc5ca
chore(test): updated test stub to use AmplifyOutputs
tyllark Jun 12, 2024
e2b068a
chore: update gen2 version and config mapping tests (#5010)
Jordan-Nelson Jun 13, 2024
dcf5056
chore: supporting missing auth config options (#5011)
Jordan-Nelson Jun 13, 2024
a391ecc
chore(authenticator): updated unit tests AmplifyConfig Json to Amplif…
tyllark Jun 13, 2024
20ca76c
Merge branch 'feat/config-gen2' into chore/config-gen2-asyncConfig
tyllark Jun 13, 2024
b565963
chore(authenticator): updated unit tests AmplifyOutputs Json values
tyllark Jun 13, 2024
5ff3478
Merge branch 'feat/config-gen2' into chore/config-gen2-asyncConfig
tyllark Jun 14, 2024
6e71be3
chore(core): updated amplify_core.dart file format
tyllark Jun 14, 2024
a66e07f
chore(core): re-internalized IdentityProvider, MFA, and PasswordPolicies
tyllark Jun 14, 2024
0599648
chore(authenticator): renamed InheritedConfig.amplifyConfig to Inheri…
tyllark Jun 14, 2024
0b545ce
chore(authenticator): Renamed FormFieldValidator.validateNewPassword'…
tyllark Jun 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/amplify_core/lib/amplify_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export 'src/category/amplify_categories.dart';
/// Config
export 'src/config/amplify_config.dart';
export 'src/config/amplify_outputs/amplify_outputs.dart';
export 'src/config/amplify_outputs/auth/identity_provider.dart';
export 'src/config/amplify_outputs/auth/mfa.dart';
export 'src/config/amplify_outputs/auth/password_policy.dart';
tyllark marked this conversation as resolved.
Show resolved Hide resolved
export 'src/config/amplify_plugin_config.dart' hide UnknownPluginConfigFactory;
export 'src/config/amplify_plugin_registry.dart';
export 'src/config/analytics/analytics_config.dart';
Expand Down
8 changes: 4 additions & 4 deletions packages/amplify_core/lib/src/amplify_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class AmplifyClass {
final AmplifyAuthProviderRepository authProviderRepo =
AmplifyAuthProviderRepository();

var _configCompleter = Completer<AmplifyConfig>();
var _configCompleter = Completer<AmplifyOutputs>();
final _addPluginFutures = <Future<void>>[];

/// Adds one plugin at a time. Note: this method can only
Expand Down Expand Up @@ -93,7 +93,7 @@ abstract class AmplifyClass {

/// A future when completes when Amplify has been successfully configured.
@internal
Future<AmplifyConfig> get asyncConfig => _configCompleter.future;
Future<AmplifyOutputs> get asyncConfig => _configCompleter.future;

/// Configures Amplify with the provided configuration string.
/// **This method can only be called once**, after all the plugins
Expand Down Expand Up @@ -128,7 +128,7 @@ abstract class AmplifyClass {
);
}
await _configurePlugins(amplifyConfig);
_configCompleter.complete(amplifyConfig);
_configCompleter.complete(amplifyConfig.toAmplifyOutputs());
} on ConfigurationError catch (e, st) {
// Complete with the configuration error and reset the completer so
// that 1) `configure` can be called again and 2) listeners registered
Expand All @@ -146,7 +146,7 @@ abstract class AmplifyClass {
// handled by the developer, but since they are unrelated to
// configuration, listeners to `Amplify.asyncConfig` should be allowed to
// proceed with the validated configuration.
_configCompleter.complete(amplifyConfig);
_configCompleter.complete(amplifyConfig.toAmplifyOutputs());
_configCompleter = Completer();
rethrow;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_core/src/config/amplify_outputs/auth/mfa.dart';
import 'package:amplify_core/src/config/amplify_outputs/auth/oauth_outputs.dart';
import 'package:amplify_core/src/config/amplify_outputs/auth/password_policy.dart';
import 'package:meta/meta.dart';

part 'auth_outputs.g.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_core/src/config/amplify_outputs/auth/identity_provider.dart';
import 'package:amplify_core/src/config/amplify_outputs/auth/oauth_response_type.dart';
import 'package:meta/meta.dart';

Expand Down
1 change: 0 additions & 1 deletion packages/amplify_core/lib/src/config/auth/auth_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
import 'package:amplify_core/src/config/amplify_outputs/auth/oauth_outputs.dart';
import 'package:amplify_core/src/config/amplify_outputs/auth/oauth_response_type.dart';
import 'package:amplify_core/src/config/amplify_outputs/auth/password_policy.dart';
import 'package:meta/meta.dart';

export 'cognito_config.dart' hide CognitoPluginConfigFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ void main() {

// ignore: invalid_use_of_internal_member
final config = await Amplify.asyncConfig;
final authConfig = config.auth!.awsPlugin!.cognitoUserPool!.default$!;
final authConfig = config.auth!;
client = AWSHttpClient()
..supportedProtocols = SupportedProtocols.http1;
cognitoClient = cognito_idp.CognitoIdentityProviderClient(
region: authConfig.region,
region: authConfig.awsRegion,
);
addTearDown(client.close);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AppState {
/// main app component
class AppComponent extends StatefulComponent {
AppState appState = AppState();
late AmplifyConfig config;
late AmplifyOutputs config;

String? _error;

Expand Down Expand Up @@ -189,7 +189,7 @@ class AppComponent extends StatefulComponent {
);
},
),
if (config.auth!.awsPlugin!.auth!.default$!.oAuth != null)
if (config.auth!.oauth != null)
ButtonComponent(
id: 'hostedUiLogin',
innerHtml: 'Login with Hosted UI',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import 'package:amplify_authenticator/src/utils/dial_code.dart';
import 'package:amplify_authenticator/src/utils/dial_code_options.dart';
import 'package:amplify_authenticator/src/widgets/authenticator_banner.dart';
import 'package:amplify_authenticator/src/widgets/form.dart';
import 'package:amplify_core/amplify_core.dart' as core;
tyllark marked this conversation as resolved.
Show resolved Hide resolved
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -510,7 +511,7 @@ class _AuthenticatorState extends State<Authenticator> {
late final StreamSubscription<MessageResolverKey> _infoSub;
late final StreamSubscription<AuthState> _successSub;

AmplifyConfig? _config;
AmplifyOutputs? _config;
late List<String> _missingConfigValues;
bool _configInitialized = false;

Expand Down Expand Up @@ -643,19 +644,19 @@ class _AuthenticatorState extends State<Authenticator> {
});
}

List<String> missingConfigValues(AmplifyConfig? config) {
List<String> missingConfigValues(core.AmplifyOutputs? config) {
final missingValues = <String>[];
final cognitoPlugin = config?.auth?.awsPlugin?.auth?.default$;
final cognitoPlugin = config?.auth;
if (cognitoPlugin == null) {
return const ['auth.plugins.Auth.Default'];
}
if (cognitoPlugin.usernameAttributes == null) {
missingValues.add('usernameAttributes');
}
if (cognitoPlugin.signupAttributes == null) {
if (cognitoPlugin.standardRequiredAttributes == null) {
missingValues.add('signupAttributes');
}
if (cognitoPlugin.passwordProtectionSettings == null) {
if (cognitoPlugin.passwordPolicy == null) {
missingValues.add('passwordProtectionSettings');
}
return missingValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ mixin AuthenticatorUsernameField<FieldType extends Enum,
mixin UsernameAttributes<T extends AuthenticatorForm>
on AuthenticatorComponentState<T> {
late final Set<CognitoUserAttributeKey> usernameAttributes = () {
final authConfig = config.amplifyConfig?.auth?.awsPlugin?.auth?.default$;
final authConfig = config.amplifyConfig?.auth;
return <CognitoUserAttributeKey>{...?authConfig?.usernameAttributes};
}();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ abstract class AuthService {
required CognitoUserAttributeKey userAttributeKey,
required String confirmationCode,
});
Future<AmplifyConfig> waitForConfiguration();
Future<AmplifyOutputs> waitForConfiguration();

Future<void> rememberDevice();

Expand Down Expand Up @@ -324,7 +324,7 @@ class AmplifyAuthService
}

@override
Future<AmplifyConfig> waitForConfiguration() async {
Future<AmplifyOutputs> waitForConfiguration() async {
final timer = Timer.periodic(const Duration(seconds: 10), (timer) {
logger.warn(
'Amplify is taking longer than expected to configure.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class InheritedConfig extends InheritedWidget {
required super.child,
});

final AmplifyConfig? amplifyConfig;
final AmplifyOutputs? amplifyConfig;
tyllark marked this conversation as resolved.
Show resolved Hide resolved
final EdgeInsets padding;

static InheritedConfig of(BuildContext context) {
Expand Down Expand Up @@ -58,7 +58,7 @@ class InheritedConfig extends InheritedWidget {
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty<AmplifyConfig?>('config', amplifyConfig))
..add(DiagnosticsProperty<AmplifyOutputs?>('config', amplifyConfig))
..add(DiagnosticsProperty<EdgeInsets>('padding', padding));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_authenticator/amplify_authenticator.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -73,11 +74,10 @@ extension PasswordPolicyCharactersX on PasswordPolicyCharacters {
}

FormFieldValidator<String> Function(BuildContext) validateNewPassword({
required AmplifyConfig? amplifyConfig,
required AmplifyOutputs? amplifyConfig,
tyllark marked this conversation as resolved.
Show resolved Hide resolved
required InputResolver inputResolver,
}) {
final passwordProtectionSettings = amplifyConfig
?.auth?.awsPlugin?.auth?.default$?.passwordProtectionSettings;
final passwordPolicies = amplifyConfig?.auth?.passwordPolicy;
return (BuildContext context) => (String? password) {
if (password == null || password.isEmpty) {
return inputResolver.resolve(
Expand All @@ -86,23 +86,15 @@ FormFieldValidator<String> Function(BuildContext) validateNewPassword({
);
}
password = password.trim();
if (passwordProtectionSettings == null) {
if (passwordPolicies == null) {
return null;
}

final unmetReqs = <PasswordPolicyCharacters>[];

final minLength = passwordProtectionSettings.passwordPolicyMinLength;
final minLength = passwordPolicies.minLength;
final meetsMinLengthRequirement =
minLength == null || password.length >= minLength;

final passwordPolicies =
passwordProtectionSettings.passwordPolicyCharacters;
for (final policy in passwordPolicies) {
if (!policy.meetsRequirement(password)) {
unmetReqs.add(policy);
}
}
final unmetReqs = _getUnmetPasswordPolicies(password, passwordPolicies);

final error = inputResolver.resolve(
context,
Expand All @@ -118,6 +110,26 @@ FormFieldValidator<String> Function(BuildContext) validateNewPassword({
};
}

List<PasswordPolicyCharacters> _getUnmetPasswordPolicies(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Remove use of PasswordPolicyCharacters.

PasswordPolicyCharacters is part of AmplifyConfig. _getUnmetPasswordPolicies is mapping PasswordPolicy (part of AmplifyOutputs) to PasswordPolicyCharacters (part of AmplifyConfig). I don't think we want to map back to AmplifyConfig unless there is a good reason.

String password,
PasswordPolicy? policy,
) {
final unmetReqs = <PasswordPolicyCharacters>[];
if ((policy?.requireLowercase ?? false) && !password.contains(_lowercase)) {
unmetReqs.add(PasswordPolicyCharacters.requiresLowercase);
}
if ((policy?.requireUppercase ?? false) && !password.contains(_uppercase)) {
unmetReqs.add(PasswordPolicyCharacters.requiresUppercase);
}
if ((policy?.requireNumbers ?? false) && !password.contains(_numeric)) {
unmetReqs.add(PasswordPolicyCharacters.requiresNumbers);
}
if ((policy?.requireSymbols ?? false) && !password.contains(_symbols)) {
unmetReqs.add(PasswordPolicyCharacters.requiresSymbols);
}
return unmetReqs;
}

FormFieldValidator<String> validatePasswordConfirmation(
String Function() getPassword, {
required BuildContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ abstract class AuthenticatorComponentState<T extends StatefulWidget>
),
)
..add(
DiagnosticsProperty<AmplifyConfig>('config', config.amplifyConfig),
DiagnosticsProperty<AmplifyOutputs>('config', config.amplifyConfig),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,10 @@ class _SignUpFormState extends AuthenticatorFormState<SignUpForm> {

@override
List<SignUpFormField> runtimeFields(BuildContext context) {
final authConfig = InheritedConfig.of(context)
.amplifyConfig
?.auth
?.awsPlugin
?.auth
?.default$;
final authConfig = InheritedConfig.of(context).amplifyConfig?.auth;
final runtimeAttributes = <CognitoUserAttributeKey>{
...?authConfig?.signupAttributes,
...?authConfig?.verificationMechanisms,
...?authConfig?.standardRequiredAttributes,
...?authConfig?.userVerificationTypes,
};
if (runtimeAttributes.isEmpty) {
return const [];
Expand Down Expand Up @@ -362,17 +357,17 @@ class _SignUpFormState extends AuthenticatorFormState<SignUpForm> {
.whereType<SignUpFormField>()
.toList();

final hasSmsMfa = authConfig?.mfaTypes?.contains(MfaType.sms) ?? false;
final hasSmsMfa = authConfig?.mfaMethods?.contains(MfaMethod.sms) ?? false;
if (hasSmsMfa && selectedUsernameType != UsernameType.phoneNumber) {
final mfaConfiguration =
authConfig?.mfaConfiguration ?? MfaConfiguration.off;
authConfig?.mfaConfiguration ?? MfaEnforcement.off;
final hasSmsField = runtimeFields.any(
(f) => f.field == SignUpField.phoneNumber,
);
if (!hasSmsField && mfaConfiguration != MfaConfiguration.off) {
if (!hasSmsField && mfaConfiguration != MfaEnforcement.off) {
runtimeFields.add(
SignUpFormField.phoneNumber(
required: mfaConfiguration == MfaConfiguration.on,
required: mfaConfiguration == MfaEnforcement.on,
),
);
}
Expand Down Expand Up @@ -438,19 +433,18 @@ class _SignInFormState extends AuthenticatorFormState<SignInForm> {
final socialProviders = InheritedConfig.of(context)
.amplifyConfig
?.auth
?.awsPlugin
?.auth
?.default$
?.socialProviders;
?.oauth
?.identityProviders;

if (socialProviders == null || socialProviders.isEmpty) {
return const [];
}

// Sort Apple first based off their app guidelines.
socialProviders.sort((a, b) {
if (a == SocialProvider.apple) {
if (a == IdentityProvider.apple) {
return -1;
} else if (b == SocialProvider.apple) {
} else if (b == IdentityProvider.apple) {
return 1;
}
return a.name.compareTo(b.name);
Expand All @@ -460,13 +454,13 @@ class _SignInFormState extends AuthenticatorFormState<SignInForm> {
SocialSignInButtons(
providers: socialProviders.map((e) {
switch (e) {
case SocialProvider.facebook:
case IdentityProvider.facebook:
return AuthProvider.facebook;
case SocialProvider.google:
case IdentityProvider.google:
return AuthProvider.google;
case SocialProvider.amazon:
case IdentityProvider.amazon:
return AuthProvider.amazon;
case SocialProvider.apple:
case IdentityProvider.apple:
return AuthProvider.apple;
}
}).toList(),
Expand Down
Loading
Loading