Skip to content

Commit dc6884e

Browse files
committed
ref: replace EncPref with SecureStow
1 parent efbdb6e commit dc6884e

File tree

1 file changed

+8
-69
lines changed

1 file changed

+8
-69
lines changed

lib/data/prefs.dart

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'dart:io';
55

66
import 'package:flutter/foundation.dart';
77
import 'package:flutter/material.dart';
8-
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
98
import 'package:logging/logging.dart';
109
import 'package:nextcloud/provisioning_api.dart';
1110
import 'package:perfect_freehand/perfect_freehand.dart';
@@ -18,6 +17,7 @@ import 'package:saber/data/tools/highlighter.dart';
1817
import 'package:saber/data/tools/pen.dart';
1918
import 'package:shared_preferences/shared_preferences.dart';
2019
import 'package:stow/stow.dart';
20+
import 'package:stow_secure/stow_secure.dart';
2121

2222
typedef Quota = UserDetailsQuota;
2323

@@ -49,18 +49,18 @@ class Stows {
4949
PlainPref<String?>('customDataDir', null, autoRead: _isOnMainIsolate);
5050

5151
final allowInsecureConnections =
52-
EncPref('allowInsecureConnections', false, autoRead: _isOnMainIsolate);
53-
final url = EncPref('url', '', autoRead: _isOnMainIsolate);
54-
final username = EncPref('username', '', autoRead: _isOnMainIsolate);
52+
SecureStow('allowInsecureConnections', false, autoRead: _isOnMainIsolate);
53+
final url = SecureStow('url', '', autoRead: _isOnMainIsolate);
54+
final username = SecureStow('username', '', autoRead: _isOnMainIsolate);
5555

5656
/// the password used to login to Nextcloud
57-
final ncPassword = EncPref('ncPassword', '', autoRead: _isOnMainIsolate);
57+
final ncPassword = SecureStow('ncPassword', '', autoRead: _isOnMainIsolate);
5858
// TODO(adil192): maybe deprecate?
5959
final ncPasswordIsAnAppPassword =
6060
PlainPref('ncPasswordIsAnAppPassword', false, autoRead: _isOnMainIsolate);
6161

6262
/// the password used to encrypt/decrypt notes
63-
final encPassword = EncPref('encPassword', '', autoRead: _isOnMainIsolate);
63+
final encPassword = SecureStow('encPassword', '', autoRead: _isOnMainIsolate);
6464

6565
/// Whether the user is logged in and has provided both passwords.
6666
/// Please ensure that the relevant Prefs are loaded before using this.
@@ -69,8 +69,8 @@ class Stows {
6969
ncPassword.value.isNotEmpty &&
7070
encPassword.value.isNotEmpty;
7171

72-
final key = EncPref('key', '', autoRead: _isOnMainIsolate);
73-
final iv = EncPref('iv', '', autoRead: _isOnMainIsolate);
72+
final key = SecureStow('key', '', autoRead: _isOnMainIsolate);
73+
final iv = SecureStow('iv', '', autoRead: _isOnMainIsolate);
7474

7575
final pfp = PlainPref<Uint8List?>('pfp', null, autoRead: _isOnMainIsolate);
7676
final syncInBackground =
@@ -427,67 +427,6 @@ class PlainPref<T> extends IPref<T> {
427427
}
428428
}
429429

430-
class EncPref<T> extends IPref<T> {
431-
FlutterSecureStorage? _storage;
432-
433-
EncPref(
434-
super.key,
435-
super.defaultValue, {
436-
super.codec,
437-
super.autoRead,
438-
}) {
439-
assert(T == String || T == typeOf<List<String>>() || T == bool);
440-
}
441-
442-
@override
443-
Future protectedWrite(T value) async {
444-
_storage ??= const FlutterSecureStorage();
445-
if (T == String)
446-
return await _storage!.write(key: key, value: value as String);
447-
if (T == bool)
448-
return await _storage!.write(key: key, value: jsonEncode(value));
449-
return await _storage!.write(key: key, value: jsonEncode(value));
450-
}
451-
452-
@override
453-
Future<T> protectedRead() async {
454-
if (!_isOnMainIsolate) return defaultValue;
455-
456-
try {
457-
final String? value = await _storage!.read(key: key);
458-
return _parseString(value) ?? defaultValue;
459-
} catch (e) {
460-
stows.log.severe('Error loading $key: $e', e);
461-
return defaultValue;
462-
}
463-
}
464-
465-
T? _parseString(String? value) {
466-
if (value == null || value.isEmpty) return null;
467-
468-
if (T == String) {
469-
return value as T;
470-
} else if (T == bool) {
471-
return jsonDecode(value) as T;
472-
} else {
473-
return List<String>.from(jsonDecode(value)) as T;
474-
}
475-
}
476-
477-
@override
478-
Future<void> delete() async {
479-
if (!_isOnMainIsolate) return;
480-
481-
_storage ??= const FlutterSecureStorage();
482-
await _storage!.delete(key: key);
483-
}
484-
485-
@override
486-
String toString() {
487-
return 'EncPref<$T>($key, $value, $codec)';
488-
}
489-
}
490-
491430
/// An [IPref] that transforms the value of another [IPref].
492431
///
493432
/// Only instantiate this once during the lifetime of the app

0 commit comments

Comments
 (0)