Skip to content

chore(auth): legacy credential provider to use AuthOutputs instead of AmplifyConfig types #5303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,53 +23,49 @@ 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 {
if (authOutputs.userPoolId == null) return null;
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
final device = await bridge.fetchLegacyDeviceSecrets(
username,
userPoolConfig.poolId,
authOutputs.userPoolId!,
);
return device?.toLegacyDeviceDetails();
}

@override
Future<void> deleteLegacyDeviceSecrets({
required String username,
CognitoUserPoolConfig? userPoolConfig,
}) async {
if (userPoolConfig != null) {
Future<void> deleteLegacyDeviceSecrets(
String username,
AuthOutputs authOutputs,
) async {
if (authOutputs.userPoolId != null) {
final bridge = _stateMachine.expect<auth_cognito.NativeAuthBridge>();
return bridge.deleteLegacyDeviceSecrets(
username,
userPoolConfig.poolId,
authOutputs.userPoolId!,
);
}
}
Expand Down
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) {
if (authOutputs.userPoolClientId != null) {
final userPoolStorage = await _getUserPoolStorage();
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
final cognitoUserKeys =
LegacyCognitoUserKeys(authOutputs.userPoolClientId!);
final currentUserId = await userPoolStorage.read(
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
);
if (currentUserId != null) {
final userPoolKeys = LegacyCognitoUserPoolKeys(
currentUserId,
userPoolConfig,
authOutputs.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,10 +72,9 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {

String? identityId;
AWSCredentials? awsCredentials;
final identityPoolId = identityPoolConfig?.poolId;
if (identityPoolId != null) {
if (authOutputs.identityPoolId != null) {
final identityPoolStorage = await _getIdentityPoolStorage(
identityPoolId,
authOutputs.identityPoolId!,
);
const identityPoolKeys = LegacyCognitoIdentityPoolKeys();
identityId = await identityPoolStorage.read(
Expand Down Expand Up @@ -122,21 +122,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 {
if (authOutputs.userPoolClientId != null) {
final userPoolStorage = await _getUserPoolStorage();
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
final cognitoUserKeys =
LegacyCognitoUserKeys(authOutputs.userPoolClientId!);
final currentUser = await userPoolStorage.read(
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
);
if (currentUser != null) {
final userPoolKeys = LegacyCognitoUserPoolKeys(
currentUser,
userPoolConfig,
authOutputs.userPoolClientId!,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See suggestion below about remove force casts

);
await userPoolStorage.deleteMany([
userPoolKeys[LegacyCognitoUserPoolKey.accessToken],
Expand All @@ -147,9 +146,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 +161,21 @@ 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 {
if (authOutputs.userPoolClientId == null) return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Remove the force casts. If you create variables for userPoolId and userPoolClientId and return early if they are null, then Dart will not require the force case.

    final userPoolId = authOutputs.userPoolId;
    final userPoolClientId = authOutputs.userPoolClientId;
    if (userPoolId == null || userPoolClientId == null) return null;
    final userPoolStorage = await _getUserPoolStorage();
    final cognitoUserKeys = LegacyCognitoUserKeys(userPoolClientId);
    final currentUserId = await userPoolStorage.read(
      key: cognitoUserKeys[LegacyCognitoKey.currentUser],
    );
    if (currentUserId == null) return null;
    final keys = LegacyDeviceSecretKeys(currentUserId, userPoolId);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies to several other locations in the PR.

final userPoolStorage = await _getUserPoolStorage();
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
final cognitoUserKeys =
LegacyCognitoUserKeys(authOutputs.userPoolClientId!);
final currentUserId = await userPoolStorage.read(
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
);
if (currentUserId == null) return null;
if (currentUserId == null || authOutputs.userPoolId == null) return null;
final keys = LegacyDeviceSecretKeys(
currentUserId,
userPoolConfig,
authOutputs.userPoolId!,
);
final deviceKey = await userPoolStorage.read(
key: keys[LegacyDeviceSecretKey.id],
Expand All @@ -187,7 +187,7 @@ class LegacyCredentialProviderIOS implements LegacyCredentialProvider {
key: keys[LegacyDeviceSecretKey.group],
);

final asfKeys = LegacyAsfDeviceKeys(currentUserId, userPoolConfig);
final asfKeys = LegacyAsfDeviceKeys(currentUserId, authOutputs.userPoolId!);
final asfDeviceId = await userPoolStorage.read(
key: asfKeys[LegacyAsfDeviceKey.id],
);
Expand All @@ -201,19 +201,20 @@ 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 {
if (authOutputs.userPoolClientId == null) return;
final userPoolStorage = await _getUserPoolStorage();
final cognitoUserKeys = LegacyCognitoUserKeys(userPoolConfig);
final cognitoUserKeys =
LegacyCognitoUserKeys(authOutputs.userPoolClientId!);
final currentUserId = await userPoolStorage.read(
key: cognitoUserKeys[LegacyCognitoKey.currentUser],
);
if (currentUserId == null) return;
final keys = LegacyDeviceSecretKeys(currentUserId, userPoolConfig);
final asfKeys = LegacyAsfDeviceKeys(currentUserId, userPoolConfig);
if (currentUserId == null || authOutputs.userPoolId == null) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as above. Remove the force cast by creating a userPoolId var.

final keys = LegacyDeviceSecretKeys(currentUserId, authOutputs.userPoolId!);
final asfKeys = LegacyAsfDeviceKeys(currentUserId, authOutputs.userPoolId!);
await userPoolStorage.deleteMany([
keys[LegacyDeviceSecretKey.id],
keys[LegacyDeviceSecretKey.secret],
Expand Down
Loading
Loading