mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-25 05:58:42 +01:00
TMP 4
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
abstract class AuthState {}
|
||||
|
||||
class AuthenticationStateInitial extends AuthState {}
|
||||
|
||||
class AuthenticationInProgress extends AuthState {}
|
||||
|
||||
class AuthenticatedSuccessfully extends AuthState {}
|
||||
|
||||
class AuthenticationFailure extends AuthState {
|
||||
AuthenticationFailure({this.error});
|
||||
|
||||
final String error;
|
||||
}
|
||||
|
||||
class AuthenticationBanned extends AuthState {
|
||||
AuthenticationBanned({this.error});
|
||||
|
||||
final String error;
|
||||
}
|
||||
|
||||
@@ -1,99 +1,99 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/src/domain/services/user_service.dart';
|
||||
import 'package:cake_wallet/src/domain/services/wallet_service.dart';
|
||||
import 'package:cake_wallet/src/stores/auth/auth_state.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
|
||||
part 'auth_store.g.dart';
|
||||
|
||||
class AuthStore = AuthStoreBase with _$AuthStore;
|
||||
|
||||
abstract class AuthStoreBase with Store {
|
||||
AuthStoreBase(
|
||||
{@required this.userService,
|
||||
@required this.walletService,
|
||||
@required this.sharedPreferences}) {
|
||||
state = AuthenticationStateInitial();
|
||||
_failureCounter = 0;
|
||||
}
|
||||
|
||||
static const maxFailedLogins = 3;
|
||||
static const banTimeout = 180; // 3 mins
|
||||
final banTimeoutKey = S.current.auth_store_ban_timeout;
|
||||
|
||||
final UserService userService;
|
||||
final WalletService walletService;
|
||||
|
||||
final SharedPreferences sharedPreferences;
|
||||
|
||||
@observable
|
||||
AuthState state;
|
||||
|
||||
@observable
|
||||
int _failureCounter;
|
||||
|
||||
@action
|
||||
Future auth({String password}) async {
|
||||
state = AuthenticationStateInitial();
|
||||
final _banDuration = banDuration();
|
||||
|
||||
if (_banDuration != null) {
|
||||
state = AuthenticationBanned(
|
||||
error: S.current.auth_store_banned_for + '${_banDuration.inMinutes}' + S.current.auth_store_banned_minutes);
|
||||
return;
|
||||
}
|
||||
|
||||
state = AuthenticationInProgress();
|
||||
final isAuth = await userService.authenticate(password);
|
||||
|
||||
if (isAuth) {
|
||||
state = AuthenticatedSuccessfully();
|
||||
_failureCounter = 0;
|
||||
} 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;
|
||||
}
|
||||
|
||||
state = AuthenticationFailure(error: S.current.auth_store_incorrect_password);
|
||||
}
|
||||
}
|
||||
|
||||
Duration banDuration() {
|
||||
final unbanTimestamp = sharedPreferences.getInt(banTimeoutKey);
|
||||
|
||||
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;
|
||||
await sharedPreferences.setInt(banTimeoutKey, unbanTimestamp);
|
||||
|
||||
return Duration(milliseconds: timeout);
|
||||
}
|
||||
|
||||
@action
|
||||
void biometricAuth() {
|
||||
state = AuthenticatedSuccessfully();
|
||||
}
|
||||
}
|
||||
//import 'dart:async';
|
||||
//import 'package:flutter/foundation.dart';
|
||||
//import 'package:shared_preferences/shared_preferences.dart';
|
||||
//import 'package:mobx/mobx.dart';
|
||||
//import 'package:cake_wallet/src/domain/services/user_service.dart';
|
||||
//import 'package:cake_wallet/src/domain/services/wallet_service.dart';
|
||||
//import 'package:cake_wallet/view_model/auth_state.dart';
|
||||
//import 'package:cake_wallet/generated/i18n.dart';
|
||||
//
|
||||
//part 'auth_store.g.dart';
|
||||
//
|
||||
//class AuthStore = AuthStoreBase with _$AuthStore;
|
||||
//
|
||||
//abstract class AuthStoreBase with Store {
|
||||
// AuthStoreBase(
|
||||
// {@required this.userService,
|
||||
// @required this.walletService,
|
||||
// @required this.sharedPreferences}) {
|
||||
// state = AuthenticationStateInitial();
|
||||
// _failureCounter = 0;
|
||||
// }
|
||||
//
|
||||
// static const maxFailedLogins = 3;
|
||||
// static const banTimeout = 180; // 3 mins
|
||||
// final banTimeoutKey = S.current.auth_store_ban_timeout;
|
||||
//
|
||||
// final UserService userService;
|
||||
// final WalletService walletService;
|
||||
//
|
||||
// final SharedPreferences sharedPreferences;
|
||||
//
|
||||
// @observable
|
||||
// AuthState state;
|
||||
//
|
||||
// @observable
|
||||
// int _failureCounter;
|
||||
//
|
||||
// @action
|
||||
// Future auth({String password}) async {
|
||||
// state = AuthenticationStateInitial();
|
||||
// final _banDuration = banDuration();
|
||||
//
|
||||
// if (_banDuration != null) {
|
||||
// state = AuthenticationBanned(
|
||||
// error: S.current.auth_store_banned_for + '${_banDuration.inMinutes}' + S.current.auth_store_banned_minutes);
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// state = AuthenticationInProgress();
|
||||
// final isAuth = await userService.authenticate(password);
|
||||
//
|
||||
// if (isAuth) {
|
||||
// state = AuthenticatedSuccessfully();
|
||||
// _failureCounter = 0;
|
||||
// } 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;
|
||||
// }
|
||||
//
|
||||
// state = AuthenticationFailure(error: S.current.auth_store_incorrect_password);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Duration banDuration() {
|
||||
// final unbanTimestamp = sharedPreferences.getInt(banTimeoutKey);
|
||||
//
|
||||
// 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;
|
||||
// await sharedPreferences.setInt(banTimeoutKey, unbanTimestamp);
|
||||
//
|
||||
// return Duration(milliseconds: timeout);
|
||||
// }
|
||||
//
|
||||
// @action
|
||||
// void biometricAuth() {
|
||||
// state = AuthenticatedSuccessfully();
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -30,8 +30,8 @@ abstract class AuthenticationStoreBase with Store {
|
||||
@observable
|
||||
AuthenticationState state;
|
||||
|
||||
@observable
|
||||
String errorMessage;
|
||||
// @observable
|
||||
// String errorMessage;
|
||||
|
||||
Future started() async {
|
||||
final canAuth = await userService.canAuthenticate();
|
||||
|
||||
@@ -2,7 +2,7 @@ import 'package:mobx/mobx.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:cake_wallet/src/domain/services/wallet_list_service.dart';
|
||||
import 'package:cake_wallet/src/domain/common/mnemotic_item.dart';
|
||||
import 'package:cake_wallet/src/domain/common/mnemonic_item.dart';
|
||||
import 'package:cake_wallet/src/stores/wallet_restoration/wallet_restoration_state.dart';
|
||||
import 'package:cake_wallet/src/stores/authentication/authentication_store.dart';
|
||||
import 'package:cake_wallet/src/domain/common/crypto_currency.dart';
|
||||
@@ -37,7 +37,7 @@ abstract class WalleRestorationStoreBase with Store {
|
||||
bool isValid;
|
||||
|
||||
@observable
|
||||
List<MnemoticItem> seed;
|
||||
List<MnemonicItem> seed;
|
||||
|
||||
@observable
|
||||
bool disabledState;
|
||||
@@ -79,35 +79,35 @@ abstract class WalleRestorationStoreBase with Store {
|
||||
}
|
||||
|
||||
@action
|
||||
void setSeed(List<MnemoticItem> seed) {
|
||||
void setSeed(List<MnemonicItem> seed) {
|
||||
this.seed = seed;
|
||||
}
|
||||
|
||||
@action
|
||||
void validateSeed(List<MnemoticItem> seed) {
|
||||
final _seed = seed != null ? seed : this.seed;
|
||||
bool isValid = _seed != null ? _seed.length == 25 : false;
|
||||
|
||||
if (!isValid) {
|
||||
errorMessage = S.current.wallet_restoration_store_incorrect_seed_length;
|
||||
this.isValid = isValid;
|
||||
return;
|
||||
}
|
||||
|
||||
for (final item in _seed) {
|
||||
if (!item.isCorrect()) {
|
||||
isValid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isValid) {
|
||||
errorMessage = null;
|
||||
}
|
||||
|
||||
this.isValid = isValid;
|
||||
return;
|
||||
}
|
||||
// @action
|
||||
// void validateSeed(List<MnemonicItem> seed) {
|
||||
// final _seed = seed != null ? seed : this.seed;
|
||||
// bool isValid = _seed != null ? _seed.length == 25 : false;
|
||||
//
|
||||
// if (!isValid) {
|
||||
// errorMessage = S.current.wallet_restoration_store_incorrect_seed_length;
|
||||
// this.isValid = isValid;
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// for (final item in _seed) {
|
||||
// if (!item.isCorrect()) {
|
||||
// isValid = false;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (isValid) {
|
||||
// errorMessage = null;
|
||||
// }
|
||||
//
|
||||
// this.isValid = isValid;
|
||||
// return;
|
||||
// }
|
||||
|
||||
String _seedText() {
|
||||
return seed.fold('', (acc, item) => acc + ' ' + item.toString());
|
||||
|
||||
Reference in New Issue
Block a user