@@ -18,6 +18,8 @@ import 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/
18
18
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart' ;
19
19
import 'package:amplify_auth_cognito_dart/src/state/state.dart' ;
20
20
import 'package:amplify_core/amplify_core.dart' ;
21
+ // ignore: implementation_imports
22
+ import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart' ;
21
23
import 'package:meta/meta.dart' ;
22
24
23
25
/// {@template amplify_auth_cognito.fetch_auth_session_state_machine}
@@ -51,10 +53,12 @@ final class FetchAuthSessionStateMachine
51
53
/// The registered Cognito Identity client.
52
54
CognitoIdentityClient get _cognitoIdentityClient => expect ();
53
55
54
- /// The registered user pool config .
55
- CognitoUserPoolConfig ? get _userPoolConfig => get ();
56
+ /// The registered auth outputs .
57
+ AuthOutputs ? get _authConfig => get ();
56
58
57
59
/// The registered identity pool config
60
+ // TODO(nikahsn): remove after refactoring CognitoIdentityPoolKeys to use
61
+ // AmplifyOutputs type
58
62
CognitoIdentityCredentialsProvider ? get _identityPoolConfig => get ();
59
63
60
64
/// Invalidates the current session, forcing a refresh on the next retrieval
@@ -113,11 +117,10 @@ final class FetchAuthSessionStateMachine
113
117
return const {};
114
118
}
115
119
final logins = < String , String > {};
116
- final userPoolConfig = _userPoolConfig;
117
- if (userPoolConfig != null &&
120
+ if (_authConfig? .userPoolId != null &&
118
121
federatedIdentity.provider == AuthProvider .cognito) {
119
- final userPoolKey = 'cognito-idp.${userPoolConfig . region }.amazonaws.com/'
120
- '${userPoolConfig . poolId }' ;
122
+ final userPoolKey = 'cognito-idp.${_authConfig ?. awsRegion }.amazonaws.com/'
123
+ '${_authConfig ?. userPoolId }' ;
121
124
logins[userPoolKey] = federatedIdentity.token;
122
125
} else {
123
126
logins[federatedIdentity.provider.identityPoolProviderName] =
@@ -128,14 +131,14 @@ final class FetchAuthSessionStateMachine
128
131
129
132
/// Gets the identity ID from the authorization state machine.
130
133
Future <String > _getIdentityId ({
131
- required CognitoIdentityCredentialsProvider config ,
134
+ required String identityPoolId ,
132
135
_FederatedIdentity ? federatedIdentity,
133
136
}) async {
134
137
final resp = await _withZoneOverrides (
135
138
() => _cognitoIdentityClient
136
139
.getId (
137
140
GetIdInput (
138
- identityPoolId: config.poolId ,
141
+ identityPoolId: identityPoolId ,
139
142
logins: _logins (federatedIdentity),
140
143
),
141
144
)
@@ -203,7 +206,7 @@ final class FetchAuthSessionStateMachine
203
206
final options = event.options ?? const FetchAuthSessionOptions ();
204
207
final result = await manager.loadCredentials ();
205
208
206
- final hasUserPool = _userPoolConfig != null ;
209
+ final hasUserPool = _authConfig ? .userPoolId != null ;
207
210
final userPoolTokens = result.userPoolTokens;
208
211
final accessTokenExpiration = userPoolTokens? .accessToken.claims.expiration;
209
212
final idTokenExpiration = userPoolTokens? .idToken.claims.expiration;
@@ -215,7 +218,7 @@ final class FetchAuthSessionStateMachine
215
218
_isExpired (accessTokenExpiration) ||
216
219
_isExpired (idTokenExpiration));
217
220
218
- final hasIdentityPool = _identityPoolConfig != null ;
221
+ final hasIdentityPool = _authConfig ? .identityPoolId != null ;
219
222
final awsCredentials = result.awsCredentials;
220
223
final awsCredentialsExpiration = awsCredentials? .expiration;
221
224
final forceRefreshAwsCredentials = options.forceRefresh;
@@ -298,7 +301,7 @@ final class FetchAuthSessionStateMachine
298
301
'Amplify.Auth.federateToIdentityPool.' ,
299
302
);
300
303
}
301
- if (_identityPoolConfig == null ) {
304
+ if (_authConfig ? .identityPoolId == null ) {
302
305
throw const InvalidAccountTypeException .noIdentityPool ();
303
306
}
304
307
@@ -363,7 +366,7 @@ final class FetchAuthSessionStateMachine
363
366
AuthResult <AWSCredentials > credentialsResult;
364
367
AuthResult <String > identityIdResult;
365
368
366
- final hasUserPool = _userPoolConfig != null ;
369
+ final hasUserPool = _authConfig ? .userPoolId != null ;
367
370
var userPoolTokens = result.userPoolTokens;
368
371
if (! hasUserPool) {
369
372
userPoolTokensResult = const AuthResult .error (
@@ -402,7 +405,7 @@ final class FetchAuthSessionStateMachine
402
405
final existingIdentityId = result.identityId;
403
406
final existingAwsCredentials = result.awsCredentials;
404
407
405
- final hasIdentityPool = _identityPoolConfig != null ;
408
+ final hasIdentityPool = _authConfig ? .identityPoolId != null ;
406
409
407
410
if (! hasIdentityPool) {
408
411
credentialsResult = const AuthResult <AWSCredentials >.error (
@@ -461,14 +464,13 @@ final class FetchAuthSessionStateMachine
461
464
String ? existingIdentityId,
462
465
_FederatedIdentity ? federatedIdentity,
463
466
}) async {
464
- final identityPoolConfig = _identityPoolConfig;
465
- if (identityPoolConfig == null ) {
467
+ if (_identityPoolConfig == null || _authConfig? .identityPoolId == null ) {
466
468
throw const InvalidAccountTypeException .noIdentityPool ();
467
469
}
468
470
try {
469
471
final identityId = existingIdentityId ??
470
472
await _getIdentityId (
471
- config : identityPoolConfig ,
473
+ identityPoolId : _authConfig ! .identityPoolId ! ,
472
474
federatedIdentity: federatedIdentity,
473
475
);
474
476
@@ -499,7 +501,7 @@ final class FetchAuthSessionStateMachine
499
501
// session expired in an identity pool not supporting unauthenticated
500
502
// access and we should prevent further attempts at refreshing.
501
503
await manager.clearCredentials (
502
- CognitoIdentityPoolKeys (identityPoolConfig ),
504
+ CognitoIdentityPoolKeys (_identityPoolConfig ! ),
503
505
);
504
506
Error .throwWithStackTrace (
505
507
e.toSessionExpired ('The AWS credentials could not be retrieved' ),
@@ -513,22 +515,24 @@ final class FetchAuthSessionStateMachine
513
515
) async {
514
516
final deviceSecrets = await getOrCreate <DeviceMetadataRepository >()
515
517
.get (userPoolTokens.username);
516
- final config = _userPoolConfig! ;
517
518
final refreshRequest = cognito_idp.InitiateAuthRequest .build ((b) {
518
519
b
519
520
..authFlow = cognito_idp.AuthFlowType .refreshTokenAuth
520
- ..clientId = config.appClientId
521
+ ..clientId = _authConfig ? .userPoolClientId
521
522
..authParameters.addAll ({
522
523
CognitoConstants .refreshToken: userPoolTokens.refreshToken,
523
524
})
524
525
..analyticsMetadata = get <AnalyticsMetadataType >()? .toBuilder ();
525
526
526
- if (config.appClientSecret != null ) {
527
+ // ignore: invalid_use_of_internal_member
528
+ if (_authConfig? .appClientSecret != null &&
529
+ _authConfig? .userPoolClientId != null ) {
527
530
b.authParameters[CognitoConstants .challengeParamSecretHash] =
528
531
computeSecretHash (
529
532
userPoolTokens.username,
530
- config.appClientId,
531
- config.appClientSecret! ,
533
+ _authConfig! .userPoolClientId! ,
534
+ // ignore: invalid_use_of_internal_member
535
+ _authConfig! .appClientSecret! ,
532
536
);
533
537
}
534
538
@@ -573,12 +577,11 @@ final class FetchAuthSessionStateMachine
573
577
case CognitoSignInMethod .hostedUi:
574
578
keys = HostedUiKeys (expect ());
575
579
}
576
- final identityPoolConfig = _identityPoolConfig;
577
580
await manager.clearCredentials ([
578
581
...keys,
579
- if (identityPoolConfig != null )
582
+ if (_identityPoolConfig != null )
580
583
// Clear associated AWS credentials
581
- ...CognitoIdentityPoolKeys (identityPoolConfig ),
584
+ ...CognitoIdentityPoolKeys (_identityPoolConfig ! ),
582
585
]);
583
586
Error .throwWithStackTrace (
584
587
e.toSessionExpired ('The tokens could not be refreshed' ),
0 commit comments