Skip to content

Commit 780a28d

Browse files
authored
chore(auth): credential store state machine to use AuthOutputs instead of AmplifyConfig types (#5298)
1 parent 32c5043 commit 780a28d

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

packages/auth/amplify_auth_cognito_dart/lib/src/state/machines/credential_store_state_machine.dart

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart
1717
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
1818
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
1919
import 'package:amplify_core/amplify_core.dart';
20+
// ignore: implementation_imports
21+
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
2022
import 'package:amplify_secure_storage_dart/amplify_secure_storage_dart.dart';
2123
import 'package:meta/meta.dart';
2224

@@ -49,6 +51,14 @@ final class CredentialStoreStateMachine
4951

5052
DeviceMetadataRepository get _deviceRepository => getOrCreate();
5153

54+
AuthOutputs get _authOutputs => expect<AuthOutputs>();
55+
56+
late final bool _hasUserPool =
57+
_authOutputs.userPoolId != null && _authOutputs.userPoolClientId != null;
58+
late final bool _hasIdentityPool = _authOutputs.identityPoolId != null;
59+
late final bool _hasHostedUi =
60+
_authOutputs.oauth != null && _authOutputs.userPoolClientId != null;
61+
5262
@override
5363
Future<void> resolve(CredentialStoreEvent event) async {
5464
switch (event) {
@@ -99,13 +109,11 @@ final class CredentialStoreStateMachine
99109

100110
/// Loads the credential store from storage and returns the data.
101111
Future<CredentialStoreData> _loadCredentialStore() async {
102-
final authConfig = expect<AuthConfiguration>();
103-
104112
CognitoSignInDetails? signInDetails;
105113
CognitoUserPoolTokens? userPoolTokens;
106-
final userPoolConfig = authConfig.userPoolConfig;
107-
if (userPoolConfig != null) {
108-
final keys = CognitoUserPoolKeys(userPoolConfig.appClientId);
114+
115+
if (_hasUserPool) {
116+
final keys = CognitoUserPoolKeys(_authOutputs.userPoolClientId!);
109117
final accessToken = await _secureStorage.read(
110118
key: keys[CognitoUserPoolKey.accessToken],
111119
);
@@ -138,9 +146,8 @@ final class CredentialStoreStateMachine
138146
}
139147
}
140148

141-
final hostedUiConfig = authConfig.hostedUiConfig;
142-
if (hostedUiConfig != null) {
143-
final keys = HostedUiKeys(hostedUiConfig.appClientId);
149+
if (_hasHostedUi) {
150+
final keys = HostedUiKeys(_authOutputs.userPoolClientId!);
144151
final accessToken = await _secureStorage.read(
145152
key: keys[HostedUiKey.accessToken],
146153
);
@@ -172,9 +179,8 @@ final class CredentialStoreStateMachine
172179

173180
String? identityId;
174181
AWSCredentials? awsCredentials;
175-
final identityPoolConfig = authConfig.identityPoolConfig;
176-
if (identityPoolConfig != null) {
177-
final keys = CognitoIdentityPoolKeys(identityPoolConfig.poolId);
182+
if (_hasIdentityPool) {
183+
final keys = CognitoIdentityPoolKeys(_authOutputs.identityPoolId!);
178184
identityId = await _secureStorage.read(
179185
key: keys[CognitoIdentityPoolKey.identityId],
180186
);
@@ -232,14 +238,12 @@ final class CredentialStoreStateMachine
232238
final identityId = data.identityId;
233239
final awsCredentials = data.awsCredentials;
234240
final signInDetails = data.signInDetails;
235-
final authConfig = expect<AuthConfiguration>();
236241

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

240-
final userPoolConfig = authConfig.userPoolConfig;
241-
if (userPoolConfig != null) {
242-
final keys = CognitoUserPoolKeys(userPoolConfig.appClientId);
245+
if (_hasUserPool) {
246+
final keys = CognitoUserPoolKeys(_authOutputs.userPoolClientId!);
243247
if (userPoolTokens != null &&
244248
userPoolTokens.signInMethod == CognitoSignInMethod.default$) {
245249
signInDetails as CognitoSignInDetailsApiBased?;
@@ -256,9 +260,8 @@ final class CredentialStoreStateMachine
256260
}
257261
}
258262

259-
final hostedUiConfig = authConfig.hostedUiConfig;
260-
if (hostedUiConfig != null) {
261-
final keys = HostedUiKeys(hostedUiConfig.appClientId);
263+
if (_hasHostedUi) {
264+
final keys = HostedUiKeys(_authOutputs.userPoolClientId!);
262265
if (userPoolTokens != null &&
263266
(userPoolTokens.signInMethod == CognitoSignInMethod.hostedUi)) {
264267
signInDetails as CognitoSignInDetailsHostedUi?;
@@ -273,9 +276,8 @@ final class CredentialStoreStateMachine
273276
}
274277
}
275278

276-
final identityPoolConfig = authConfig.identityPoolConfig;
277-
if (identityPoolConfig != null) {
278-
final keys = CognitoIdentityPoolKeys(identityPoolConfig.poolId);
279+
if (_hasIdentityPool) {
280+
final keys = CognitoIdentityPoolKeys(_authOutputs.identityPoolId!);
279281
if (identityId != null) {
280282
items[keys[CognitoIdentityPoolKey.identityId]] = identityId;
281283
}
@@ -334,6 +336,7 @@ final class CredentialStoreStateMachine
334336
/// Migrates AWS Credentials and User Pool tokens.
335337
Future<CredentialStoreData?> _migrateLegacyCredentials() async {
336338
final provider = get<LegacyCredentialProvider>();
339+
// TODO(nikahsn): remove after refactoring LegacyCredentialProvider
337340
final authConfig = expect<AuthConfiguration>();
338341
if (provider == null) return null;
339342
CredentialStoreData? legacyData;
@@ -355,9 +358,9 @@ final class CredentialStoreStateMachine
355358
/// Migrates legacy device secrets.
356359
Future<void> _migrateDeviceSecrets(String username) async {
357360
final credentialProvider = get<LegacyCredentialProvider>();
361+
// TODO(nikahsn): remove after refactoring LegacyCredentialProvider
358362
final authConfig = expect<AuthConfiguration>();
359-
final userPoolKeys =
360-
CognitoUserPoolKeys(authConfig.userPoolConfig!.appClientId);
363+
final userPoolKeys = CognitoUserPoolKeys(_authOutputs.userPoolClientId!);
361364
if (credentialProvider == null) return;
362365
try {
363366
final legacySecrets = await credentialProvider.fetchLegacyDeviceSecrets(
@@ -396,6 +399,7 @@ final class CredentialStoreStateMachine
396399
/// Deletes legacy credentials.
397400
Future<void> _deleteLegacyCredentials() async {
398401
final provider = get<LegacyCredentialProvider>();
402+
// TODO(nikahsn): remove after refactoring LegacyCredentialProvider
399403
final authConfig = expect<AuthConfiguration>();
400404
if (provider == null) return;
401405
try {
@@ -431,37 +435,32 @@ final class CredentialStoreStateMachine
431435
Future<void> onClearCredentials(
432436
CredentialStoreClearCredentials event,
433437
) async {
434-
final authConfig = expect<AuthConfiguration>();
435-
436438
final clearKeys = event.keys;
437439
final deletions = <String>[];
438440
bool shouldDelete(String key) =>
439441
clearKeys.isEmpty || clearKeys.contains(key);
440442

441-
final userPoolConfig = authConfig.userPoolConfig;
442-
if (userPoolConfig != null) {
443-
final userPoolKeys = CognitoUserPoolKeys(userPoolConfig.appClientId);
443+
if (_hasUserPool) {
444+
final userPoolKeys = CognitoUserPoolKeys(_authOutputs.userPoolClientId!);
444445
for (final key in userPoolKeys) {
445446
if (shouldDelete(key)) {
446447
deletions.add(key);
447448
}
448449
}
449450
}
450451

451-
final hostedUiConfig = authConfig.hostedUiConfig;
452-
if (hostedUiConfig != null) {
453-
final hostedUiKeys = HostedUiKeys(hostedUiConfig.appClientId);
452+
if (_hasHostedUi) {
453+
final hostedUiKeys = HostedUiKeys(_authOutputs.userPoolClientId!);
454454
for (final key in hostedUiKeys) {
455455
if (shouldDelete(key)) {
456456
deletions.add(key);
457457
}
458458
}
459459
}
460460

461-
final identityPoolConfig = authConfig.identityPoolConfig;
462-
if (identityPoolConfig != null) {
461+
if (_hasIdentityPool) {
463462
final identityPoolKeys =
464-
CognitoIdentityPoolKeys(identityPoolConfig.poolId);
463+
CognitoIdentityPoolKeys(_authOutputs.identityPoolId!);
465464
for (final key in identityPoolKeys) {
466465
if (shouldDelete(key)) {
467466
deletions.add(key);

packages/auth/amplify_auth_cognito_test/test/state/credential_store_state_machine_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void main() {
3131
secureStorage = MockSecureStorage();
3232
manager = DependencyManager()
3333
..addInstance(secureStorage)
34-
..addInstance(mockConfig)
34+
..addInstance(mockConfig.auth!)
3535
..addInstance(authConfig);
3636
stateMachine = CognitoAuthStateMachine(dependencyManager: manager);
3737
});

0 commit comments

Comments
 (0)