From 87596bdaeed43185ad20bea051dc42a442b293b5 Mon Sep 17 00:00:00 2001 From: Damian Molinski Date: Mon, 23 Sep 2024 13:31:42 +0200 Subject: [PATCH] feat: dummy auth storage which encapsulates keys --- .../lib/dependency/dependencies.dart | 5 +- .../src/credentials_storage_repository.dart | 24 +++------- .../lib/src/catalyst_voices_services.dart | 2 +- .../lib/src/storage/dummy_auth_storage.dart | 47 +++++++++++++++++++ .../lib/src/storage/secure_storage.dart | 4 +- .../storage/secure_storage_keys_const.dart | 4 -- .../storage/vault/secure_storage_vault.dart | 4 +- 7 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 catalyst_voices/packages/catalyst_voices_services/lib/src/storage/dummy_auth_storage.dart delete mode 100644 catalyst_voices/packages/catalyst_voices_services/lib/src/storage/secure_storage_keys_const.dart diff --git a/catalyst_voices/lib/dependency/dependencies.dart b/catalyst_voices/lib/dependency/dependencies.dart index 670f1404aa..3c9a6b4ba0 100644 --- a/catalyst_voices/lib/dependency/dependencies.dart +++ b/catalyst_voices/lib/dependency/dependencies.dart @@ -41,7 +41,8 @@ final class Dependencies extends DependencyProvider { } void _registerServices() { - registerSingleton(SecureStorage()); - registerSingleton(SecureStorageVault()); + registerSingleton(const SecureStorage()); + registerSingleton(const SecureStorageVault()); + registerSingleton(const SecureDummyAuthStorage()); } } diff --git a/catalyst_voices/packages/catalyst_voices_repositories/lib/src/credentials_storage_repository.dart b/catalyst_voices/packages/catalyst_voices_repositories/lib/src/credentials_storage_repository.dart index ab836eda17..25beccc74b 100644 --- a/catalyst_voices/packages/catalyst_voices_repositories/lib/src/credentials_storage_repository.dart +++ b/catalyst_voices/packages/catalyst_voices_repositories/lib/src/credentials_storage_repository.dart @@ -9,23 +9,18 @@ import 'package:result_type/result_type.dart'; /// It will be replaced by a proper implementation as soon as authentication /// flow will be defined. final class CredentialsStorageRepository { - final Storage _storage; + final DummyAuthStorage _storage; const CredentialsStorageRepository({ - required Storage storage, + required DummyAuthStorage storage, }) : _storage = storage; Future get clearSessionData async => _storage.clear(); Future> getSessionData() async { try { - final email = await _storage.readString( - key: SecureStorageKeysConst.dummyEmail, - ); - - final password = await _storage.readString( - key: SecureStorageKeysConst.dummyPassword, - ); + final email = await _storage.readEmail(); + final password = await _storage.readPassword(); if (email == null || password == null) { return Success(null); @@ -46,15 +41,8 @@ final class CredentialsStorageRepository { SessionData sessionData, ) async { try { - await _storage.writeString( - sessionData.email, - key: SecureStorageKeysConst.dummyEmail, - ); - - await _storage.writeString( - sessionData.password, - key: SecureStorageKeysConst.dummyPassword, - ); + await _storage.writeEmail(sessionData.email); + await _storage.writePassword(sessionData.password); return Success(null); } on SecureStorageError catch (_) { return Failure(SecureStorageError.canNotSaveData); diff --git a/catalyst_voices/packages/catalyst_voices_services/lib/src/catalyst_voices_services.dart b/catalyst_voices/packages/catalyst_voices_services/lib/src/catalyst_voices_services.dart index 2602036d42..c86d5ab5db 100644 --- a/catalyst_voices/packages/catalyst_voices_services/lib/src/catalyst_voices_services.dart +++ b/catalyst_voices/packages/catalyst_voices_services/lib/src/catalyst_voices_services.dart @@ -1,5 +1,5 @@ +export 'storage/dummy_auth_storage.dart'; export 'storage/secure_storage.dart'; -export 'storage/secure_storage_keys_const.dart'; export 'storage/storage.dart'; export 'storage/vault/lock_factor.dart'; export 'storage/vault/lock_factor_codec.dart' show LockFactorCodec; diff --git a/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/dummy_auth_storage.dart b/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/dummy_auth_storage.dart new file mode 100644 index 0000000000..74a5674591 --- /dev/null +++ b/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/dummy_auth_storage.dart @@ -0,0 +1,47 @@ +import 'dart:async'; + +import 'package:catalyst_voices_services/src/storage/secure_storage.dart'; + +abstract interface class DummyAuthStorage { + FutureOr readEmail(); + + FutureOr writeEmail(String? value); + + FutureOr readPassword(); + + FutureOr writePassword(String? value); + + Future clear(); +} + +final class SecureDummyAuthStorage extends SecureStorage + implements DummyAuthStorage { + static const _emailKey = 'email'; + static const _passwordKey = 'password'; + + const SecureDummyAuthStorage({ + super.secureStorage, + }); + + @override + FutureOr readEmail() => readString(key: _emailKey); + + @override + FutureOr writeEmail(String? value) { + return writeString(value, key: _emailKey); + } + + @override + FutureOr readPassword() => readString(key: _passwordKey); + + @override + FutureOr writePassword(String? value) { + return writeString(value, key: _passwordKey); + } + + @override + Future clear() async { + await writeEmail(null); + await writePassword(null); + } +} diff --git a/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/secure_storage.dart b/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/secure_storage.dart index 56225409b5..ca9d3aebbd 100644 --- a/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/secure_storage.dart +++ b/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/secure_storage.dart @@ -6,10 +6,10 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart'; const _keyPrefix = 'SecureStorage'; -final class SecureStorage with StorageAsStringMixin implements Storage { +base class SecureStorage with StorageAsStringMixin implements Storage { final FlutterSecureStorage _secureStorage; - SecureStorage({ + const SecureStorage({ FlutterSecureStorage secureStorage = const FlutterSecureStorage(), }) : _secureStorage = secureStorage; diff --git a/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/secure_storage_keys_const.dart b/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/secure_storage_keys_const.dart deleted file mode 100644 index d51966cd61..0000000000 --- a/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/secure_storage_keys_const.dart +++ /dev/null @@ -1,4 +0,0 @@ -abstract final class SecureStorageKeysConst { - static const dummyEmail = 'email'; - static const dummyPassword = 'password'; -} diff --git a/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/vault/secure_storage_vault.dart b/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/vault/secure_storage_vault.dart index 8817e6e9b5..024b3ebac0 100644 --- a/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/vault/secure_storage_vault.dart +++ b/catalyst_voices/packages/catalyst_voices_services/lib/src/storage/vault/secure_storage_vault.dart @@ -13,11 +13,11 @@ const _unlockKey = 'UnlockFactorKey'; // TODO(damian-molinski): Maybe we'll need to encrypt data with LockFactor /// Implementation of [Vault] that uses [FlutterSecureStorage] as /// facade for read/write operations. -final class SecureStorageVault with StorageAsStringMixin implements Vault { +base class SecureStorageVault with StorageAsStringMixin implements Vault { final FlutterSecureStorage _secureStorage; final LockFactorCodec _lockCodec; - SecureStorageVault({ + const SecureStorageVault({ FlutterSecureStorage secureStorage = const FlutterSecureStorage(), LockFactorCodec lockCodec = const DefaultLockFactorCodec(), }) : _secureStorage = secureStorage,