workaround for secure storage bug on mac

This commit is contained in:
Matthew Fosse 2024-04-25 10:18:03 -07:00
parent efc23c9d38
commit 46c46c36d1
9 changed files with 36 additions and 28 deletions

View file

@ -42,12 +42,7 @@ class AuthService with Store {
Future<void> setPassword(String password) async {
final key = generateStoreKeyFor(key: SecretStoreKey.pinCodePassword);
final encodedPassword = encodedPinCode(pin: password);
// secure storage has a weird bug on macOS, where overwriting a key doesn't work, unless
// we delete what's there first:
if (Platform.isMacOS) {
await secureStorage.delete(key: key);
}
await secureStorage.write(key: key, value: encodedPassword);
await writeSecureStorage(secureStorage, key: key, value: encodedPassword);
}
Future<bool> canAuthenticate() async {
@ -74,7 +69,11 @@ class AuthService with Store {
void saveLastAuthTime() {
int timestamp = DateTime.now().millisecondsSinceEpoch;
secureStorage.write(key: SecureKey.lastAuthTimeMilliseconds, value: timestamp.toString());
writeSecureStorage(
secureStorage,
key: SecureKey.lastAuthTimeMilliseconds,
value: timestamp.toString(),
);
}
Future<bool> requireAuth() async {

View file

@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:cake_wallet/core/secure_storage.dart';
import 'package:cake_wallet/themes/theme_list.dart';
import 'package:cake_wallet/utils/device_info.dart';
import 'package:cw_core/wallet_type.dart';
@ -275,7 +276,7 @@ class BackupService {
if (currentTransactionPriorityKeyLegacy != null)
await _sharedPreferences.setInt(
PreferencesKey.currentTransactionPriorityKeyLegacy, currentTransactionPriorityKeyLegacy);
if (currentBitcoinElectrumSererId != null)
await _sharedPreferences.setInt(
PreferencesKey.currentBitcoinElectrumSererIdKey, currentBitcoinElectrumSererId);
@ -373,16 +374,15 @@ class BackupService {
final backupPasswordKey = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
final backupPassword = keychainJSON[backupPasswordKey] as String;
await _flutterSecureStorage.delete(key: backupPasswordKey);
await _flutterSecureStorage.write(key: backupPasswordKey, value: backupPassword);
await writeSecureStorage(_flutterSecureStorage, key: backupPasswordKey, value: backupPassword);
keychainWalletsInfo.forEach((dynamic rawInfo) async {
final info = rawInfo as Map<String, dynamic>;
await importWalletKeychainInfo(info);
});
await _flutterSecureStorage.delete(key: pinCodeKey);
await _flutterSecureStorage.write(key: pinCodeKey, value: encodedPinCode(pin: decodedPin));
await writeSecureStorage(_flutterSecureStorage,
key: pinCodeKey, value: encodedPinCode(pin: decodedPin));
keychainDumpFile.deleteSync();
}
@ -401,16 +401,15 @@ class BackupService {
final backupPasswordKey = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
final backupPassword = keychainJSON[backupPasswordKey] as String;
await _flutterSecureStorage.delete(key: backupPasswordKey);
await _flutterSecureStorage.write(key: backupPasswordKey, value: backupPassword);
await writeSecureStorage(_flutterSecureStorage, key: backupPasswordKey, value: backupPassword);
keychainWalletsInfo.forEach((dynamic rawInfo) async {
final info = rawInfo as Map<String, dynamic>;
await importWalletKeychainInfo(info);
});
await _flutterSecureStorage.delete(key: pinCodeKey);
await _flutterSecureStorage.write(key: pinCodeKey, value: encodedPinCode(pin: decodedPin));
await writeSecureStorage(_flutterSecureStorage,
key: pinCodeKey, value: encodedPinCode(pin: decodedPin));
keychainDumpFile.deleteSync();
}

View file

@ -20,8 +20,7 @@ class KeyService {
key: SecretStoreKey.moneroWalletPassword, walletName: walletName);
final encodedPassword = encodeWalletPassword(password: password);
await _secureStorage.delete(key: key);
await _secureStorage.write(key: key, value: encodedPassword);
await writeSecureStorage(_secureStorage, key: key, value: encodedPassword);
}
Future<void> deleteWalletPassword({required String walletName}) async {

View file

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
// For now, we can create a utility function to handle this.
//
@ -25,3 +26,12 @@ Future<String?> readSecureStorage(FlutterSecureStorage secureStorage, String key
return result;
}
Future<void> writeSecureStorage(FlutterSecureStorage secureStorage,
{required String key, required String value}) async {
// delete the value before writing on macOS because of a weird bug
if (Platform.isMacOS) {
await secureStorage.delete(key: key);
}
await secureStorage.write(key: key, value: value);
}

View file

@ -1,5 +1,6 @@
import 'dart:io';
import 'dart:convert';
import 'package:cake_wallet/core/secure_storage.dart';
import 'package:collection/collection.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -147,8 +148,8 @@ Future<void> ios_migrate_pin() async {
final key = generateStoreKeyFor(key: SecretStoreKey.pinCodePassword);
final encodedPassword = encodedPinCode(pin: pinPassword);
await flutterSecureStorage.delete(key: key);
await flutterSecureStorage.write(key: key, value: encodedPassword);
await writeSecureStorage(flutterSecureStorage, key: key, value: encodedPassword);
await prefs.setBool('ios_migration_pin_completed', true);
}

View file

@ -1,3 +1,4 @@
import 'package:cake_wallet/core/secure_storage.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:cw_core/cake_hive.dart';
@ -10,8 +11,7 @@ Future<List<int>> getEncryptionKey(
key = CakeHive.generateSecureKey();
final keyStringified = key.join(',');
String storageKey = 'transactionDescriptionsBoxKey';
await secureStorage.delete(key: storageKey);
await secureStorage.write(key: storageKey, value: keyStringified);
await writeSecureStorage(secureStorage, key: storageKey, value: keyStringified);
} else {
key = stringifiedKey.split(',').map((i) => int.parse(i)).toList();
}

View file

@ -1,5 +1,6 @@
import 'dart:convert';
import 'package:cake_wallet/core/secure_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
@ -58,7 +59,6 @@ class ChatwootWidgetState extends State<ChatwootWidget> {
}
Future<void> storeCookie(String value) async {
await widget.secureStorage.delete(key: COOKIE_KEY);
await widget.secureStorage.write(key: COOKIE_KEY, value: value);
await writeSecureStorage(widget.secureStorage, key: COOKIE_KEY, value: value);
}
}

View file

@ -2,6 +2,7 @@ import 'dart:io';
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/bitcoin_cash/bitcoin_cash.dart';
import 'package:cake_wallet/core/secure_storage.dart';
import 'package:cake_wallet/entities/auto_generate_subaddress_status.dart';
import 'package:cake_wallet/entities/provider_types.dart';
import 'package:cake_wallet/entities/cake_2fa_preset_options.dart';
@ -434,7 +435,7 @@ abstract class SettingsStoreBase with Store {
// secure storage keys:
reaction(
(_) => allowBiometricalAuthentication,
(bool biometricalAuthentication) => secureStorage.write(
(bool biometricalAuthentication) => writeSecureStorage(secureStorage,
key: SecureKey.allowBiometricalAuthenticationKey,
value: biometricalAuthentication.toString()));

View file

@ -1,3 +1,4 @@
import 'package:cake_wallet/core/secure_storage.dart';
import 'package:mobx/mobx.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:cake_wallet/entities/secret_store_key.dart';
@ -37,8 +38,6 @@ abstract class EditBackupPasswordViewModelBase with Store {
@action
Future<void> save() async {
final key = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
await secureStorage.delete(key: key);
await secureStorage.write(key: key, value: backupPassword);
secretStore.write(key: key, value: backupPassword);
await writeSecureStorage(secureStorage, key: key, value: backupPassword);
}
}