Skip to content

Commit ecd7a0c

Browse files
author
Nika Hassani
committed
chore(auth): legacy credential provider to use AuthOutputs instead of AmplifyConfig types
1 parent 5bfd639 commit ecd7a0c

File tree

8 files changed

+139
-172
lines changed

8 files changed

+139
-172
lines changed

packages/auth/amplify_auth_cognito/lib/src/credentials/legacy_credential_provider_android.dart

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import 'package:amplify_auth_cognito/src/native_auth_plugin.g.dart'
1010
import 'package:amplify_auth_cognito_dart/src/credentials/legacy_credential_provider.dart';
1111
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
1212
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
13-
import 'package:amplify_core/src/config/auth/cognito/credentials_provider.dart';
14-
import 'package:amplify_core/src/config/auth/cognito/oauth.dart';
15-
import 'package:amplify_core/src/config/auth/cognito/user_pool.dart';
13+
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
1614

1715
/// {@template amplify_auth_cognito.legacy_android_credential_provider}
1816
/// The implementation of [LegacyCredentialProvider] for migrating
@@ -25,53 +23,49 @@ class LegacyCredentialProviderAndroid implements LegacyCredentialProvider {
2523
final CognitoAuthStateMachine _stateMachine;
2624

2725
@override
28-
Future<CredentialStoreData?> fetchLegacyCredentials({
29-
CognitoUserPoolConfig? userPoolConfig,
30-
CognitoIdentityCredentialsProvider? identityPoolConfig,
31-
CognitoOAuthConfig? hostedUiConfig,
32-
}) async {
26+
Future<CredentialStoreData?> fetchLegacyCredentials(
27+
AuthOutputs authOutputs,
28+
) async {
3329
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
3430
final legacyCredentials = await bridge.getLegacyCredentials(
35-
identityPoolConfig?.poolId,
36-
userPoolConfig?.appClientId,
31+
authOutputs.identityPoolId,
32+
authOutputs.userPoolClientId,
3733
);
3834
return legacyCredentials.toCredentialStoreData();
3935
}
4036

4137
@override
42-
Future<void> deleteLegacyCredentials({
43-
CognitoUserPoolConfig? userPoolConfig,
44-
CognitoIdentityCredentialsProvider? identityPoolConfig,
45-
CognitoOAuthConfig? hostedUiConfig,
46-
}) {
38+
Future<void> deleteLegacyCredentials(
39+
AuthOutputs authOutputs,
40+
) {
4741
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
4842
return bridge.clearLegacyCredentials();
4943
}
5044

5145
@override
52-
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets({
53-
required String username,
54-
CognitoUserPoolConfig? userPoolConfig,
55-
}) async {
56-
if (userPoolConfig == null) return null;
46+
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets(
47+
String username,
48+
AuthOutputs authOutputs,
49+
) async {
50+
if (authOutputs.userPoolId == null) return null;
5751
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
5852
final device = await bridge.fetchLegacyDeviceSecrets(
5953
username,
60-
userPoolConfig.poolId,
54+
authOutputs.userPoolId!,
6155
);
6256
return device?.toLegacyDeviceDetails();
6357
}
6458

6559
@override
66-
Future<void> deleteLegacyDeviceSecrets({
67-
required String username,
68-
CognitoUserPoolConfig? userPoolConfig,
69-
}) async {
70-
if (userPoolConfig != null) {
60+
Future<void> deleteLegacyDeviceSecrets(
61+
String username,
62+
AuthOutputs authOutputs,
63+
) async {
64+
if (authOutputs.userPoolId != null) {
7165
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
7266
return bridge.deleteLegacyDeviceSecrets(
7367
username,
74-
userPoolConfig.poolId,
68+
authOutputs.userPoolId!,
7569
);
7670
}
7771
}

packages/auth/amplify_auth_cognito/lib/src/credentials/legacy_credential_provider_impl.dart

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
1212
// ignore: implementation_imports, invalid_use_of_internal_member
1313
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
1414
import 'package:amplify_core/amplify_core.dart';
15+
// ignore: implementation_imports
16+
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
1517

1618
/// {@template amplify_auth_cognito.legacy_credential_provider_impl}
1719
/// The implementation of [LegacyCredentialProvider] for migrating
@@ -35,54 +37,46 @@ class LegacyCredentialProviderImpl implements LegacyCredentialProvider {
3537
}();
3638

3739
@override
38-
Future<CredentialStoreData?> fetchLegacyCredentials({
39-
CognitoUserPoolConfig? userPoolConfig,
40-
CognitoIdentityCredentialsProvider? identityPoolConfig,
41-
CognitoOAuthConfig? hostedUiConfig,
42-
}) async {
40+
Future<CredentialStoreData?> fetchLegacyCredentials(
41+
AuthOutputs authOutputs,
42+
) async {
4343
if (_instance == null) return null;
4444
return _instance.fetchLegacyCredentials(
45-
userPoolConfig: userPoolConfig,
46-
identityPoolConfig: identityPoolConfig,
47-
hostedUiConfig: hostedUiConfig,
45+
authOutputs,
4846
);
4947
}
5048

5149
@override
52-
Future<void> deleteLegacyCredentials({
53-
CognitoUserPoolConfig? userPoolConfig,
54-
CognitoIdentityCredentialsProvider? identityPoolConfig,
55-
CognitoOAuthConfig? hostedUiConfig,
56-
}) async {
50+
Future<void> deleteLegacyCredentials(
51+
AuthOutputs authOutputs,
52+
) async {
5753
if (_instance == null) return;
5854
return _instance.deleteLegacyCredentials(
59-
userPoolConfig: userPoolConfig,
60-
identityPoolConfig: identityPoolConfig,
61-
hostedUiConfig: hostedUiConfig,
55+
authOutputs,
6256
);
6357
}
6458

6559
@override
66-
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets({
67-
required String username,
68-
CognitoUserPoolConfig? userPoolConfig,
69-
}) async {
60+
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets(
61+
String username,
62+
AuthOutputs authOutputs,
63+
) async {
7064
if (_instance == null) return null;
7165
return _instance.fetchLegacyDeviceSecrets(
72-
username: username,
73-
userPoolConfig: userPoolConfig,
66+
username,
67+
authOutputs,
7468
);
7569
}
7670

7771
@override
78-
Future<void> deleteLegacyDeviceSecrets({
79-
required String username,
80-
CognitoUserPoolConfig? userPoolConfig,
81-
}) async {
72+
Future<void> deleteLegacyDeviceSecrets(
73+
String username,
74+
AuthOutputs authOutputs,
75+
) async {
8276
if (_instance == null) return;
8377
return _instance.deleteLegacyDeviceSecrets(
84-
username: username,
85-
userPoolConfig: userPoolConfig,
78+
username,
79+
authOutputs,
8680
);
8781
}
8882
}

packages/auth/amplify_auth_cognito/lib/src/credentials/legacy_credential_provider_ios.dart

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import 'package:amplify_auth_cognito_dart/src/credentials/legacy_credential_prov
1414
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
1515
// ignore: implementation_imports, invalid_use_of_internal_member
1616
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
17+
// ignore: implementation_imports
18+
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
1719
import 'package:amplify_flutter/amplify_flutter.dart';
1820
import 'package:async/async.dart';
1921

@@ -28,22 +30,21 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
2830
final CognitoAuthStateMachine _stateMachine;
2931

3032
@override
31-
Future<CredentialStoreData?> fetchLegacyCredentials({
32-
CognitoUserPoolConfig? userPoolConfig,
33-
CognitoIdentityCredentialsProvider? identityPoolConfig,
34-
CognitoOAuthConfig? hostedUiConfig,
35-
}) async {
33+
Future<CredentialStoreData?> fetchLegacyCredentials(
34+
AuthOutputs authOutputs,
35+
) async {
3636
CognitoUserPoolTokens? userPoolTokens;
37-
if (userPoolConfig != null) {
37+
if (authOutputs.userPoolClientId != null) {
3838
final userPoolStorage = await _getUserPoolStorage();
39-
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
39+
final cognitoUserKeys =
40+
LegacyCognitoUserKeys(authOutputs.userPoolClientId!);
4041
final currentUserId = await userPoolStorage.read(
4142
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
4243
);
4344
if (currentUserId != null) {
4445
final userPoolKeys = LegacyCognitoUserPoolKeys(
4546
currentUserId,
46-
userPoolConfig,
47+
authOutputs.userPoolClientId!,
4748
);
4849
final accessToken = await userPoolStorage.read(
4950
key: userPoolKeys[LegacyCognitoUserPoolKey.accessToken],
@@ -56,7 +57,7 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
5657
);
5758
if (accessToken != null && refreshToken != null && idToken != null) {
5859
// TODO(Jordan-Nelson): fetch sign in method from keychain on iOS
59-
final signInMethod = hostedUiConfig != null
60+
final signInMethod = authOutputs.oauth != null
6061
? CognitoSignInMethod.hostedUi
6162
: CognitoSignInMethod.default$;
6263
userPoolTokens = CognitoUserPoolTokens(
@@ -71,10 +72,9 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
7172

7273
String? identityId;
7374
AWSCredentials? awsCredentials;
74-
final identityPoolId = identityPoolConfig?.poolId;
75-
if (identityPoolId != null) {
75+
if (authOutputs.identityPoolId != null) {
7676
final identityPoolStorage = await _getIdentityPoolStorage(
77-
identityPoolId,
77+
authOutputs.identityPoolId!,
7878
);
7979
const identityPoolKeys = LegacyCognitoIdentityPoolKeys();
8080
identityId = await identityPoolStorage.read(
@@ -122,21 +122,20 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
122122
}
123123

124124
@override
125-
Future<void> deleteLegacyCredentials({
126-
CognitoUserPoolConfig? userPoolConfig,
127-
CognitoIdentityCredentialsProvider? identityPoolConfig,
128-
CognitoOAuthConfig? hostedUiConfig,
129-
}) async {
130-
if (userPoolConfig != null) {
125+
Future<void> deleteLegacyCredentials(
126+
AuthOutputs authOutputs,
127+
) async {
128+
if (authOutputs.userPoolClientId != null) {
131129
final userPoolStorage = await _getUserPoolStorage();
132-
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
130+
final cognitoUserKeys =
131+
LegacyCognitoUserKeys(authOutputs.userPoolClientId!);
133132
final currentUser = await userPoolStorage.read(
134133
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
135134
);
136135
if (currentUser != null) {
137136
final userPoolKeys = LegacyCognitoUserPoolKeys(
138137
currentUser,
139-
userPoolConfig,
138+
authOutputs.userPoolClientId!,
140139
);
141140
await userPoolStorage.deleteMany([
142141
userPoolKeys[LegacyCognitoUserPoolKey.accessToken],
@@ -147,9 +146,9 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
147146
}
148147
}
149148

150-
final identityPoolId = identityPoolConfig?.poolId;
151-
if (identityPoolId != null) {
152-
final identityPoolStorage = await _getIdentityPoolStorage(identityPoolId);
149+
if (authOutputs.identityPoolId != null) {
150+
final identityPoolStorage =
151+
await _getIdentityPoolStorage(authOutputs.identityPoolId!);
153152
const identityPoolKeys = LegacyCognitoIdentityPoolKeys();
154153
await identityPoolStorage.deleteMany([
155154
identityPoolKeys[LegacyCognitoIdentityPoolKey.identityId],
@@ -162,20 +161,21 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
162161
}
163162

164163
@override
165-
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets({
166-
required String username,
167-
CognitoUserPoolConfig? userPoolConfig,
168-
}) async {
169-
if (userPoolConfig == null) return null;
164+
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets(
165+
String username,
166+
AuthOutputs authOutputs,
167+
) async {
168+
if (authOutputs.userPoolClientId == null) return null;
170169
final userPoolStorage = await _getUserPoolStorage();
171-
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
170+
final cognitoUserKeys =
171+
LegacyCognitoUserKeys(authOutputs.userPoolClientId!);
172172
final currentUserId = await userPoolStorage.read(
173173
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
174174
);
175-
if (currentUserId == null) return null;
175+
if (currentUserId == null || authOutputs.userPoolId == null) return null;
176176
final keys = LegacyDeviceSecretKeys(
177177
currentUserId,
178-
userPoolConfig,
178+
authOutputs.userPoolId!,
179179
);
180180
final deviceKey = await userPoolStorage.read(
181181
key: keys[LegacyDeviceSecretKey.id],
@@ -187,7 +187,7 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
187187
key: keys[LegacyDeviceSecretKey.group],
188188
);
189189

190-
final asfKeys = LegacyAsfDeviceKeys(currentUserId, userPoolConfig);
190+
final asfKeys = LegacyAsfDeviceKeys(currentUserId, authOutputs.userPoolId!);
191191
final asfDeviceId = await userPoolStorage.read(
192192
key: asfKeys[LegacyAsfDeviceKey.id],
193193
);
@@ -201,19 +201,20 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
201201
}
202202

203203
@override
204-
Future<void> deleteLegacyDeviceSecrets({
205-
required String username,
206-
CognitoUserPoolConfig? userPoolConfig,
207-
}) async {
208-
if (userPoolConfig == null) return;
204+
Future<void> deleteLegacyDeviceSecrets(
205+
String username,
206+
AuthOutputs authOutputs,
207+
) async {
208+
if (authOutputs.userPoolClientId == null) return;
209209
final userPoolStorage = await _getUserPoolStorage();
210-
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
210+
final cognitoUserKeys =
211+
LegacyCognitoUserKeys(authOutputs.userPoolClientId!);
211212
final currentUserId = await userPoolStorage.read(
212213
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
213214
);
214-
if (currentUserId == null) return;
215-
final keys = LegacyDeviceSecretKeys(currentUserId, userPoolConfig);
216-
final asfKeys = LegacyAsfDeviceKeys(currentUserId, userPoolConfig);
215+
if (currentUserId == null || authOutputs.userPoolId == null) return;
216+
final keys = LegacyDeviceSecretKeys(currentUserId, authOutputs.userPoolId!);
217+
final asfKeys = LegacyAsfDeviceKeys(currentUserId, authOutputs.userPoolId!);
217218
await userPoolStorage.deleteMany([
218219
keys[LegacyDeviceSecretKey.id],
219220
keys[LegacyDeviceSecretKey.secret],

0 commit comments

Comments
 (0)