Skip to content

Commit

Permalink
updated login impl
Browse files Browse the repository at this point in the history
  • Loading branch information
drriguz committed Mar 12, 2024
1 parent 318b718 commit 2e3ee8c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
1 change: 1 addition & 0 deletions editing/lib/common/exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class IncorrectPasswordException implements Exception {}
11 changes: 7 additions & 4 deletions editing/lib/service/app_service.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -25,14 +27,15 @@ class AppService {
return config.existsSync();
}

Future<bool> verifyPassword(ProtectedValue password) async {
Future<Meta> 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();
}
}

Expand Down
23 changes: 23 additions & 0 deletions editing/lib/store/app.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<void> logout() async {
await openedDb?.close();
meta = null;
isAuthenticated = false;
openedDb = null;
}
}
28 changes: 14 additions & 14 deletions editing/lib/ui/screens/login_screen.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -59,23 +60,22 @@ class _LoginScreenState extends State<LoginScreen> {
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);
}
Expand Down

0 comments on commit 2e3ee8c

Please sign in to comment.