Skip to content

chore(auth): credential store state machine to use AuthOutputs instead of AmplifyConfig types #5298

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

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ 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_dart/src/state/state.dart';
import 'package:amplify_core/amplify_core.dart';
// ignore: implementation_imports
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
import 'package:amplify_secure_storage_dart/amplify_secure_storage_dart.dart';
import 'package:meta/meta.dart';

Expand Down Expand Up @@ -49,6 +51,14 @@ final class CredentialStoreStateMachine

DeviceMetadataRepository get _deviceRepository => getOrCreate();

AuthOutputs get _authOutputs => expect<AuthOutputs>();

late final bool _hasUserPool =
_authOutputs.userPoolId != null && _authOutputs.userPoolClientId != null;
late final bool _hasIdentityPool = _authOutputs.identityPoolId != null;
late final bool _hasHostedUi =
_authOutputs.oauth != null && _authOutputs.userPoolClientId != null;

@override
Future<void> resolve(CredentialStoreEvent event) async {
switch (event) {
Expand Down Expand Up @@ -99,13 +109,11 @@ final class CredentialStoreStateMachine

/// Loads the credential store from storage and returns the data.
Future<CredentialStoreData> _loadCredentialStore() async {
final authConfig = expect<AuthConfiguration>();

CognitoSignInDetails? signInDetails;
CognitoUserPoolTokens? userPoolTokens;
final userPoolConfig = authConfig.userPoolConfig;
if (userPoolConfig != null) {
final keys = CognitoUserPoolKeys(userPoolConfig.appClientId);

if (_hasUserPool) {
final keys = CognitoUserPoolKeys(_authOutputs.userPoolClientId!);
final accessToken = await _secureStorage.read(
key: keys[CognitoUserPoolKey.accessToken],
);
Expand Down Expand Up @@ -138,9 +146,8 @@ final class CredentialStoreStateMachine
}
}

final hostedUiConfig = authConfig.hostedUiConfig;
if (hostedUiConfig != null) {
final keys = HostedUiKeys(hostedUiConfig.appClientId);
if (_hasHostedUi) {
final keys = HostedUiKeys(_authOutputs.userPoolClientId!);
final accessToken = await _secureStorage.read(
key: keys[HostedUiKey.accessToken],
);
Expand Down Expand Up @@ -172,9 +179,8 @@ final class CredentialStoreStateMachine

String? identityId;
AWSCredentials? awsCredentials;
final identityPoolConfig = authConfig.identityPoolConfig;
if (identityPoolConfig != null) {
final keys = CognitoIdentityPoolKeys(identityPoolConfig.poolId);
if (_hasIdentityPool) {
final keys = CognitoIdentityPoolKeys(_authOutputs.identityPoolId!);
identityId = await _secureStorage.read(
key: keys[CognitoIdentityPoolKey.identityId],
);
Expand Down Expand Up @@ -232,14 +238,12 @@ final class CredentialStoreStateMachine
final identityId = data.identityId;
final awsCredentials = data.awsCredentials;
final signInDetails = data.signInDetails;
final authConfig = expect<AuthConfiguration>();

final items = <String, String>{};
final deletions = <String>[];

final userPoolConfig = authConfig.userPoolConfig;
if (userPoolConfig != null) {
final keys = CognitoUserPoolKeys(userPoolConfig.appClientId);
if (_hasUserPool) {
final keys = CognitoUserPoolKeys(_authOutputs.userPoolClientId!);
if (userPoolTokens != null &&
userPoolTokens.signInMethod == CognitoSignInMethod.default$) {
signInDetails as CognitoSignInDetailsApiBased?;
Expand All @@ -256,9 +260,8 @@ final class CredentialStoreStateMachine
}
}

final hostedUiConfig = authConfig.hostedUiConfig;
if (hostedUiConfig != null) {
final keys = HostedUiKeys(hostedUiConfig.appClientId);
if (_hasHostedUi) {
final keys = HostedUiKeys(_authOutputs.userPoolClientId!);
if (userPoolTokens != null &&
(userPoolTokens.signInMethod == CognitoSignInMethod.hostedUi)) {
signInDetails as CognitoSignInDetailsHostedUi?;
Expand All @@ -273,9 +276,8 @@ final class CredentialStoreStateMachine
}
}

final identityPoolConfig = authConfig.identityPoolConfig;
if (identityPoolConfig != null) {
final keys = CognitoIdentityPoolKeys(identityPoolConfig.poolId);
if (_hasIdentityPool) {
final keys = CognitoIdentityPoolKeys(_authOutputs.identityPoolId!);
if (identityId != null) {
items[keys[CognitoIdentityPoolKey.identityId]] = identityId;
}
Expand Down Expand Up @@ -334,6 +336,7 @@ final class CredentialStoreStateMachine
/// Migrates AWS Credentials and User Pool tokens.
Future<CredentialStoreData?> _migrateLegacyCredentials() async {
final provider = get<LegacyCredentialProvider>();
// TODO(nikahsn): remove after refactoring LegacyCredentialProvider
final authConfig = expect<AuthConfiguration>();
if (provider == null) return null;
CredentialStoreData? legacyData;
Expand All @@ -355,9 +358,9 @@ final class CredentialStoreStateMachine
/// Migrates legacy device secrets.
Future<void> _migrateDeviceSecrets(String username) async {
final credentialProvider = get<LegacyCredentialProvider>();
// TODO(nikahsn): remove after refactoring LegacyCredentialProvider
final authConfig = expect<AuthConfiguration>();
final userPoolKeys =
CognitoUserPoolKeys(authConfig.userPoolConfig!.appClientId);
final userPoolKeys = CognitoUserPoolKeys(_authOutputs.userPoolClientId!);
if (credentialProvider == null) return;
try {
final legacySecrets = await credentialProvider.fetchLegacyDeviceSecrets(
Expand Down Expand Up @@ -396,6 +399,7 @@ final class CredentialStoreStateMachine
/// Deletes legacy credentials.
Future<void> _deleteLegacyCredentials() async {
final provider = get<LegacyCredentialProvider>();
// TODO(nikahsn): remove after refactoring LegacyCredentialProvider
final authConfig = expect<AuthConfiguration>();
if (provider == null) return;
try {
Expand Down Expand Up @@ -431,37 +435,32 @@ final class CredentialStoreStateMachine
Future<void> onClearCredentials(
CredentialStoreClearCredentials event,
) async {
final authConfig = expect<AuthConfiguration>();

final clearKeys = event.keys;
final deletions = <String>[];
bool shouldDelete(String key) =>
clearKeys.isEmpty || clearKeys.contains(key);

final userPoolConfig = authConfig.userPoolConfig;
if (userPoolConfig != null) {
final userPoolKeys = CognitoUserPoolKeys(userPoolConfig.appClientId);
if (_hasUserPool) {
final userPoolKeys = CognitoUserPoolKeys(_authOutputs.userPoolClientId!);
for (final key in userPoolKeys) {
if (shouldDelete(key)) {
deletions.add(key);
}
}
}

final hostedUiConfig = authConfig.hostedUiConfig;
if (hostedUiConfig != null) {
final hostedUiKeys = HostedUiKeys(hostedUiConfig.appClientId);
if (_hasHostedUi) {
final hostedUiKeys = HostedUiKeys(_authOutputs.userPoolClientId!);
for (final key in hostedUiKeys) {
if (shouldDelete(key)) {
deletions.add(key);
}
}
}

final identityPoolConfig = authConfig.identityPoolConfig;
if (identityPoolConfig != null) {
if (_hasIdentityPool) {
final identityPoolKeys =
CognitoIdentityPoolKeys(identityPoolConfig.poolId);
CognitoIdentityPoolKeys(_authOutputs.identityPoolId!);
for (final key in identityPoolKeys) {
if (shouldDelete(key)) {
deletions.add(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
secureStorage = MockSecureStorage();
manager = DependencyManager()
..addInstance(secureStorage)
..addInstance(mockConfig)
..addInstance(mockConfig.auth!)
..addInstance(authConfig);
stateMachine = CognitoAuthStateMachine(dependencyManager: manager);
});
Expand Down
Loading