CW-597-AuthService-Bug (#1346)

* fix: AuthService keychain bug fix * fix: Fetch read implementation * fix: Simplify logic for retries * Minor enhancement --------- Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

Adegoke David committed Mar 26, 2024 at 11:37 UTC 6ae0f37b9c6ccb6376981e944eea1a7054eb270d
3 files changed +34 -3
lib/core/auth_service.dart
+5 -2
@@ -1,5 +1,7 @@
1 +import 'dart:async';
2 import 'dart:io';
3
4 +import 'package:cake_wallet/core/secure_storage.dart';
5 import 'package:cake_wallet/core/totp_request_details.dart';
6 import 'package:cake_wallet/routes.dart';
7 import 'package:cake_wallet/src/screens/auth/auth_page.dart';
@@ -64,7 +66,7 @@ class AuthService with Store {
66
67 Future<bool> authenticate(String pin) async {
68 final key = generateStoreKeyFor(key: SecretStoreKey.pinCodePassword);
67 - final encodedPin = await secureStorage.read(key: key);
69 + final encodedPin = await readSecureStorage(secureStorage, key);
70 final decodedPin = decodedPinCode(pin: encodedPin!);
71
72 return decodedPin == pin;
@@ -76,7 +78,8 @@ class AuthService with Store {
78 }
79
80 Future<bool> requireAuth() async {
79 - final timestamp = int.tryParse(await secureStorage.read(key: SecureKey.lastAuthTimeMilliseconds) ?? '0');
81 + final timestamp =
82 + int.tryParse(await secureStorage.read(key: SecureKey.lastAuthTimeMilliseconds) ?? '0');
83 final duration = _durationToRequireAuth(timestamp ?? 0);
84 final requiredPinInterval = settingsStore.pinTimeOutDuration;
85
lib/core/key_service.dart
+2 -1
@@ -1,3 +1,4 @@
1 +import 'package:cake_wallet/core/secure_storage.dart';
2 import 'package:flutter_secure_storage/flutter_secure_storage.dart';
3 import 'package:cake_wallet/entities/secret_store_key.dart';
4 import 'package:cake_wallet/entities/encrypt.dart';
@@ -10,7 +11,7 @@ class KeyService {
11 Future<String> getWalletPassword({required String walletName}) async {
12 final key = generateStoreKeyFor(
13 key: SecretStoreKey.moneroWalletPassword, walletName: walletName);
13 - final encodedPassword = await _secureStorage.read(key: key);
14 + final encodedPassword = await readSecureStorage(_secureStorage, key);
15 return decodeWalletPassword(password: encodedPassword!);
16 }
17
lib/core/secure_storage.dart new
+27
@@ -0,0 +1,27 @@
1 +import 'dart:async';
2 +import 'package:flutter_secure_storage/flutter_secure_storage.dart';
3 +// For now, we can create a utility function to handle this.
4 +//
5 +// However, we could look into abstracting the entire FlutterSecureStorage package
6 +// so the app doesn't depend on the package directly but an absraction.
7 +// It'll make these kind of modifications to read/write come from a single point.
8 +
9 +Future<String?> readSecureStorage(FlutterSecureStorage secureStorage, String key) async {
10 + String? result;
11 + const maxWait = Duration(seconds: 3);
12 + const checkInterval = Duration(milliseconds: 200);
13 +
14 + DateTime start = DateTime.now();
15 +
16 + while (result == null && DateTime.now().difference(start) < maxWait) {
17 + result = await secureStorage.read(key: key);
18 +
19 + if (result != null) {
20 + break;
21 + }
22 +
23 + await Future.delayed(checkInterval);
24 + }
25 +
26 + return result;
27 +}