From ee26d745c03be8c8fc8ea5bd17dda1880d5c216a Mon Sep 17 00:00:00 2001 From: NikaHsn Date: Wed, 14 Aug 2024 09:32:05 -0700 Subject: [PATCH] chore(auth): device metadata repository to use AuthOutputs instead of CognitoUserPoolConfig (#5289) --- .../device_metadata_repository.dart | 31 +++++++++++++------ .../state/sign_in_state_machine_test.dart | 2 +- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/auth/amplify_auth_cognito_dart/lib/src/credentials/device_metadata_repository.dart b/packages/auth/amplify_auth_cognito_dart/lib/src/credentials/device_metadata_repository.dart index c3f0d4c253..f3e3521e85 100644 --- a/packages/auth/amplify_auth_cognito_dart/lib/src/credentials/device_metadata_repository.dart +++ b/packages/auth/amplify_auth_cognito_dart/lib/src/credentials/device_metadata_repository.dart @@ -1,10 +1,13 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart'; import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart'; import 'package:amplify_auth_cognito_dart/src/model/cognito_device_secrets.dart'; import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart'; import 'package:amplify_core/amplify_core.dart'; +// ignore: implementation_imports +import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart'; import 'package:amplify_secure_storage_dart/amplify_secure_storage_dart.dart'; /// {@template amplify_auth_cognito_dart.credentials.device_metadata_repository} @@ -13,26 +16,32 @@ import 'package:amplify_secure_storage_dart/amplify_secure_storage_dart.dart'; class DeviceMetadataRepository { /// {@macro amplify_auth_cognito_dart.credentials.device_metadata_repository} const DeviceMetadataRepository( - this._userPoolConfig, + this._authOutputs, this._secureStorage, ); /// {@macro amplify_auth_cognito_dart.credentials.device_metadata_repository} factory DeviceMetadataRepository.fromDependencies( DependencyManager dependencies, - ) => - DeviceMetadataRepository( - dependencies.expect(), - dependencies.getOrCreate(), - ); + ) { + final authOutputs = dependencies.expect(); + if (authOutputs.userPoolClientId == null) { + throw const InvalidAccountTypeException.noUserPool(); + } + return DeviceMetadataRepository( + authOutputs, + dependencies.getOrCreate(), + ); + } - final CognitoUserPoolConfig _userPoolConfig; + final AuthOutputs _authOutputs; final SecureStorageInterface _secureStorage; /// Retrieves the device secrets for [username]. Future get(String username) async { CognitoDeviceSecrets? deviceSecrets; - final deviceKeys = CognitoDeviceKeys(_userPoolConfig.appClientId, username); + final deviceKeys = + CognitoDeviceKeys(_authOutputs.userPoolClientId!, username); final deviceKey = await _secureStorage.read( key: deviceKeys[CognitoDeviceKey.deviceKey], ); @@ -61,7 +70,8 @@ class DeviceMetadataRepository { /// Save the [deviceSecrets] for [username]. Future put(String username, CognitoDeviceSecrets deviceSecrets) async { - final deviceKeys = CognitoDeviceKeys(_userPoolConfig.appClientId, username); + final deviceKeys = + CognitoDeviceKeys(_authOutputs.userPoolClientId!, username); await _secureStorage.write( key: deviceKeys[CognitoDeviceKey.deviceKey], value: deviceSecrets.deviceKey, @@ -82,7 +92,8 @@ class DeviceMetadataRepository { /// Clears the device secrets for [username]. Future remove(String username) async { - final deviceKeys = CognitoDeviceKeys(_userPoolConfig.appClientId, username); + final deviceKeys = + CognitoDeviceKeys(_authOutputs.userPoolClientId!, username); for (final key in deviceKeys) { await _secureStorage.delete(key: key); } diff --git a/packages/auth/amplify_auth_cognito_test/test/state/sign_in_state_machine_test.dart b/packages/auth/amplify_auth_cognito_test/test/state/sign_in_state_machine_test.dart index 40e0f7b715..ebb052091f 100644 --- a/packages/auth/amplify_auth_cognito_test/test/state/sign_in_state_machine_test.dart +++ b/packages/auth/amplify_auth_cognito_test/test/state/sign_in_state_machine_test.dart @@ -186,7 +186,7 @@ void main() { ), completion(isA()), ); - deviceRepo = DeviceMetadataRepository(userPoolConfig, secureStorage); + deviceRepo = DeviceMetadataRepository(mockConfig.auth!, secureStorage); stateMachine.addInstance(deviceRepo); });