2020-06-20 07:10:00 +00:00
|
|
|
import 'dart:async';
|
2022-10-26 18:13:44 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:cake_wallet/view_model/auth_state.dart';
|
|
|
|
import 'package:cake_wallet/core/auth_service.dart';
|
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/core/execution_state.dart';
|
|
|
|
import 'package:cake_wallet/entities/biometric_auth.dart';
|
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
part 'auth_view_model.g.dart';
|
|
|
|
|
|
|
|
class AuthViewModel = AuthViewModelBase with _$AuthViewModel;
|
|
|
|
|
|
|
|
abstract class AuthViewModelBase with Store {
|
2022-12-09 19:18:36 +00:00
|
|
|
AuthViewModelBase(
|
|
|
|
this._authService, this._sharedPreferences, this._settingsStore, this._biometricAuth)
|
2022-10-12 17:09:57 +00:00
|
|
|
: _failureCounter = 0,
|
2022-12-09 19:18:36 +00:00
|
|
|
state = InitialExecutionState() {
|
|
|
|
reaction((_) => state, _saveLastAuthTime);
|
|
|
|
}
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
static const maxFailedLogins = 3;
|
2020-09-21 11:50:26 +00:00
|
|
|
static const banTimeout = 180; // 3 minutes
|
2020-06-20 07:10:00 +00:00
|
|
|
final banTimeoutKey = S.current.auth_store_ban_timeout;
|
|
|
|
|
|
|
|
@observable
|
2020-09-21 11:50:26 +00:00
|
|
|
ExecutionState state;
|
|
|
|
|
|
|
|
int get pinLength => _settingsStore.pinCodeLength;
|
|
|
|
|
2022-12-09 19:18:36 +00:00
|
|
|
bool get isBiometricalAuthenticationAllowed => _settingsStore.allowBiometricalAuthentication;
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
@observable
|
|
|
|
int _failureCounter;
|
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
final AuthService _authService;
|
|
|
|
final BiometricAuth _biometricAuth;
|
|
|
|
final SharedPreferences _sharedPreferences;
|
|
|
|
final SettingsStore _settingsStore;
|
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
@action
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> auth({required String password}) async {
|
2020-09-21 11:50:26 +00:00
|
|
|
state = InitialExecutionState();
|
2020-06-20 07:10:00 +00:00
|
|
|
final _banDuration = banDuration();
|
|
|
|
|
|
|
|
if (_banDuration != null) {
|
|
|
|
state = AuthenticationBanned(
|
|
|
|
error: S.current.auth_store_banned_for +
|
|
|
|
'${_banDuration.inMinutes}' +
|
|
|
|
S.current.auth_store_banned_minutes);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
state = IsExecutingState();
|
|
|
|
final isSuccessfulAuthenticated = await _authService.authenticate(password);
|
2020-06-20 07:10:00 +00:00
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
if (isSuccessfulAuthenticated) {
|
2022-10-26 18:13:44 +00:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
2022-12-09 19:18:36 +00:00
|
|
|
state = ExecutedSuccessfullyState();
|
2022-10-26 18:13:44 +00:00
|
|
|
_failureCounter = 0;
|
|
|
|
});
|
2020-06-20 07:10:00 +00:00
|
|
|
} else {
|
|
|
|
_failureCounter += 1;
|
|
|
|
|
|
|
|
if (_failureCounter >= maxFailedLogins) {
|
|
|
|
final banDuration = await ban();
|
|
|
|
state = AuthenticationBanned(
|
|
|
|
error: S.current.auth_store_banned_for +
|
|
|
|
'${banDuration.inMinutes}' +
|
|
|
|
S.current.auth_store_banned_minutes);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
state = FailureState(S.current.auth_store_incorrect_password);
|
2020-06-20 07:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Duration? banDuration() {
|
2020-09-21 11:50:26 +00:00
|
|
|
final unbanTimestamp = _sharedPreferences.getInt(banTimeoutKey);
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
if (unbanTimestamp == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final unbanTime = DateTime.fromMillisecondsSinceEpoch(unbanTimestamp);
|
|
|
|
final now = DateTime.now();
|
|
|
|
|
|
|
|
if (now.isAfter(unbanTime)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Duration(milliseconds: unbanTimestamp - now.millisecondsSinceEpoch);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Duration> ban() async {
|
|
|
|
final multiplier = _failureCounter - maxFailedLogins + 1;
|
|
|
|
final timeout = (multiplier * banTimeout) * 1000;
|
|
|
|
final unbanTimestamp = DateTime.now().millisecondsSinceEpoch + timeout;
|
2020-09-21 11:50:26 +00:00
|
|
|
await _sharedPreferences.setInt(banTimeoutKey, unbanTimestamp);
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
return Duration(milliseconds: timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2020-09-21 11:50:26 +00:00
|
|
|
Future<void> biometricAuth() async {
|
|
|
|
try {
|
|
|
|
final canBiometricAuth = await _biometricAuth.canCheckBiometrics();
|
|
|
|
|
|
|
|
if (canBiometricAuth) {
|
|
|
|
final isAuthenticated = await _biometricAuth.isAuthenticated();
|
|
|
|
|
|
|
|
if (isAuthenticated) {
|
|
|
|
state = ExecutedSuccessfullyState();
|
|
|
|
}
|
|
|
|
}
|
2022-12-09 19:18:36 +00:00
|
|
|
} catch (e) {
|
2020-09-21 11:50:26 +00:00
|
|
|
state = FailureState(e.toString());
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 20:52:28 +00:00
|
|
|
|
2022-12-09 16:08:52 +00:00
|
|
|
void _saveLastAuthTime(ExecutionState state) {
|
2022-12-09 19:18:36 +00:00
|
|
|
if (state is ExecutedSuccessfullyState) {
|
|
|
|
_authService.saveLastAuthTime();
|
2022-11-22 20:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-20 07:10:00 +00:00
|
|
|
}
|