Skip to content

Commit

Permalink
chore(auth): update auth plugin impl and config state machine to use …
Browse files Browse the repository at this point in the history
…AuthOutputs instead of Auth config (#5456)
  • Loading branch information
NikaHsn authored Sep 18, 2024
1 parent e6288c7 commit 718068a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import 'package:amplify_auth_cognito_dart/src/state/state.dart';
import 'package:amplify_auth_cognito_dart/src/util/cognito_iam_auth_provider.dart';
import 'package:amplify_auth_cognito_dart/src/util/cognito_user_pools_auth_provider.dart';
import 'package:amplify_core/amplify_core.dart';
// ignore: implementation_imports
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
// ignore: implementation_imports, invalid_use_of_internal_member
import 'package:amplify_core/src/http/amplify_category_method.dart';
import 'package:amplify_secure_storage_dart/amplify_secure_storage_dart.dart';
Expand Down Expand Up @@ -123,19 +125,15 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
return cognitoIdp;
}

/// The Cognito user pool configuration.
CognitoUserPoolConfig get _userPoolConfig {
final userPoolConfig = _stateMachine.get<CognitoUserPoolConfig>();
if (userPoolConfig == null) {
AuthOutputs get _authOutputs {
final authOutputs = _stateMachine.get<AuthOutputs>();
if (authOutputs?.userPoolId == null ||
authOutputs?.userPoolClientId == null) {
throw const InvalidAccountTypeException.noUserPool();
}
return userPoolConfig;
return authOutputs!;
}

/// The Cognito identity pool configuration.
CognitoIdentityCredentialsProvider? get _identityPoolConfig =>
_stateMachine.get();

/// The device metadata repository, used for handling device operations.
DeviceMetadataRepository get _deviceRepo => _stateMachine.getOrCreate();

Expand Down Expand Up @@ -304,13 +302,13 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
/// If there is no federation active, this is a no-op.
/// {@endtemplate}
Future<void> clearFederationToIdentityPool() async {
final identityPoolConfig = _identityPoolConfig;
if (identityPoolConfig == null) {
final identityPoolId = _authOutputs.identityPoolId;
if (identityPoolId == null) {
throw const InvalidAccountTypeException.noIdentityPool();
}
await stateMachine.acceptAndComplete(
CredentialStoreEvent.clearCredentials(
CognitoIdentityPoolKeys(identityPoolConfig.poolId),
CognitoIdentityPoolKeys(identityPoolId),
),
);
}
Expand Down Expand Up @@ -451,15 +449,16 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
final result = await _cognitoIdp.resendConfirmationCode(
cognito.ResendConfirmationCodeRequest.build((b) {
b
..clientId = _userPoolConfig.appClientId
..clientId = _authOutputs.userPoolClientId
..username = username
..analyticsMetadata = _analyticsMetadata?.toBuilder();

final clientSecret = _userPoolConfig.appClientSecret;
// ignore: invalid_use_of_internal_member
final clientSecret = _authOutputs.appClientSecret;
if (clientSecret != null) {
b.secretHash = computeSecretHash(
username,
_userPoolConfig.appClientId,
_authOutputs.userPoolClientId!,
clientSecret,
);
}
Expand Down Expand Up @@ -763,16 +762,17 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
final result = await _cognitoIdp.forgotPassword(
cognito.ForgotPasswordRequest.build((b) {
b
..clientId = _userPoolConfig.appClientId
..clientId = _authOutputs.userPoolClientId
..username = username
..analyticsMetadata = _analyticsMetadata?.toBuilder()
..clientMetadata.addAll(pluginOptions.clientMetadata);

final clientSecret = _userPoolConfig.appClientSecret;
// ignore: invalid_use_of_internal_member
final clientSecret = _authOutputs.appClientSecret;
if (clientSecret != null) {
b.secretHash = computeSecretHash(
username,
_userPoolConfig.appClientId,
_authOutputs.userPoolClientId!,
clientSecret,
);
}
Expand Down Expand Up @@ -817,15 +817,16 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
..username = username
..password = newPassword
..confirmationCode = confirmationCode
..clientId = _userPoolConfig.appClientId
..clientId = _authOutputs.userPoolClientId
..clientMetadata.addAll(pluginOptions.clientMetadata)
..analyticsMetadata = _analyticsMetadata?.toBuilder();

final clientSecret = _userPoolConfig.appClientSecret;
// ignore: invalid_use_of_internal_member
final clientSecret = _authOutputs.appClientSecret;
if (clientSecret != null) {
b.secretHash = computeSecretHash(
username,
_userPoolConfig.appClientId,
_authOutputs.userPoolClientId!,
clientSecret,
);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import 'dart:async';
// ignore: implementation_imports
import 'package:amplify_analytics_pinpoint_dart/src/impl/analytics_client/endpoint_client/endpoint_info_store_manager.dart';
import 'package:amplify_auth_cognito_dart/src/credentials/auth_plugin_credentials_provider.dart';
import 'package:amplify_auth_cognito_dart/src/model/auth_configuration.dart';
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity.dart';
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart';
import 'package:amplify_auth_cognito_dart/src/sdk/sdk_bridge.dart';
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
import 'package:amplify_core/amplify_core.dart';
// ignore: implementation_imports
import 'package:amplify_core/src/config/amplify_outputs/analytics/analytics_outputs.dart';

/// {@template amplify_auth_cognito.configuration_state_machine}
/// Manages configuration of the Auth category.
Expand Down Expand Up @@ -69,36 +70,24 @@ final class ConfigurationStateMachine
throw ConfigurationError('No Cognito plugin config available');
}
addInstance(authOutputs);
final config = AuthConfiguration.fromAmplifyOutputs(event.config);
addInstance(config);

final waiters = <Future<void>>[];
final userPoolConfig = config.userPoolConfig;
if (userPoolConfig != null) {
addInstance(userPoolConfig);
addInstance<CognitoIdentityProviderClient>(
WrappedCognitoIdentityProviderClient(
region: userPoolConfig.region,
credentialsProvider: _credentialsProvider,
dependencyManager: this,
endpoint: userPoolConfig.endpoint,
),
);
}
addInstance<CognitoIdentityProviderClient>(
WrappedCognitoIdentityProviderClient(
region: authOutputs.awsRegion,
credentialsProvider: _credentialsProvider,
dependencyManager: this,
),
);

// Configure HostedUI, if available
final hostedUiConfig = config.hostedUiConfig;
if (hostedUiConfig != null) {
addInstance(hostedUiConfig);
if (authOutputs.oauth != null) {
waiters.add(manager.configureHostedUI());
}

final identityPoolConfig = config.identityPoolConfig;
if (identityPoolConfig != null) {
addInstance(identityPoolConfig);
if (authOutputs.identityPoolId != null) {
addInstance<CognitoIdentityClient>(
WrappedCognitoIdentityClient(
region: identityPoolConfig.region,
region: authOutputs.awsRegion,
credentialsProvider: _credentialsProvider,
dependencyManager: this,
),
Expand All @@ -110,7 +99,7 @@ final class ConfigurationStateMachine
await _waitForConfiguration(event.config, waiters);

// Setup AnalyticsMetadataType
await _registerAnalyticsMetadata(config);
await _registerAnalyticsMetadata(event.config.analytics);
}

Future<void> _waitForConfiguration(
Expand All @@ -124,13 +113,9 @@ final class ConfigurationStateMachine
/// State machine callback for the [ConfigureSucceeded] event.
Future<void> onConfigureSucceeded(ConfigureSucceeded event) async {}

Future<void> _registerAnalyticsMetadata(AuthConfiguration config) async {
final analyticsConfig = config.pinpointConfig;
if (analyticsConfig == null) {
return;
}
final appId = analyticsConfig.appId;

Future<void> _registerAnalyticsMetadata(AnalyticsOutputs? config) async {
final appId = config?.amazonPinpoint?.appId;
if (appId == null) return;
final endpointStoreManager = getOrCreate<EndpointInfoStoreManager>();
await endpointStoreManager.init(pinpointAppId: appId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:convert';
import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart';
import 'package:amplify_auth_cognito_dart/src/jwt/jwt.dart';
import 'package:amplify_auth_cognito_dart/src/model/auth_configuration.dart';
import 'package:amplify_core/amplify_core.dart';

const amplifyConfig = '''{
Expand Down Expand Up @@ -131,9 +130,6 @@ final mockConfigWithPinpoint = AmplifyOutputs.fromJson(
jsonDecode(amplifyConfigWithAnalytics) as Map<String, Object?>,
);

final authConfig = AuthConfiguration.fromAmplifyOutputs(mockConfig);
// TODO(nikahsn): remove after refactoring DeviceMetadataRepository
final userPoolConfig = authConfig.userPoolConfig!;
final userPoolKeys = CognitoUserPoolKeys(mockConfig.auth!.userPoolClientId!);
final deviceKeys =
CognitoDeviceKeys(mockConfig.auth!.userPoolClientId!, userSub);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'dart:async';
import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart'
hide InternalErrorException;
import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart';
import 'package:amplify_auth_cognito_dart/src/model/auth_configuration.dart';
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart';
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
import 'package:amplify_auth_cognito_test/common/matchers.dart';
Expand All @@ -20,11 +19,9 @@ import 'package:amplify_secure_storage_dart/amplify_secure_storage_dart.dart';
import 'package:test/test.dart';

void main() {
final authConfig = AuthConfiguration.fromAmplifyOutputs(mockConfig);
final userPoolConfig = authConfig.userPoolConfig!;
final identityPoolConfig = authConfig.identityPoolConfig!;
final userPoolKeys = CognitoUserPoolKeys(userPoolConfig.appClientId);
final identityPoolKeys = CognitoIdentityPoolKeys(identityPoolConfig.poolId);
final userPoolKeys = CognitoUserPoolKeys(mockConfig.auth!.userPoolClientId!);
final identityPoolKeys =
CognitoIdentityPoolKeys(mockConfig.auth!.identityPoolId!);

late AmplifyAuthCognitoDart plugin;
late CognitoAuthStateMachine stateMachine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:test/test.dart';
void main() {
AmplifyLogger().logLevel = LogLevel.verbose;

final userPoolKeys = CognitoUserPoolKeys(userPoolConfig.appClientId);
final userPoolKeys = CognitoUserPoolKeys(mockConfig.auth!.userPoolClientId!);
final identityPoolKeys =
CognitoIdentityPoolKeys(mockConfig.auth!.identityPoolId!);
final testAuthRepo = AmplifyAuthProviderRepository();
Expand All @@ -31,7 +31,8 @@ void main() {
secureStorage,
userPoolKeys: userPoolKeys,
identityPoolKeys: identityPoolKeys,
deviceKeys: CognitoDeviceKeys(userPoolConfig.appClientId, username),
deviceKeys:
CognitoDeviceKeys(mockConfig.auth!.userPoolClientId!, username),
);
plugin = AmplifyAuthCognitoDart(
secureStorageFactory: (_) => secureStorage,
Expand Down
Loading

0 comments on commit 718068a

Please sign in to comment.