Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch exception during biometrics process and fallback to PIN dialog in this case. #1610

Merged
merged 5 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ enum BiometricAuthState {
BiometricAuthState.disabled => 'disabled',
};
}

bool get isEnabled => this == BiometricAuthState.enabled;
}
13 changes: 9 additions & 4 deletions app/lib/modules/login/logic/login_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';

import 'package:encointer_wallet/modules/modules.dart';
import 'package:encointer_wallet/config/biometiric_auth_state.dart';
import 'package:encointer_wallet/config/biometric_auth_state.dart';

part 'login_store.g.dart';

Expand Down Expand Up @@ -38,9 +38,9 @@ abstract class _LoginStoreBase with Store {
await loginService.setPin(pin);
}

Future<void> clearPin() async {
Future<void> deleteAuthenticationData() async {
cachedPin = null;
await loginService.clearPin();
await loginService.deleteAuthenticationData();
}

@computed
Expand All @@ -58,7 +58,12 @@ abstract class _LoginStoreBase with Store {
return loginService.isDeviceSupported();
}

/// Might throw a `PlatformException` if there were technical problems.
Future<bool> localAuthenticate(String localizedReason, [bool stickyAuth = false]) {
return loginService.localAuthenticate(localizedReason, stickyAuth);
try {
return loginService.localAuthenticate(localizedReason, stickyAuth);
} catch (e) {
rethrow;
}
}
}
18 changes: 13 additions & 5 deletions app/lib/modules/login/service/login_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/foundation.dart';
import 'package:local_auth/local_auth.dart';

import 'package:encointer_wallet/service/service.dart';
import 'package:encointer_wallet/config/biometiric_auth_state.dart';
import 'package:encointer_wallet/config/biometric_auth_state.dart';

@immutable
final class LoginService {
Expand All @@ -13,7 +13,6 @@ final class LoginService {
final SharedPreferences preferences;
final SecureStorage secureStorage;

static const isDeviceSupportKey = 'is-device-support-key';
static const biometricAuthStateKey = 'biometric-auth-state';
static const oldBiometricAuthStateKey = 'biometric-auth-enabled';
static const pinStorageKey = 'pin-key';
Expand Down Expand Up @@ -50,7 +49,13 @@ final class LoginService {
await secureStorage.write(key: pinStorageKey, value: pin);
}

Future<void> clearPin() => secureStorage.clear();
Copy link
Member Author

@clangenb clangenb Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was dangerous, it clears the entire secure storage associated with the application. This was harmless now, as we don't store anything else currently, but later we intend to store the account seeds there too.

Future<void> deleteAuthenticationData() {
return Future.wait([
secureStorage.delete(key: pinStorageKey),
preferences.remove(oldBiometricAuthStateKey),
preferences.remove(biometricAuthStateKey),
]);
}

/// Check if local authentication is supported on the device.
/// Returns a `future` with `true` if supported, `false` otherwise.
Expand All @@ -66,16 +71,19 @@ final class LoginService {

/// Authenticates the user with biometrics or device authentication options available on the device.
/// Returns a `Future<bool>` which is `true` if successful, `false` otherwise.
///
/// [localizedReason] is the message displayed to the user during the authentication prompt.
///
/// Might throw a `PlatformException` if there were technical problems.
Future<bool> localAuthenticate(String localizedReason, [bool stickyAuth = false]) {
try {
return localAuthentication.authenticate(
localizedReason: localizedReason,
options: AuthenticationOptions(useErrorDialogs: false, stickyAuth: stickyAuth),
);
} catch (e, s) {
Log.e('$e', 'LoginStore', s);
return Future.value(false);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might have been the reason that on some phones the authentication didn't work any more. We swallowed the exception, and returned false for authentication failed.

Log.e('$e', 'LoginService', s);
rethrow;
}
}
}
37 changes: 27 additions & 10 deletions app/lib/modules/login/view/login_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:provider/provider.dart';

import 'package:encointer_wallet/config/biometiric_auth_state.dart';
import 'package:encointer_wallet/config/biometric_auth_state.dart';
import 'package:encointer_wallet/service/service.dart';
import 'package:encointer_wallet/store/app.dart';
import 'package:encointer_wallet/l10n/l10.dart';
Expand Down Expand Up @@ -59,13 +59,26 @@ final class LoginDialog {
String? titleText,
}) async {
final loginStore = context.read<LoginStore>();
if (loginStore.getBiometricAuthState == BiometricAuthState.enabled) {
await showLocalAuth(
context,
onSuccess: onSuccess,
stickyAuth: stickyAuth,
titleText: titleText,
);
if (loginStore.getBiometricAuthState?.isEnabled ?? false) {
try {
await showLocalAuth(
context,
onSuccess: onSuccess,
stickyAuth: stickyAuth,
titleText: titleText,
);
} catch (e, s) {
Log.e('$e', 'LoginDialog: error with biometrics, fallback to PIN dialog', s);
await showPasswordInputDialog(
context,
onSuccess: onSuccess,
barrierDismissible: barrierDismissible,
autoCloseOnSuccess: autoCloseOnSuccess,
showCancelButton: showCancelButton,
canPop: canPop,
titleText: titleText,
);
}
} else {
await showPasswordInputDialog(
context,
Expand All @@ -86,8 +99,12 @@ final class LoginDialog {
bool stickyAuth = false,
}) async {
final loginStore = context.read<LoginStore>();
final value = await loginStore.localAuthenticate(titleText ?? context.l10n.verifyAuthTitle('true'), stickyAuth);
if (value) await onSuccess(loginStore.cachedPin ?? await loginStore.loginService.getPin() ?? '');
try {
final value = await loginStore.localAuthenticate(titleText ?? context.l10n.verifyAuthTitle('true'), stickyAuth);
if (value) await onSuccess(loginStore.cachedPin ?? await loginStore.loginService.getPin() ?? '');
} catch (e) {
rethrow;
}
}

static Future<void> showPasswordInputDialog(
Expand Down
2 changes: 1 addition & 1 deletion app/lib/modules/splash/view/splash_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:encointer_wallet/service/init_web_api/init_web_api.dart';
import 'package:encointer_wallet/config/biometiric_auth_state.dart';
import 'package:encointer_wallet/config/biometric_auth_state.dart';
import 'package:encointer_wallet/modules/modules.dart';
import 'package:encointer_wallet/gen/assets.gen.dart';
import 'package:encointer_wallet/presentation/home/views/home_page.dart';
Expand Down
2 changes: 1 addition & 1 deletion app/lib/page/profile/account/account_manage_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class _AccountManagePageState extends State<AccountManagePage> {
.then((_) => _appStore.loadAccountCache())
.then((_) => webApi.fetchAccountData());
if (_appStore.account.accountListAll.isEmpty) {
await context.read<LoginStore>().clearPin();
await context.read<LoginStore>().deleteAuthenticationData();
await Navigator.pushNamedAndRemoveUntil(context, CreateAccountEntryView.route, (route) => false);
} else {
Navigator.pop(context);
Expand Down
6 changes: 3 additions & 3 deletions app/lib/page/profile/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:encointer_wallet/common/components/address_icon.dart';
import 'package:encointer_wallet/common/components/submit_button.dart';
import 'package:encointer_wallet/common/components/launch/send_to_trello_list_tile.dart';
import 'package:encointer_wallet/theme/theme.dart';
import 'package:encointer_wallet/config/biometiric_auth_state.dart';
import 'package:encointer_wallet/config/biometric_auth_state.dart';
import 'package:encointer_wallet/modules/modules.dart';
import 'package:encointer_wallet/page/network_select_page.dart';
import 'package:encointer_wallet/page/profile/about_page.dart';
Expand Down Expand Up @@ -155,7 +155,7 @@ class _ProfileState extends State<Profile> {
for (final acc in context.read<AppStore>().account.accountListAll) {
await store.account.removeAccount(acc);
}
await context.read<LoginStore>().clearPin();
await context.read<LoginStore>().deleteAuthenticationData();
await Navigator.pushNamedAndRemoveUntil(context, CreateAccountEntryView.route, (route) => false);
},
);
Expand Down Expand Up @@ -183,7 +183,7 @@ class _ProfileState extends State<Profile> {
_ => SwitchListTile(
title: Text(l10n.biometricAuth, style: h3Grey),
onChanged: (value) => LoginDialog.switchBiometricAuth(context, isEnable: value),
value: loginStore.biometricAuthState == BiometricAuthState.enabled,
value: loginStore.biometricAuthState?.isEnabled ?? false,
),
};
}),
Expand Down
Loading