Replace secure storage with encripted db
This commit is contained in:
parent
7ffba397ce
commit
fb00b5d9ff
@ -16,7 +16,8 @@ import 'package:http/http.dart' as http;
|
|||||||
import 'package:charts_flutter/flutter.dart' as charts;
|
import 'package:charts_flutter/flutter.dart' as charts;
|
||||||
import 'package:flutter_markdown/flutter_markdown.dart';
|
import 'package:flutter_markdown/flutter_markdown.dart';
|
||||||
import 'package:flutter_custom_tabs/flutter_custom_tabs.dart';
|
import 'package:flutter_custom_tabs/flutter_custom_tabs.dart';
|
||||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
import 'package:hive/hive.dart';
|
||||||
|
import 'package:hive_flutter/hive_flutter.dart';
|
||||||
import 'package:device_info/device_info.dart';
|
import 'package:device_info/device_info.dart';
|
||||||
import 'package:in_app_purchase/in_app_purchase.dart';
|
import 'package:in_app_purchase/in_app_purchase.dart';
|
||||||
import 'plugins/dynamic_multi_column_layout.dart';
|
import 'plugins/dynamic_multi_column_layout.dart';
|
||||||
@ -184,7 +185,8 @@ void main() async {
|
|||||||
};
|
};
|
||||||
|
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
AppSettings().loadAppTheme();
|
await AppSettings().loadAppTheme();
|
||||||
|
await Hive.initFlutter();
|
||||||
|
|
||||||
runZoned(() {
|
runZoned(() {
|
||||||
runApp(new HAClientApp(
|
runApp(new HAClientApp(
|
||||||
|
@ -2,6 +2,10 @@ part of '../main.dart';
|
|||||||
|
|
||||||
class AppSettings {
|
class AppSettings {
|
||||||
|
|
||||||
|
static const DEFAULT_HIVE_BOX = 'defaultSettingsBox';
|
||||||
|
|
||||||
|
static const AUTH_TOKEN_KEY = 'llt';
|
||||||
|
|
||||||
static final AppSettings _instance = AppSettings._internal();
|
static final AppSettings _instance = AppSettings._internal();
|
||||||
|
|
||||||
factory AppSettings() {
|
factory AppSettings() {
|
||||||
@ -37,6 +41,7 @@ class AppSettings {
|
|||||||
|
|
||||||
Future load(bool full) async {
|
Future load(bool full) async {
|
||||||
if (full) {
|
if (full) {
|
||||||
|
await Hive.openBox(DEFAULT_HIVE_BOX);
|
||||||
Logger.d('Loading settings...');
|
Logger.d('Loading settings...');
|
||||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
_domain = prefs.getString('hassio-domain');
|
_domain = prefs.getString('hassio-domain');
|
||||||
@ -52,18 +57,11 @@ class AppSettings {
|
|||||||
locationUpdateInterval = Duration(minutes: prefs.getInt("location-interval") ??
|
locationUpdateInterval = Duration(minutes: prefs.getInt("location-interval") ??
|
||||||
defaultLocationUpdateIntervalMinutes);
|
defaultLocationUpdateIntervalMinutes);
|
||||||
locationTrackingEnabled = prefs.getBool("location-enabled") ?? false;
|
locationTrackingEnabled = prefs.getBool("location-enabled") ?? false;
|
||||||
Logger.d('Done. $_domain:$_port');
|
longLivedToken = Hive.box(DEFAULT_HIVE_BOX).get(AUTH_TOKEN_KEY);
|
||||||
try {
|
|
||||||
final storage = new FlutterSecureStorage();
|
|
||||||
longLivedToken = await storage.read(key: "hacl_llt");
|
|
||||||
Logger.d("Long-lived token read successful");
|
|
||||||
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent(
|
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent(
|
||||||
'https://ha-client.app')}&redirect_uri=${Uri
|
'https://ha-client.app')}&redirect_uri=${Uri
|
||||||
.encodeComponent(
|
.encodeComponent(
|
||||||
'https://ha-client.app/service/auth_callback.html')}";
|
'https://ha-client.app/service/auth_callback.html')}";
|
||||||
} catch (e, stacktrace) {
|
|
||||||
Logger.e("Error reading secure storage: $e", stacktrace: stacktrace);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,25 +101,13 @@ class AppSettings {
|
|||||||
Future clearTokens() async {
|
Future clearTokens() async {
|
||||||
longLivedToken = null;
|
longLivedToken = null;
|
||||||
tempToken = null;
|
tempToken = null;
|
||||||
try {
|
Hive.box(DEFAULT_HIVE_BOX).delete(AUTH_TOKEN_KEY);
|
||||||
final storage = new FlutterSecureStorage();
|
|
||||||
await storage.delete(key: "hacl_llt");
|
|
||||||
} catch(e, stacktrace) {
|
|
||||||
Logger.e("Error clearing tokens: $e", stacktrace: stacktrace);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future saveLongLivedToken(token) async {
|
Future saveLongLivedToken(token) async {
|
||||||
longLivedToken = token;
|
longLivedToken = token;
|
||||||
tempToken = null;
|
tempToken = null;
|
||||||
try {
|
Hive.box(DEFAULT_HIVE_BOX).put(AUTH_TOKEN_KEY, longLivedToken);
|
||||||
final storage = new FlutterSecureStorage();
|
|
||||||
await storage.write(key: "hacl_llt", value: "$longLivedToken");
|
|
||||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
||||||
prefs.setBool("oauth-used", true);
|
|
||||||
} catch(e, stacktrace) {
|
|
||||||
Logger.e("Error saving long-lived token: $e", stacktrace: stacktrace);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isNotConfigured() {
|
bool isNotConfigured() {
|
||||||
|
@ -654,6 +654,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
WidgetsBinding.instance.removeObserver(this);
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
|
Hive.close();
|
||||||
//final flutterWebviewPlugin = new FlutterWebviewPlugin();
|
//final flutterWebviewPlugin = new FlutterWebviewPlugin();
|
||||||
//flutterWebviewPlugin.dispose();
|
//flutterWebviewPlugin.dispose();
|
||||||
_viewsTabController?.dispose();
|
_viewsTabController?.dispose();
|
||||||
|
@ -73,11 +73,9 @@ class TokenLoginPopup extends Popup {
|
|||||||
padding: EdgeInsets.all(20),
|
padding: EdgeInsets.all(20),
|
||||||
child: TextFormField(
|
child: TextFormField(
|
||||||
onSaved: (newValue) {
|
onSaved: (newValue) {
|
||||||
final storage = new FlutterSecureStorage();
|
Hive.box(AppSettings.DEFAULT_HIVE_BOX).put(AppSettings.AUTH_TOKEN_KEY, newValue.trim());
|
||||||
storage.write(key: "hacl_llt", value: newValue.trim()).then((_) {
|
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
eventBus.fire(SettingsChangedEvent(true));
|
eventBus.fire(SettingsChangedEvent(true));
|
||||||
});
|
|
||||||
},
|
},
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
hintText: 'Please enter long-lived token',
|
hintText: 'Please enter long-lived token',
|
||||||
|
@ -23,7 +23,8 @@ dependencies:
|
|||||||
flutter_custom_tabs: ^0.6.0
|
flutter_custom_tabs: ^0.6.0
|
||||||
flutter_webview_plugin: ^0.3.10+1
|
flutter_webview_plugin: ^0.3.10+1
|
||||||
webview_flutter: ^0.3.19+7
|
webview_flutter: ^0.3.19+7
|
||||||
flutter_secure_storage: ^3.3.3
|
hive: ^1.4.1+1
|
||||||
|
hive_flutter: ^0.3.0+2
|
||||||
device_info: ^0.4.1+4
|
device_info: ^0.4.1+4
|
||||||
geolocator: ^5.3.1
|
geolocator: ^5.3.1
|
||||||
workmanager: ^0.2.2
|
workmanager: ^0.2.2
|
||||||
|
Reference in New Issue
Block a user