Integrate secure storage service
This commit is contained in:
@@ -2,8 +2,10 @@ library;
|
||||
|
||||
import 'package:fluttery/logger/logger.dart';
|
||||
import 'package:fluttery/preferences/preferences.dart';
|
||||
import 'package:fluttery/secure_storage.dart';
|
||||
import 'package:fluttery/src/logger/logger_impl.dart';
|
||||
import 'package:fluttery/src/preferences/preferences_impl.dart';
|
||||
import 'package:fluttery/src/storage/secure/secure_storage_impl.dart';
|
||||
import 'package:kiwi/kiwi.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
@@ -34,6 +36,8 @@ class App {
|
||||
SharedPreferences.getInstance().then((instance) {
|
||||
registerService<Preferences>(() => PreferencesImpl(instance: instance));
|
||||
});
|
||||
|
||||
registerService<SecureStorage>(() => SecureStorageImpl());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
72
finlog_app/fluttery/lib/secure_storage.dart
Normal file
72
finlog_app/fluttery/lib/secure_storage.dart
Normal file
@@ -0,0 +1,72 @@
|
||||
import 'package:fluttery/fluttery.dart';
|
||||
|
||||
/// Interface for secure storage operations.
|
||||
///
|
||||
/// Provides methods for securely storing and retrieving sensitive data
|
||||
/// like passwords, tokens, API keys, etc. Data stored through this interface
|
||||
/// is encrypted and stored in the device's secure storage (Keychain on iOS,
|
||||
/// Keystore on Android).
|
||||
abstract class SecureStorage implements Service {
|
||||
/// Stores a string value securely with the given [key].
|
||||
///
|
||||
/// Returns a Future that completes when the value is successfully stored.
|
||||
/// Throws an exception if the storage operation fails.
|
||||
Future<void> write(String key, String value);
|
||||
|
||||
/// Retrieves the securely stored string value for the given [key].
|
||||
///
|
||||
/// Returns the stored value if found, null otherwise.
|
||||
/// Throws an exception if the retrieval operation fails.
|
||||
Future<String?> read(String key);
|
||||
|
||||
/// Stores an integer value securely with the given [key].
|
||||
///
|
||||
/// The integer is converted to a string for storage.
|
||||
Future<void> writeInt(String key, int value);
|
||||
|
||||
/// Retrieves the securely stored integer value for the given [key].
|
||||
///
|
||||
/// Returns the stored integer if found and valid, null otherwise.
|
||||
Future<int?> readInt(String key);
|
||||
|
||||
/// Stores a boolean value securely with the given [key].
|
||||
///
|
||||
/// The boolean is converted to a string for storage.
|
||||
Future<void> writeBool(String key, bool value);
|
||||
|
||||
/// Retrieves the securely stored boolean value for the given [key].
|
||||
///
|
||||
/// Returns the stored boolean if found and valid, null otherwise.
|
||||
Future<bool?> readBool(String key);
|
||||
|
||||
/// Stores a double value securely with the given [key].
|
||||
///
|
||||
/// The double is converted to a string for storage.
|
||||
Future<void> writeDouble(String key, double value);
|
||||
|
||||
/// Retrieves the securely stored double value for the given [key].
|
||||
///
|
||||
/// Returns the stored double if found and valid, null otherwise.
|
||||
Future<double?> readDouble(String key);
|
||||
|
||||
/// Removes the securely stored value for the given [key].
|
||||
///
|
||||
/// Returns a Future that completes when the value is successfully removed.
|
||||
Future<void> delete(String key);
|
||||
|
||||
/// Removes all securely stored values.
|
||||
///
|
||||
/// Returns a Future that completes when all values are successfully removed.
|
||||
/// Use with caution as this operation cannot be undone.
|
||||
Future<void> deleteAll();
|
||||
|
||||
/// Returns all keys currently stored in secure storage.
|
||||
///
|
||||
/// Useful for debugging or migration purposes.
|
||||
Future<Set<String>> readAllKeys();
|
||||
|
||||
/// Checks if a value exists for the given [key].
|
||||
///
|
||||
/// Returns true if a value exists, false otherwise.
|
||||
Future<bool> containsKey(String key);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:fluttery/secure_storage.dart';
|
||||
|
||||
class SecureStorageImpl implements SecureStorage {
|
||||
final FlutterSecureStorage _secureStorage;
|
||||
|
||||
/// Constructor - creates a single instance with default FlutterSecureStorage
|
||||
SecureStorageImpl() : _secureStorage = const FlutterSecureStorage();
|
||||
|
||||
/// Testing constructor
|
||||
@visibleForTesting
|
||||
SecureStorageImpl.forTesting({required FlutterSecureStorage instance})
|
||||
: _secureStorage = instance;
|
||||
|
||||
@override
|
||||
Future<void> write(String key, String value) async {
|
||||
await _secureStorage.write(key: key, value: value);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> read(String key) async {
|
||||
return await _secureStorage.read(key: key);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> containsKey(String key) async {
|
||||
return await _secureStorage.containsKey(key: key);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> delete(String key) async {
|
||||
await _secureStorage.delete(key: key);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteAll() async {
|
||||
await _secureStorage.deleteAll();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Set<String>> readAll() async {
|
||||
final allData = await _secureStorage.readAll();
|
||||
return allData.keys.toSet();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Set<String>> readAllKeys() async {
|
||||
final allData = await _secureStorage.readAll();
|
||||
return allData.keys.toSet();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> writeInt(String key, int value) async {
|
||||
await _secureStorage.write(key: key, value: value.toString());
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int?> readInt(String key) async {
|
||||
final value = await _secureStorage.read(key: key);
|
||||
if (value == null) return null;
|
||||
return int.tryParse(value);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> writeBool(String key, bool value) async {
|
||||
await _secureStorage.write(key: key, value: value.toString());
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool?> readBool(String key) async {
|
||||
final value = await _secureStorage.read(key: key);
|
||||
if (value == null) return null;
|
||||
if (value.toLowerCase() == 'true') return true;
|
||||
if (value.toLowerCase() == 'false') return false;
|
||||
return null; // Invalid boolean value
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> writeDouble(String key, double value) async {
|
||||
await _secureStorage.write(key: key, value: value.toString());
|
||||
}
|
||||
|
||||
@override
|
||||
Future<double?> readDouble(String key) async {
|
||||
final value = await _secureStorage.read(key: key);
|
||||
if (value == null) return null;
|
||||
return double.tryParse(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user