Skip to content

Commit 75f7c94

Browse files
authored
chore(auth): fetch auth session state machine to use AmplifyOutputs instead of AmplifyConfig types (#5234)
1 parent 4eba4c8 commit 75f7c94

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

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

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/
1818
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
1919
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
2020
import 'package:amplify_core/amplify_core.dart';
21+
// ignore: implementation_imports
22+
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
2123
import 'package:meta/meta.dart';
2224

2325
/// {@template amplify_auth_cognito.fetch_auth_session_state_machine}
@@ -51,10 +53,12 @@ final class FetchAuthSessionStateMachine
5153
/// The registered Cognito Identity client.
5254
CognitoIdentityClient get _cognitoIdentityClient => expect();
5355

54-
/// The registered user pool config.
55-
CognitoUserPoolConfig? get _userPoolConfig => get();
56+
/// The registered auth outputs.
57+
AuthOutputs? get _authConfig => get();
5658

5759
/// The registered identity pool config
60+
// TODO(nikahsn): remove after refactoring CognitoIdentityPoolKeys to use
61+
// AmplifyOutputs type
5862
CognitoIdentityCredentialsProvider? get _identityPoolConfig => get();
5963

6064
/// Invalidates the current session, forcing a refresh on the next retrieval
@@ -113,11 +117,10 @@ final class FetchAuthSessionStateMachine
113117
return const {};
114118
}
115119
final logins = <String, String>{};
116-
final userPoolConfig = _userPoolConfig;
117-
if (userPoolConfig != null &&
120+
if (_authConfig?.userPoolId != null &&
118121
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}';
121124
logins[userPoolKey] = federatedIdentity.token;
122125
} else {
123126
logins[federatedIdentity.provider.identityPoolProviderName] =
@@ -128,14 +131,14 @@ final class FetchAuthSessionStateMachine
128131

129132
/// Gets the identity ID from the authorization state machine.
130133
Future<String> _getIdentityId({
131-
required CognitoIdentityCredentialsProvider config,
134+
required String identityPoolId,
132135
_FederatedIdentity? federatedIdentity,
133136
}) async {
134137
final resp = await _withZoneOverrides(
135138
() => _cognitoIdentityClient
136139
.getId(
137140
GetIdInput(
138-
identityPoolId: config.poolId,
141+
identityPoolId: identityPoolId,
139142
logins: _logins(federatedIdentity),
140143
),
141144
)
@@ -203,7 +206,7 @@ final class FetchAuthSessionStateMachine
203206
final options = event.options ?? const FetchAuthSessionOptions();
204207
final result = await manager.loadCredentials();
205208

206-
final hasUserPool = _userPoolConfig != null;
209+
final hasUserPool = _authConfig?.userPoolId != null;
207210
final userPoolTokens = result.userPoolTokens;
208211
final accessTokenExpiration = userPoolTokens?.accessToken.claims.expiration;
209212
final idTokenExpiration = userPoolTokens?.idToken.claims.expiration;
@@ -215,7 +218,7 @@ final class FetchAuthSessionStateMachine
215218
_isExpired(accessTokenExpiration) ||
216219
_isExpired(idTokenExpiration));
217220

218-
final hasIdentityPool = _identityPoolConfig != null;
221+
final hasIdentityPool = _authConfig?.identityPoolId != null;
219222
final awsCredentials = result.awsCredentials;
220223
final awsCredentialsExpiration = awsCredentials?.expiration;
221224
final forceRefreshAwsCredentials = options.forceRefresh;
@@ -298,7 +301,7 @@ final class FetchAuthSessionStateMachine
298301
'Amplify.Auth.federateToIdentityPool.',
299302
);
300303
}
301-
if (_identityPoolConfig == null) {
304+
if (_authConfig?.identityPoolId == null) {
302305
throw const InvalidAccountTypeException.noIdentityPool();
303306
}
304307

@@ -363,7 +366,7 @@ final class FetchAuthSessionStateMachine
363366
AuthResult<AWSCredentials> credentialsResult;
364367
AuthResult<String> identityIdResult;
365368

366-
final hasUserPool = _userPoolConfig != null;
369+
final hasUserPool = _authConfig?.userPoolId != null;
367370
var userPoolTokens = result.userPoolTokens;
368371
if (!hasUserPool) {
369372
userPoolTokensResult = const AuthResult.error(
@@ -402,7 +405,7 @@ final class FetchAuthSessionStateMachine
402405
final existingIdentityId = result.identityId;
403406
final existingAwsCredentials = result.awsCredentials;
404407

405-
final hasIdentityPool = _identityPoolConfig != null;
408+
final hasIdentityPool = _authConfig?.identityPoolId != null;
406409

407410
if (!hasIdentityPool) {
408411
credentialsResult = const AuthResult<AWSCredentials>.error(
@@ -461,14 +464,13 @@ final class FetchAuthSessionStateMachine
461464
String? existingIdentityId,
462465
_FederatedIdentity? federatedIdentity,
463466
}) async {
464-
final identityPoolConfig = _identityPoolConfig;
465-
if (identityPoolConfig == null) {
467+
if (_identityPoolConfig == null || _authConfig?.identityPoolId == null) {
466468
throw const InvalidAccountTypeException.noIdentityPool();
467469
}
468470
try {
469471
final identityId = existingIdentityId ??
470472
await _getIdentityId(
471-
config: identityPoolConfig,
473+
identityPoolId: _authConfig!.identityPoolId!,
472474
federatedIdentity: federatedIdentity,
473475
);
474476

@@ -499,7 +501,7 @@ final class FetchAuthSessionStateMachine
499501
// session expired in an identity pool not supporting unauthenticated
500502
// access and we should prevent further attempts at refreshing.
501503
await manager.clearCredentials(
502-
CognitoIdentityPoolKeys(identityPoolConfig),
504+
CognitoIdentityPoolKeys(_identityPoolConfig!),
503505
);
504506
Error.throwWithStackTrace(
505507
e.toSessionExpired('The AWS credentials could not be retrieved'),
@@ -513,22 +515,24 @@ final class FetchAuthSessionStateMachine
513515
) async {
514516
final deviceSecrets = await getOrCreate<DeviceMetadataRepository>()
515517
.get(userPoolTokens.username);
516-
final config = _userPoolConfig!;
517518
final refreshRequest = cognito_idp.InitiateAuthRequest.build((b) {
518519
b
519520
..authFlow = cognito_idp.AuthFlowType.refreshTokenAuth
520-
..clientId = config.appClientId
521+
..clientId = _authConfig?.userPoolClientId
521522
..authParameters.addAll({
522523
CognitoConstants.refreshToken: userPoolTokens.refreshToken,
523524
})
524525
..analyticsMetadata = get<AnalyticsMetadataType>()?.toBuilder();
525526

526-
if (config.appClientSecret != null) {
527+
// ignore: invalid_use_of_internal_member
528+
if (_authConfig?.appClientSecret != null &&
529+
_authConfig?.userPoolClientId != null) {
527530
b.authParameters[CognitoConstants.challengeParamSecretHash] =
528531
computeSecretHash(
529532
userPoolTokens.username,
530-
config.appClientId,
531-
config.appClientSecret!,
533+
_authConfig!.userPoolClientId!,
534+
// ignore: invalid_use_of_internal_member
535+
_authConfig!.appClientSecret!,
532536
);
533537
}
534538

@@ -573,12 +577,11 @@ final class FetchAuthSessionStateMachine
573577
case CognitoSignInMethod.hostedUi:
574578
keys = HostedUiKeys(expect());
575579
}
576-
final identityPoolConfig = _identityPoolConfig;
577580
await manager.clearCredentials([
578581
...keys,
579-
if (identityPoolConfig != null)
582+
if (_identityPoolConfig != null)
580583
// Clear associated AWS credentials
581-
...CognitoIdentityPoolKeys(identityPoolConfig),
584+
...CognitoIdentityPoolKeys(_identityPoolConfig!),
582585
]);
583586
Error.throwWithStackTrace(
584587
e.toSessionExpired('The tokens could not be refreshed'),

0 commit comments

Comments
 (0)