diff --git a/editing/lib/common/exceptions.dart b/editing/lib/common/exceptions.dart new file mode 100644 index 0000000..90b960d --- /dev/null +++ b/editing/lib/common/exceptions.dart @@ -0,0 +1 @@ +class IncorrectPasswordException implements Exception {} diff --git a/editing/lib/service/app_service.dart b/editing/lib/service/app_service.dart index 4882dc0..3a1ca93 100644 --- a/editing/lib/service/app_service.dart +++ b/editing/lib/service/app_service.dart @@ -1,10 +1,12 @@ import 'dart:io'; +import 'package:editing/config/meta.dart'; import 'package:editing/database/database.dart'; import 'package:editing/service/kdbx_service.dart'; import 'package:kdbx/kdbx.dart'; import 'package:logger/logger.dart'; +import '../common/exceptions.dart'; import '../common/path_util.dart'; class AppService { @@ -25,14 +27,15 @@ class AppService { return config.existsSync(); } - Future verifyPassword(ProtectedValue password) async { + Future login(ProtectedValue password) async { final File config = await getAppConfigFile(); final data = await config.readAsBytes(); try { - final file = await kdbxFormat.read(data, Credentials(password)); - return true; + final kdbx = await kdbxFormat.read(data, Credentials(password)); + return _kdbxService.loadMeta(kdbx); } on KdbxInvalidKeyException catch (e) { - return false; + logger.e("Login failed, password incorrect", error: e); + throw IncorrectPasswordException(); } } diff --git a/editing/lib/store/app.dart b/editing/lib/store/app.dart index da0420b..e53d8ea 100644 --- a/editing/lib/store/app.dart +++ b/editing/lib/store/app.dart @@ -1,3 +1,5 @@ +import 'package:editing/config/meta.dart'; +import 'package:editing/database/database.dart'; import 'package:kdbx/kdbx.dart'; import 'package:mobx/mobx.dart'; @@ -12,8 +14,29 @@ abstract class AppBase with Store { @observable bool isLogging = false; + @observable + Meta? meta; + + @observable + AppDb? openedDb; + @action void setLogging(bool isLogging) { this.isLogging = isLogging; } + + @action + void authenticate(Meta meta, AppDb db) { + this.meta = meta; + isAuthenticated = true; + openedDb = db; + } + + @action + Future logout() async { + await openedDb?.close(); + meta = null; + isAuthenticated = false; + openedDb = null; + } } diff --git a/editing/lib/ui/screens/login_screen.dart b/editing/lib/ui/screens/login_screen.dart index fecaf93..153c20f 100644 --- a/editing/lib/ui/screens/login_screen.dart +++ b/editing/lib/ui/screens/login_screen.dart @@ -1,3 +1,4 @@ +import 'package:editing/database/database.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_svg/flutter_svg.dart'; @@ -59,23 +60,22 @@ class _LoginScreenState extends State { onLogin(ProtectedValue p) async { appStore.setLogging(true); try { - final isPasswordCorrect = await appService.verifyPassword(p); - if (isPasswordCorrect) { - if (context.mounted) { - Navigator.pushReplacementNamed(context, "/home"); - } - } else { - await Future.delayed(const Duration(seconds: 1)); - if (context.mounted) { - ScaffoldMessenger.of(context).showSnackBar(const SnackBar( - showCloseIcon: true, - backgroundColor: Colors.redAccent, - content: Text("Incorrect password!"), - )); - } + final meta = await appService.login(p); + AppDb db = AppDb.open("${meta.dbInstance}.enc", meta.dbEncryptionKey); + appStore.authenticate(meta, db); + if (context.mounted) { + Navigator.pushReplacementNamed(context, "/home"); } } catch (e) { logger.e(e); + await Future.delayed(const Duration(seconds: 1)); + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar(const SnackBar( + showCloseIcon: true, + backgroundColor: Colors.redAccent, + content: Text("Login failed!"), + )); + } } finally { appStore.setLogging(false); }