import 'dart:convert'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import '../types/abstracts.dart'; import '../types/connection_config.dart'; import '../utils/crypto.dart' as crypto; class SecureStorageConfig implements ConfigRepo { static const _keyPrivateKey = "private_key"; static const _keyConnectionConfig = "connection_config"; static const _keyPassword = "password"; final _storage = FlutterSecureStorage(); bool _passwordMatched = false; String _password; String get password { _checkIfPasswordMatched(); return _password; } Future setPrivateKey(String key) { _checkIfPasswordMatched(); return _storage.write(key: _keyPrivateKey, value: key.replaceAll('-', '')); } Future get privateKey { _checkIfPasswordMatched(); return _storage.read(key: _keyPrivateKey); } Future setPassword(String password) { _checkIfPasswordMatched(); _password = password; return _storage.write( key: _keyPassword, value: crypto.hashPassword(password)); } Future get passwordIsSet async { final passHash = await _storage.read(key: _keyPassword); if (passHash != null) { return true; } _passwordMatched = true; return false; } Future matchesPasswordHash(String password) async { _passwordMatched = crypto.matchHashedPassword( await _storage.read(key: _keyPassword), password); if (_passwordMatched) _password = password; return _passwordMatched; } Future setConnectionConfig(ConnectionConfig config) { _checkIfPasswordMatched(); return _storage.write( key: _keyConnectionConfig, value: json.encode(config)); } Future get connectionConfig async { _checkIfPasswordMatched(); final connConfig = await _storage.read(key: _keyConnectionConfig); if (connConfig == null) { return null; } return ConnectionConfig.fromJson(json.decode(connConfig)); } Future deleteAll() { _checkIfPasswordMatched(); return _storage.deleteAll(); } void _checkIfPasswordMatched() { if (_passwordMatched) return; throw Exception('password not matched yet'); } }