use secure storage for password

secure_password_storage
Felix Bruns 10 months ago
parent 1b2850c41d
commit b5a89c3cc4

@ -17,7 +17,7 @@ class SettingsController with ChangeNotifier {
_themeMode = await _settingsService.themeMode();
_hostname = await _settingsService.keyStr("hostname");
_username = await _settingsService.keyStr("username");
_password = await _settingsService.keyStr("password");
_password = await _settingsService.keyStrSec("password");
// Important! Inform listeners a change has occurred.
notifyListeners();
}
@ -35,6 +35,10 @@ class SettingsController with ChangeNotifier {
notifyListeners();
await _settingsService.setKeyStr(key, value);
}
Future<void> _setKeySec(String key, String value) async {
notifyListeners();
await _settingsService.setKeyStrSec(key, value);
}
Future<void> setHostname(String newHostname) async {
_hostname = newHostname;
@ -48,24 +52,16 @@ class SettingsController with ChangeNotifier {
}
Future<void> setPassword(String newPassword) async {
_password = newPassword;
await _setKey("password", password);
await _setKeySec("password", password);
Log().debug("password update");
}
/// Update and persist the ThemeMode based on the user's selection.
Future<void> updateThemeMode(ThemeMode? newThemeMode) async {
if (newThemeMode == null) return;
// Do not perform any work if new and old ThemeMode are identical
if (newThemeMode == _themeMode) return;
// Otherwise, store the new ThemeMode in memory
_themeMode = newThemeMode;
// Important! Inform listeners a change has occurred.
notifyListeners();
// Persist the changes to a local database or the internet using the
// SettingService.
await _settingsService.updateThemeMode(newThemeMode);
}
}

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
/// A service that stores and retrieves user settings.
///
@ -7,6 +8,7 @@ import 'package:shared_preferences/shared_preferences.dart';
/// persist the user settings locally, use the shared_preferences package. If
/// you'd like to store settings on a web server, use the http package.
class SettingsService {
/// Loads the User's preferred ThemeMode from local or remote storage.
Future<ThemeMode> themeMode() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
@ -32,4 +34,16 @@ class SettingsService {
final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString(key, value);
}
Future<String> keyStrSec(String key) async {
const storage = FlutterSecureStorage();
String? res = await storage.read(key: key);
if (res == null) return "";
return res;
}
Future<void> setKeyStrSec(String key, String value) async {
const storage = FlutterSecureStorage();
await storage.write(key: key, value: value);
}
}

@ -6,6 +6,10 @@
#include "generated_plugin_registrant.h"
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
}

@ -3,6 +3,7 @@
#
list(APPEND FLUTTER_PLUGIN_LIST
flutter_secure_storage_linux
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST

@ -18,6 +18,7 @@ dependencies:
shared_preferences:
async: ^2.11.0
logger: ^2.2.0
flutter_secure_storage: ^9.2.1
flutter_gen: any
dev_dependencies:

Loading…
Cancel
Save