Skip to content

Commit 4eba4c8

Browse files
authored
chore(auth): sign-in state machine to use AmplifyOutputs instead of AmplifyConfig types (#5231)
1 parent 88e2df5 commit 4eba4c8

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

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

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import 'package:amplify_auth_cognito_dart/src/sdk/sdk_bridge.dart';
2929
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
3030
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
3131
import 'package:amplify_core/amplify_core.dart';
32+
// ignore: implementation_imports
33+
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
3234
import 'package:async/async.dart';
3335
import 'package:built_collection/built_collection.dart';
3436
import 'package:meta/meta.dart';
@@ -59,12 +61,20 @@ final class SignInStateMachine
5961
/// Parameters to the flow.
6062
late SignInParameters parameters;
6163

62-
/// The configured user pool.
63-
late final CognitoUserPoolConfig config = expect();
64-
6564
/// The configured identity pool.
65+
// TODO(nikahsn): remove after refactoring CognitoIdentityPoolKeys to use
66+
// AmplifyOutputs type
6667
CognitoIdentityCredentialsProvider? get identityPoolConfig => get();
6768

69+
AuthOutputs get _authOutputs {
70+
final authOutputs = get<AuthOutputs>();
71+
if (authOutputs?.userPoolId == null ||
72+
authOutputs?.userPoolClientId == null) {
73+
throw const InvalidAccountTypeException.noUserPool();
74+
}
75+
return authOutputs!;
76+
}
77+
6878
/// The Cognito Identity Provider service client.
6979
late final CognitoIdentityProviderClient cognitoIdentityProvider = expect();
7080

@@ -344,7 +354,7 @@ final class SignInStateMachine
344354
if (_user.deviceSecrets?.deviceKey case final deviceKey?)
345355
CognitoConstants.challengeParamDeviceKey: deviceKey,
346356
})
347-
..clientId = config.appClientId
357+
..clientId = _authOutputs.userPoolClientId
348358
..clientMetadata.addAll(event.clientMetadata)
349359
..analyticsMetadata = get<AnalyticsMetadataType>()?.toBuilder(),
350360
);
@@ -370,9 +380,10 @@ final class SignInStateMachine
370380
final workerMessage = SrpPasswordVerifierMessage((b) {
371381
b
372382
..initResult = initResult
373-
..clientId = config.appClientId
374-
..clientSecret = config.appClientSecret
375-
..poolId = config.poolId
383+
..clientId = _authOutputs.userPoolClientId
384+
// ignore: invalid_use_of_internal_member
385+
..clientSecret = _authOutputs.appClientSecret
386+
..poolId = _authOutputs.userPoolId
376387
..deviceKey = _user.deviceSecrets?.deviceKey
377388
..challengeParameters = BuiltMap(_publicChallengeParameters)
378389
..parameters = SignInParameters(
@@ -394,7 +405,7 @@ final class SignInStateMachine
394405
_initResult ??= await _initSrp();
395406
return RespondToAuthChallengeRequest.build((b) {
396407
b
397-
..clientId = config.appClientId
408+
..clientId = _authOutputs.userPoolClientId
398409
..challengeName = ChallengeNameType.deviceSrpAuth
399410
..challengeResponses.addAll({
400411
CognitoConstants.challengeParamUsername: cognitoUsername,
@@ -416,8 +427,9 @@ final class SignInStateMachine
416427
b
417428
..deviceSecrets = _user.deviceSecrets!.build()
418429
..initResult = _initResult
419-
..clientId = config.appClientId
420-
..clientSecret = config.appClientSecret
430+
..clientId = _authOutputs.userPoolClientId
431+
// ignore: invalid_use_of_internal_member
432+
..clientSecret = _authOutputs.appClientSecret
421433
..challengeParameters = BuiltMap(_publicChallengeParameters);
422434
});
423435
worker.sink.add(workerMessage);
@@ -432,7 +444,7 @@ final class SignInStateMachine
432444
_enableMfaType = MfaType.sms;
433445
return RespondToAuthChallengeRequest.build((b) {
434446
b
435-
..clientId = config.appClientId
447+
..clientId = _authOutputs.userPoolClientId
436448
..challengeName = _challengeName
437449
..challengeResponses.addAll({
438450
CognitoConstants.challengeParamUsername: cognitoUsername,
@@ -449,7 +461,7 @@ final class SignInStateMachine
449461
) async {
450462
return RespondToAuthChallengeRequest.build((b) {
451463
b
452-
..clientId = config.appClientId
464+
..clientId = _authOutputs.userPoolClientId
453465
..challengeName = _challengeName
454466
..challengeResponses.addAll({
455467
CognitoConstants.challengeParamUsername: cognitoUsername,
@@ -490,7 +502,7 @@ final class SignInStateMachine
490502
return InitiateAuthRequest.build((b) {
491503
b
492504
..authFlow = AuthFlowType.userSrpAuth
493-
..clientId = config.appClientId
505+
..clientId = _authOutputs.userPoolClientId
494506
..authParameters.addAll({
495507
CognitoConstants.challengeParamUsername: providedUsername,
496508
CognitoConstants.challengeParamSrpA:
@@ -509,7 +521,7 @@ final class SignInStateMachine
509521
return InitiateAuthRequest.build((b) {
510522
b
511523
..authFlow = AuthFlowType.userPasswordAuth
512-
..clientId = config.appClientId
524+
..clientId = _authOutputs.userPoolClientId
513525
..authParameters.addAll({
514526
CognitoConstants.challengeParamUsername: providedUsername,
515527
CognitoConstants.challengeParamPassword: password,
@@ -562,7 +574,7 @@ final class SignInStateMachine
562574
..authFlow = AuthFlowType.customAuth
563575
..authParameters[CognitoConstants.challengeParamUsername] =
564576
providedUsername
565-
..clientId = config.appClientId
577+
..clientId = _authOutputs.userPoolClientId
566578
..clientMetadata.addAll(event.clientMetadata);
567579
});
568580
}
@@ -641,7 +653,7 @@ final class SignInStateMachine
641653
// Must be the session from `VerifySoftwareToken`
642654
CognitoConstants.challengeParamSession: _session!,
643655
})
644-
..clientId = config.appClientId
656+
..clientId = _authOutputs.userPoolClientId
645657
..clientMetadata.addAll(event.clientMetadata);
646658
});
647659
}
@@ -663,7 +675,7 @@ final class SignInStateMachine
663675
_ => throw ArgumentError('Must be either SMS or TOTP'),
664676
},
665677
})
666-
..clientId = config.appClientId
678+
..clientId = _authOutputs.userPoolClientId
667679
..clientMetadata.addAll(event.clientMetadata);
668680
});
669681
}
@@ -681,7 +693,7 @@ final class SignInStateMachine
681693
CognitoConstants.challengeParamUsername: cognitoUsername,
682694
CognitoConstants.challengeParamSoftwareTokenMfaCode: event.answer,
683695
})
684-
..clientId = config.appClientId
696+
..clientId = _authOutputs.userPoolClientId
685697
..clientMetadata.addAll(event.clientMetadata);
686698
});
687699
}
@@ -789,11 +801,12 @@ final class SignInStateMachine
789801
initRequest = initRequest.rebuild((b) {
790802
b.analyticsMetadata = get<AnalyticsMetadataType>()?.toBuilder();
791803

792-
if (config.appClientSecret case final appClientSecret?) {
804+
// ignore: invalid_use_of_internal_member
805+
if (_authOutputs.appClientSecret case final appClientSecret?) {
793806
b.authParameters[CognitoConstants.challengeParamSecretHash] =
794807
computeSecretHash(
795808
providedUsername,
796-
config.appClientId,
809+
_authOutputs.userPoolClientId!,
797810
appClientSecret,
798811
);
799812
}
@@ -1003,11 +1016,12 @@ final class SignInStateMachine
10031016
..clientMetadata.replace(event?.clientMetadata ?? const {})
10041017
..analyticsMetadata = get<AnalyticsMetadataType>()?.toBuilder();
10051018

1006-
if (config.appClientSecret case final appClientSecret?) {
1019+
// ignore: invalid_use_of_internal_member
1020+
if (_authOutputs.appClientSecret case final appClientSecret?) {
10071021
b.challengeResponses[CognitoConstants.challengeParamSecretHash] ??=
10081022
computeSecretHash(
10091023
cognitoUsername,
1010-
config.appClientId,
1024+
_authOutputs.userPoolClientId!,
10111025
appClientSecret,
10121026
);
10131027
}

0 commit comments

Comments
 (0)