Skip to content
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

chore(auth): device metadata repository to use AuthOutputs instead of CognitoUserPoolConfig #5289

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -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}
Expand All @@ -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<AuthOutputs>();
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<CognitoDeviceSecrets?> 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],
);
Expand Down Expand Up @@ -61,7 +70,8 @@ class DeviceMetadataRepository {

/// Save the [deviceSecrets] for [username].
Future<void> 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,
Expand All @@ -82,7 +92,8 @@ class DeviceMetadataRepository {

/// Clears the device secrets for [username].
Future<void> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void main() {
),
completion(isA<Configured>()),
);
deviceRepo = DeviceMetadataRepository(userPoolConfig, secureStorage);
deviceRepo = DeviceMetadataRepository(mockConfig.auth!, secureStorage);
stateMachine.addInstance<DeviceMetadataRepository>(deviceRepo);
});

Expand Down
Loading