@@ -17,6 +17,8 @@ import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart
17
17
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart' ;
18
18
import 'package:amplify_auth_cognito_dart/src/state/state.dart' ;
19
19
import 'package:amplify_core/amplify_core.dart' ;
20
+ // ignore: implementation_imports
21
+ import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart' ;
20
22
import 'package:amplify_secure_storage_dart/amplify_secure_storage_dart.dart' ;
21
23
import 'package:meta/meta.dart' ;
22
24
@@ -49,6 +51,14 @@ final class CredentialStoreStateMachine
49
51
50
52
DeviceMetadataRepository get _deviceRepository => getOrCreate ();
51
53
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
+
52
62
@override
53
63
Future <void > resolve (CredentialStoreEvent event) async {
54
64
switch (event) {
@@ -99,13 +109,11 @@ final class CredentialStoreStateMachine
99
109
100
110
/// Loads the credential store from storage and returns the data.
101
111
Future <CredentialStoreData > _loadCredentialStore () async {
102
- final authConfig = expect <AuthConfiguration >();
103
-
104
112
CognitoSignInDetails ? signInDetails;
105
113
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 ! );
109
117
final accessToken = await _secureStorage.read (
110
118
key: keys[CognitoUserPoolKey .accessToken],
111
119
);
@@ -138,9 +146,8 @@ final class CredentialStoreStateMachine
138
146
}
139
147
}
140
148
141
- final hostedUiConfig = authConfig.hostedUiConfig;
142
- if (hostedUiConfig != null ) {
143
- final keys = HostedUiKeys (hostedUiConfig.appClientId);
149
+ if (_hasHostedUi) {
150
+ final keys = HostedUiKeys (_authOutputs.userPoolClientId! );
144
151
final accessToken = await _secureStorage.read (
145
152
key: keys[HostedUiKey .accessToken],
146
153
);
@@ -172,9 +179,8 @@ final class CredentialStoreStateMachine
172
179
173
180
String ? identityId;
174
181
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! );
178
184
identityId = await _secureStorage.read (
179
185
key: keys[CognitoIdentityPoolKey .identityId],
180
186
);
@@ -232,14 +238,12 @@ final class CredentialStoreStateMachine
232
238
final identityId = data.identityId;
233
239
final awsCredentials = data.awsCredentials;
234
240
final signInDetails = data.signInDetails;
235
- final authConfig = expect <AuthConfiguration >();
236
241
237
242
final items = < String , String > {};
238
243
final deletions = < String > [];
239
244
240
- final userPoolConfig = authConfig.userPoolConfig;
241
- if (userPoolConfig != null ) {
242
- final keys = CognitoUserPoolKeys (userPoolConfig.appClientId);
245
+ if (_hasUserPool) {
246
+ final keys = CognitoUserPoolKeys (_authOutputs.userPoolClientId! );
243
247
if (userPoolTokens != null &&
244
248
userPoolTokens.signInMethod == CognitoSignInMethod .default$) {
245
249
signInDetails as CognitoSignInDetailsApiBased ? ;
@@ -256,9 +260,8 @@ final class CredentialStoreStateMachine
256
260
}
257
261
}
258
262
259
- final hostedUiConfig = authConfig.hostedUiConfig;
260
- if (hostedUiConfig != null ) {
261
- final keys = HostedUiKeys (hostedUiConfig.appClientId);
263
+ if (_hasHostedUi) {
264
+ final keys = HostedUiKeys (_authOutputs.userPoolClientId! );
262
265
if (userPoolTokens != null &&
263
266
(userPoolTokens.signInMethod == CognitoSignInMethod .hostedUi)) {
264
267
signInDetails as CognitoSignInDetailsHostedUi ? ;
@@ -273,9 +276,8 @@ final class CredentialStoreStateMachine
273
276
}
274
277
}
275
278
276
- final identityPoolConfig = authConfig.identityPoolConfig;
277
- if (identityPoolConfig != null ) {
278
- final keys = CognitoIdentityPoolKeys (identityPoolConfig.poolId);
279
+ if (_hasIdentityPool) {
280
+ final keys = CognitoIdentityPoolKeys (_authOutputs.identityPoolId! );
279
281
if (identityId != null ) {
280
282
items[keys[CognitoIdentityPoolKey .identityId]] = identityId;
281
283
}
@@ -334,6 +336,7 @@ final class CredentialStoreStateMachine
334
336
/// Migrates AWS Credentials and User Pool tokens.
335
337
Future <CredentialStoreData ?> _migrateLegacyCredentials () async {
336
338
final provider = get <LegacyCredentialProvider >();
339
+ // TODO(nikahsn): remove after refactoring LegacyCredentialProvider
337
340
final authConfig = expect <AuthConfiguration >();
338
341
if (provider == null ) return null ;
339
342
CredentialStoreData ? legacyData;
@@ -355,9 +358,9 @@ final class CredentialStoreStateMachine
355
358
/// Migrates legacy device secrets.
356
359
Future <void > _migrateDeviceSecrets (String username) async {
357
360
final credentialProvider = get <LegacyCredentialProvider >();
361
+ // TODO(nikahsn): remove after refactoring LegacyCredentialProvider
358
362
final authConfig = expect <AuthConfiguration >();
359
- final userPoolKeys =
360
- CognitoUserPoolKeys (authConfig.userPoolConfig! .appClientId);
363
+ final userPoolKeys = CognitoUserPoolKeys (_authOutputs.userPoolClientId! );
361
364
if (credentialProvider == null ) return ;
362
365
try {
363
366
final legacySecrets = await credentialProvider.fetchLegacyDeviceSecrets (
@@ -396,6 +399,7 @@ final class CredentialStoreStateMachine
396
399
/// Deletes legacy credentials.
397
400
Future <void > _deleteLegacyCredentials () async {
398
401
final provider = get <LegacyCredentialProvider >();
402
+ // TODO(nikahsn): remove after refactoring LegacyCredentialProvider
399
403
final authConfig = expect <AuthConfiguration >();
400
404
if (provider == null ) return ;
401
405
try {
@@ -431,37 +435,32 @@ final class CredentialStoreStateMachine
431
435
Future <void > onClearCredentials (
432
436
CredentialStoreClearCredentials event,
433
437
) async {
434
- final authConfig = expect <AuthConfiguration >();
435
-
436
438
final clearKeys = event.keys;
437
439
final deletions = < String > [];
438
440
bool shouldDelete (String key) =>
439
441
clearKeys.isEmpty || clearKeys.contains (key);
440
442
441
- final userPoolConfig = authConfig.userPoolConfig;
442
- if (userPoolConfig != null ) {
443
- final userPoolKeys = CognitoUserPoolKeys (userPoolConfig.appClientId);
443
+ if (_hasUserPool) {
444
+ final userPoolKeys = CognitoUserPoolKeys (_authOutputs.userPoolClientId! );
444
445
for (final key in userPoolKeys) {
445
446
if (shouldDelete (key)) {
446
447
deletions.add (key);
447
448
}
448
449
}
449
450
}
450
451
451
- final hostedUiConfig = authConfig.hostedUiConfig;
452
- if (hostedUiConfig != null ) {
453
- final hostedUiKeys = HostedUiKeys (hostedUiConfig.appClientId);
452
+ if (_hasHostedUi) {
453
+ final hostedUiKeys = HostedUiKeys (_authOutputs.userPoolClientId! );
454
454
for (final key in hostedUiKeys) {
455
455
if (shouldDelete (key)) {
456
456
deletions.add (key);
457
457
}
458
458
}
459
459
}
460
460
461
- final identityPoolConfig = authConfig.identityPoolConfig;
462
- if (identityPoolConfig != null ) {
461
+ if (_hasIdentityPool) {
463
462
final identityPoolKeys =
464
- CognitoIdentityPoolKeys (identityPoolConfig.poolId );
463
+ CognitoIdentityPoolKeys (_authOutputs.identityPoolId ! );
465
464
for (final key in identityPoolKeys) {
466
465
if (shouldDelete (key)) {
467
466
deletions.add (key);
0 commit comments