Skip to content

Commit

Permalink
feat: dummy auth storage which encapsulates keys
Browse files Browse the repository at this point in the history
  • Loading branch information
damian-molinski committed Sep 23, 2024
1 parent 5ed91cb commit 87596bd
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 deletions.
5 changes: 3 additions & 2 deletions catalyst_voices/lib/dependency/dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ final class Dependencies extends DependencyProvider {
}

void _registerServices() {
registerSingleton<Storage>(SecureStorage());
registerSingleton<Vault>(SecureStorageVault());
registerSingleton<Storage>(const SecureStorage());
registerSingleton<Vault>(const SecureStorageVault());
registerSingleton<DummyAuthStorage>(const SecureDummyAuthStorage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> get clearSessionData async => _storage.clear();

Future<Result<SessionData?, SecureStorageError>> 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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'dart:async';

import 'package:catalyst_voices_services/src/storage/secure_storage.dart';

abstract interface class DummyAuthStorage {
FutureOr<String?> readEmail();

FutureOr<void> writeEmail(String? value);

FutureOr<String?> readPassword();

FutureOr<void> writePassword(String? value);

Future<void> clear();
}

final class SecureDummyAuthStorage extends SecureStorage
implements DummyAuthStorage {
static const _emailKey = 'email';
static const _passwordKey = 'password';

const SecureDummyAuthStorage({
super.secureStorage,
});

@override
FutureOr<String?> readEmail() => readString(key: _emailKey);

@override
FutureOr<void> writeEmail(String? value) {
return writeString(value, key: _emailKey);
}

@override
FutureOr<String?> readPassword() => readString(key: _passwordKey);

@override
FutureOr<void> writePassword(String? value) {
return writeString(value, key: _passwordKey);
}

@override
Future<void> clear() async {
await writeEmail(null);
await writePassword(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 87596bd

Please sign in to comment.