Skip to content

Commit 34e3139

Browse files
author
Nika Hassani
committed
chore(auth): sign-in state machine to use AmplifyOutputs instead of AmplifyConfig types
1 parent c0f6f09 commit 34e3139

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

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

Lines changed: 37 additions & 20 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';
@@ -60,11 +62,22 @@ final class SignInStateMachine
6062
late SignInParameters parameters;
6163

6264
/// The configured user pool.
63-
late final CognitoUserPoolConfig config = expect();
65+
//late final CognitoUserPoolConfig config = expect();
6466

6567
/// The configured identity pool.
68+
// TODO(nikahsn): remove after refactoring CognitoIdentityPoolKeys to use
69+
// AmplifyOutputs type
6670
CognitoIdentityCredentialsProvider? get identityPoolConfig => get();
6771

72+
AuthOutputs get _authOutputs {
73+
final authOutputs = get<AuthOutputs>();
74+
if (authOutputs?.userPoolId == null ||
75+
authOutputs?.userPoolClientId == null) {
76+
throw const InvalidAccountTypeException.noUserPool();
77+
}
78+
return authOutputs!;
79+
}
80+
6881
/// The Cognito Identity Provider service client.
6982
late final CognitoIdentityProviderClient cognitoIdentityProvider = expect();
7083

@@ -344,7 +357,7 @@ final class SignInStateMachine
344357
if (_user.deviceSecrets?.deviceKey case final deviceKey?)
345358
CognitoConstants.challengeParamDeviceKey: deviceKey,
346359
})
347-
..clientId = config.appClientId
360+
..clientId = _authOutputs.userPoolClientId
348361
..clientMetadata.addAll(event.clientMetadata)
349362
..analyticsMetadata = get<AnalyticsMetadataType>()?.toBuilder(),
350363
);
@@ -370,9 +383,10 @@ final class SignInStateMachine
370383
final workerMessage = SrpPasswordVerifierMessage((b) {
371384
b
372385
..initResult = initResult
373-
..clientId = config.appClientId
374-
..clientSecret = config.appClientSecret
375-
..poolId = config.poolId
386+
..clientId = _authOutputs.userPoolClientId
387+
// ignore: invalid_use_of_internal_member
388+
..clientSecret = _authOutputs.appClientSecret
389+
..poolId = _authOutputs.userPoolId
376390
..deviceKey = _user.deviceSecrets?.deviceKey
377391
..challengeParameters = BuiltMap(_publicChallengeParameters)
378392
..parameters = SignInParameters(
@@ -394,7 +408,7 @@ final class SignInStateMachine
394408
_initResult ??= await _initSrp();
395409
return RespondToAuthChallengeRequest.build((b) {
396410
b
397-
..clientId = config.appClientId
411+
..clientId = _authOutputs.userPoolClientId
398412
..challengeName = ChallengeNameType.deviceSrpAuth
399413
..challengeResponses.addAll({
400414
CognitoConstants.challengeParamUsername: cognitoUsername,
@@ -416,8 +430,9 @@ final class SignInStateMachine
416430
b
417431
..deviceSecrets = _user.deviceSecrets!.build()
418432
..initResult = _initResult
419-
..clientId = config.appClientId
420-
..clientSecret = config.appClientSecret
433+
..clientId = _authOutputs.userPoolClientId
434+
// ignore: invalid_use_of_internal_member
435+
..clientSecret = _authOutputs.appClientSecret
421436
..challengeParameters = BuiltMap(_publicChallengeParameters);
422437
});
423438
worker.sink.add(workerMessage);
@@ -432,7 +447,7 @@ final class SignInStateMachine
432447
_enableMfaType = MfaType.sms;
433448
return RespondToAuthChallengeRequest.build((b) {
434449
b
435-
..clientId = config.appClientId
450+
..clientId = _authOutputs.userPoolClientId
436451
..challengeName = _challengeName
437452
..challengeResponses.addAll({
438453
CognitoConstants.challengeParamUsername: cognitoUsername,
@@ -449,7 +464,7 @@ final class SignInStateMachine
449464
) async {
450465
return RespondToAuthChallengeRequest.build((b) {
451466
b
452-
..clientId = config.appClientId
467+
..clientId = _authOutputs.userPoolClientId
453468
..challengeName = _challengeName
454469
..challengeResponses.addAll({
455470
CognitoConstants.challengeParamUsername: cognitoUsername,
@@ -490,7 +505,7 @@ final class SignInStateMachine
490505
return InitiateAuthRequest.build((b) {
491506
b
492507
..authFlow = AuthFlowType.userSrpAuth
493-
..clientId = config.appClientId
508+
..clientId = _authOutputs.userPoolClientId
494509
..authParameters.addAll({
495510
CognitoConstants.challengeParamUsername: providedUsername,
496511
CognitoConstants.challengeParamSrpA:
@@ -509,7 +524,7 @@ final class SignInStateMachine
509524
return InitiateAuthRequest.build((b) {
510525
b
511526
..authFlow = AuthFlowType.userPasswordAuth
512-
..clientId = config.appClientId
527+
..clientId = _authOutputs.userPoolClientId
513528
..authParameters.addAll({
514529
CognitoConstants.challengeParamUsername: providedUsername,
515530
CognitoConstants.challengeParamPassword: password,
@@ -562,7 +577,7 @@ final class SignInStateMachine
562577
..authFlow = AuthFlowType.customAuth
563578
..authParameters[CognitoConstants.challengeParamUsername] =
564579
providedUsername
565-
..clientId = config.appClientId
580+
..clientId = _authOutputs.userPoolClientId
566581
..clientMetadata.addAll(event.clientMetadata);
567582
});
568583
}
@@ -641,7 +656,7 @@ final class SignInStateMachine
641656
// Must be the session from `VerifySoftwareToken`
642657
CognitoConstants.challengeParamSession: _session!,
643658
})
644-
..clientId = config.appClientId
659+
..clientId = _authOutputs.userPoolClientId
645660
..clientMetadata.addAll(event.clientMetadata);
646661
});
647662
}
@@ -663,7 +678,7 @@ final class SignInStateMachine
663678
_ => throw ArgumentError('Must be either SMS or TOTP'),
664679
},
665680
})
666-
..clientId = config.appClientId
681+
..clientId = _authOutputs.userPoolClientId
667682
..clientMetadata.addAll(event.clientMetadata);
668683
});
669684
}
@@ -681,7 +696,7 @@ final class SignInStateMachine
681696
CognitoConstants.challengeParamUsername: cognitoUsername,
682697
CognitoConstants.challengeParamSoftwareTokenMfaCode: event.answer,
683698
})
684-
..clientId = config.appClientId
699+
..clientId = _authOutputs.userPoolClientId
685700
..clientMetadata.addAll(event.clientMetadata);
686701
});
687702
}
@@ -789,11 +804,12 @@ final class SignInStateMachine
789804
initRequest = initRequest.rebuild((b) {
790805
b.analyticsMetadata = get<AnalyticsMetadataType>()?.toBuilder();
791806

792-
if (config.appClientSecret case final appClientSecret?) {
807+
// ignore: invalid_use_of_internal_member
808+
if (_authOutputs.appClientSecret case final appClientSecret?) {
793809
b.authParameters[CognitoConstants.challengeParamSecretHash] =
794810
computeSecretHash(
795811
providedUsername,
796-
config.appClientId,
812+
_authOutputs.userPoolClientId!,
797813
appClientSecret,
798814
);
799815
}
@@ -1003,11 +1019,12 @@ final class SignInStateMachine
10031019
..clientMetadata.replace(event?.clientMetadata ?? const {})
10041020
..analyticsMetadata = get<AnalyticsMetadataType>()?.toBuilder();
10051021

1006-
if (config.appClientSecret case final appClientSecret?) {
1022+
// ignore: invalid_use_of_internal_member
1023+
if (_authOutputs.appClientSecret case final appClientSecret?) {
10071024
b.challengeResponses[CognitoConstants.challengeParamSecretHash] ??=
10081025
computeSecretHash(
10091026
cognitoUsername,
1010-
config.appClientId,
1027+
_authOutputs.userPoolClientId!,
10111028
appClientSecret,
10121029
);
10131030
}

0 commit comments

Comments
 (0)