Skip to content

Commit

Permalink
chore(auth): legacy credential provider to use AuthOutputs instead of…
Browse files Browse the repository at this point in the history
… AmplifyConfig types (#5303)
  • Loading branch information
NikaHsn authored Sep 16, 2024
1 parent 8deabcf commit 51d01c4
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import 'package:amplify_auth_cognito/src/native_auth_plugin.g.dart'
import 'package:amplify_auth_cognito_dart/src/credentials/legacy_credential_provider.dart';
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
import 'package:amplify_core/src/config/auth/cognito/credentials_provider.dart';
import 'package:amplify_core/src/config/auth/cognito/oauth.dart';
import 'package:amplify_core/src/config/auth/cognito/user_pool.dart';
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';

/// {@template amplify_auth_cognito.legacy_android_credential_provider}
/// The implementation of [LegacyCredentialProvider] for migrating
Expand All @@ -25,54 +23,51 @@ class LegacyCredentialProviderAndroid implements LegacyCredentialProvider {
final CognitoAuthStateMachine _stateMachine;

@override
Future<CredentialStoreData?> fetchLegacyCredentials({
CognitoUserPoolConfig? userPoolConfig,
CognitoIdentityCredentialsProvider? identityPoolConfig,
CognitoOAuthConfig? hostedUiConfig,
}) async {
Future<CredentialStoreData?> fetchLegacyCredentials(
AuthOutputs authOutputs,
) async {
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
final legacyCredentials = await bridge.getLegacyCredentials(
identityPoolConfig?.poolId,
userPoolConfig?.appClientId,
authOutputs.identityPoolId,
authOutputs.userPoolClientId,
);
return legacyCredentials.toCredentialStoreData();
}

@override
Future<void> deleteLegacyCredentials({
CognitoUserPoolConfig? userPoolConfig,
CognitoIdentityCredentialsProvider? identityPoolConfig,
CognitoOAuthConfig? hostedUiConfig,
}) {
Future<void> deleteLegacyCredentials(
AuthOutputs authOutputs,
) {
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
return bridge.clearLegacyCredentials();
}

@override
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets({
required String username,
CognitoUserPoolConfig? userPoolConfig,
}) async {
if (userPoolConfig == null) return null;
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets(
String username,
AuthOutputs authOutputs,
) async {
final userPoolId = authOutputs.userPoolId;
if (userPoolId == null) return null;
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
final device = await bridge.fetchLegacyDeviceSecrets(
username,
userPoolConfig.poolId,
userPoolId,
);
return device?.toLegacyDeviceDetails();
}

@override
Future<void> deleteLegacyDeviceSecrets({
required String username,
CognitoUserPoolConfig? userPoolConfig,
}) async {
if (userPoolConfig != null) {
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
return bridge.deleteLegacyDeviceSecrets(
username,
userPoolConfig.poolId,
);
}
Future<void> deleteLegacyDeviceSecrets(
String username,
AuthOutputs authOutputs,
) async {
final userPoolId = authOutputs.userPoolId;
if (userPoolId == null) return;
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
return bridge.deleteLegacyDeviceSecrets(
username,
userPoolId,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
// ignore: implementation_imports, invalid_use_of_internal_member
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
import 'package:amplify_core/amplify_core.dart';
// ignore: implementation_imports
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';

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

@override
Future<CredentialStoreData?> fetchLegacyCredentials({
CognitoUserPoolConfig? userPoolConfig,
CognitoIdentityCredentialsProvider? identityPoolConfig,
CognitoOAuthConfig? hostedUiConfig,
}) async {
Future<CredentialStoreData?> fetchLegacyCredentials(
AuthOutputs authOutputs,
) async {
if (_instance == null) return null;
return _instance.fetchLegacyCredentials(
userPoolConfig: userPoolConfig,
identityPoolConfig: identityPoolConfig,
hostedUiConfig: hostedUiConfig,
authOutputs,
);
}

@override
Future<void> deleteLegacyCredentials({
CognitoUserPoolConfig? userPoolConfig,
CognitoIdentityCredentialsProvider? identityPoolConfig,
CognitoOAuthConfig? hostedUiConfig,
}) async {
Future<void> deleteLegacyCredentials(
AuthOutputs authOutputs,
) async {
if (_instance == null) return;
return _instance.deleteLegacyCredentials(
userPoolConfig: userPoolConfig,
identityPoolConfig: identityPoolConfig,
hostedUiConfig: hostedUiConfig,
authOutputs,
);
}

@override
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets({
required String username,
CognitoUserPoolConfig? userPoolConfig,
}) async {
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets(
String username,
AuthOutputs authOutputs,
) async {
if (_instance == null) return null;
return _instance.fetchLegacyDeviceSecrets(
username: username,
userPoolConfig: userPoolConfig,
username,
authOutputs,
);
}

@override
Future<void> deleteLegacyDeviceSecrets({
required String username,
CognitoUserPoolConfig? userPoolConfig,
}) async {
Future<void> deleteLegacyDeviceSecrets(
String username,
AuthOutputs authOutputs,
) async {
if (_instance == null) return;
return _instance.deleteLegacyDeviceSecrets(
username: username,
userPoolConfig: userPoolConfig,
username,
authOutputs,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import 'package:amplify_auth_cognito_dart/src/credentials/legacy_credential_prov
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
// ignore: implementation_imports, invalid_use_of_internal_member
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
// ignore: implementation_imports
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:async/async.dart';

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

@override
Future<CredentialStoreData?> fetchLegacyCredentials({
CognitoUserPoolConfig? userPoolConfig,
CognitoIdentityCredentialsProvider? identityPoolConfig,
CognitoOAuthConfig? hostedUiConfig,
}) async {
Future<CredentialStoreData?> fetchLegacyCredentials(
AuthOutputs authOutputs,
) async {
CognitoUserPoolTokens? userPoolTokens;
if (userPoolConfig != null) {
final userPoolClientId = authOutputs.userPoolClientId;
if (userPoolClientId != null) {
final userPoolStorage = await _getUserPoolStorage();
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolClientId);
final currentUserId = await userPoolStorage.read(
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
);
if (currentUserId != null) {
final userPoolKeys = LegacyCognitoUserPoolKeys(
currentUserId,
userPoolConfig,
userPoolClientId,
);
final accessToken = await userPoolStorage.read(
key: userPoolKeys[LegacyCognitoUserPoolKey.accessToken],
Expand All @@ -56,7 +57,7 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
);
if (accessToken != null && refreshToken != null && idToken != null) {
// TODO(Jordan-Nelson): fetch sign in method from keychain on iOS
final signInMethod = hostedUiConfig != null
final signInMethod = authOutputs.oauth != null
? CognitoSignInMethod.hostedUi
: CognitoSignInMethod.default$;
userPoolTokens = CognitoUserPoolTokens(
Expand All @@ -71,7 +72,7 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {

String? identityId;
AWSCredentials? awsCredentials;
final identityPoolId = identityPoolConfig?.poolId;
final identityPoolId = authOutputs.identityPoolId;
if (identityPoolId != null) {
final identityPoolStorage = await _getIdentityPoolStorage(
identityPoolId,
Expand Down Expand Up @@ -122,21 +123,20 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
}

@override
Future<void> deleteLegacyCredentials({
CognitoUserPoolConfig? userPoolConfig,
CognitoIdentityCredentialsProvider? identityPoolConfig,
CognitoOAuthConfig? hostedUiConfig,
}) async {
if (userPoolConfig != null) {
Future<void> deleteLegacyCredentials(
AuthOutputs authOutputs,
) async {
final userPoolClientId = authOutputs.userPoolClientId;
if (userPoolClientId != null) {
final userPoolStorage = await _getUserPoolStorage();
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolClientId);
final currentUser = await userPoolStorage.read(
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
);
if (currentUser != null) {
final userPoolKeys = LegacyCognitoUserPoolKeys(
currentUser,
userPoolConfig,
userPoolClientId,
);
await userPoolStorage.deleteMany([
userPoolKeys[LegacyCognitoUserPoolKey.accessToken],
Expand All @@ -147,9 +147,9 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
}
}

final identityPoolId = identityPoolConfig?.poolId;
if (identityPoolId != null) {
final identityPoolStorage = await _getIdentityPoolStorage(identityPoolId);
if (authOutputs.identityPoolId != null) {
final identityPoolStorage =
await _getIdentityPoolStorage(authOutputs.identityPoolId!);
const identityPoolKeys = LegacyCognitoIdentityPoolKeys();
await identityPoolStorage.deleteMany([
identityPoolKeys[LegacyCognitoIdentityPoolKey.identityId],
Expand All @@ -162,20 +162,22 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
}

@override
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets({
required String username,
CognitoUserPoolConfig? userPoolConfig,
}) async {
if (userPoolConfig == null) return null;
Future<LegacyDeviceDetails?> fetchLegacyDeviceSecrets(
String username,
AuthOutputs authOutputs,
) async {
final userPoolClientId = authOutputs.userPoolClientId;
if (userPoolClientId == null) return null;
final userPoolStorage = await _getUserPoolStorage();
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolClientId);
final currentUserId = await userPoolStorage.read(
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
);
if (currentUserId == null) return null;
final userPoolId = authOutputs.userPoolId;
if (currentUserId == null || userPoolId == null) return null;
final keys = LegacyDeviceSecretKeys(
currentUserId,
userPoolConfig,
userPoolId,
);
final deviceKey = await userPoolStorage.read(
key: keys[LegacyDeviceSecretKey.id],
Expand All @@ -187,7 +189,7 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
key: keys[LegacyDeviceSecretKey.group],
);

final asfKeys = LegacyAsfDeviceKeys(currentUserId, userPoolConfig);
final asfKeys = LegacyAsfDeviceKeys(currentUserId, userPoolId);
final asfDeviceId = await userPoolStorage.read(
key: asfKeys[LegacyAsfDeviceKey.id],
);
Expand All @@ -201,19 +203,21 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
}

@override
Future<void> deleteLegacyDeviceSecrets({
required String username,
CognitoUserPoolConfig? userPoolConfig,
}) async {
if (userPoolConfig == null) return;
Future<void> deleteLegacyDeviceSecrets(
String username,
AuthOutputs authOutputs,
) async {
final userPoolClientId = authOutputs.userPoolClientId;
if (userPoolClientId == null) return;
final userPoolStorage = await _getUserPoolStorage();
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolClientId);
final currentUserId = await userPoolStorage.read(
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
);
if (currentUserId == null) return;
final keys = LegacyDeviceSecretKeys(currentUserId, userPoolConfig);
final asfKeys = LegacyAsfDeviceKeys(currentUserId, userPoolConfig);
final userPoolId = authOutputs.userPoolId;
if (currentUserId == null || userPoolId == null) return;
final keys = LegacyDeviceSecretKeys(currentUserId, userPoolId);
final asfKeys = LegacyAsfDeviceKeys(currentUserId, userPoolId);
await userPoolStorage.deleteMany([
keys[LegacyDeviceSecretKey.id],
keys[LegacyDeviceSecretKey.secret],
Expand Down
Loading

0 comments on commit 51d01c4

Please sign in to comment.