Merge branch 'krille/on-secret-stored-stream' into 'main'

feat: Add onSecretStored StreamController to SSSS

Closes #331

See merge request famedly/company/frontend/famedlysdk!1135
This commit is contained in:
Nicolas Werner 2022-09-20 07:42:04 +00:00
commit c8a97ce7f8
1 changed files with 9 additions and 0 deletions

View File

@ -29,6 +29,7 @@ import 'package:matrix/encryption/encryption.dart';
import 'package:matrix/encryption/utils/base64_unpadded.dart'; import 'package:matrix/encryption/utils/base64_unpadded.dart';
import 'package:matrix/encryption/utils/ssss_cache.dart'; import 'package:matrix/encryption/utils/ssss_cache.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:matrix/src/utils/cached_stream_controller.dart';
import 'package:matrix/src/utils/crypto/crypto.dart' as uc; import 'package:matrix/src/utils/crypto/crypto.dart' as uc;
import 'package:matrix/src/utils/run_in_root.dart'; import 'package:matrix/src/utils/run_in_root.dart';
@ -60,6 +61,10 @@ class SSSS {
final _cacheCallbacks = <String, FutureOr<void> Function(String)>{}; final _cacheCallbacks = <String, FutureOr<void> Function(String)>{};
final Map<String, SSSSCache> _cache = <String, SSSSCache>{}; final Map<String, SSSSCache> _cache = <String, SSSSCache>{};
/// Will be called when a new secret has been stored in the database
final CachedStreamController<String> onSecretStored =
CachedStreamController();
SSSS(this.encryption); SSSS(this.encryption);
// for testing // for testing
@ -326,6 +331,7 @@ class SSSS {
if (cacheTypes.contains(type) && db != null) { if (cacheTypes.contains(type) && db != null) {
// cache the thing // cache the thing
await db.storeSSSSCache(type, keyId, enc['ciphertext'], decrypted); await db.storeSSSSCache(type, keyId, enc['ciphertext'], decrypted);
onSecretStored.add(keyId);
if (_cacheCallbacks.containsKey(type) && await getCached(type) == null) { if (_cacheCallbacks.containsKey(type) && await getCached(type) == null) {
_cacheCallbacks[type]!(decrypted); _cacheCallbacks[type]!(decrypted);
} }
@ -357,6 +363,7 @@ class SSSS {
if (cacheTypes.contains(type) && db != null) { if (cacheTypes.contains(type) && db != null) {
// cache the thing // cache the thing
await db.storeSSSSCache(type, keyId, encrypted.ciphertext, secret); await db.storeSSSSCache(type, keyId, encrypted.ciphertext, secret);
onSecretStored.add(keyId);
if (_cacheCallbacks.containsKey(type) && await getCached(type) == null) { if (_cacheCallbacks.containsKey(type) && await getCached(type) == null) {
_cacheCallbacks[type]!(secret); _cacheCallbacks[type]!(secret);
} }
@ -387,6 +394,7 @@ class SSSS {
// cache the thing // cache the thing
await client.database?.storeSSSSCache( await client.database?.storeSSSSCache(
type, keyId, content['encrypted'][keyId]['ciphertext'], secret); type, keyId, content['encrypted'][keyId]['ciphertext'], secret);
onSecretStored.add(keyId);
} }
} }
@ -553,6 +561,7 @@ class SSSS {
if (_cacheCallbacks.containsKey(request.type)) { if (_cacheCallbacks.containsKey(request.type)) {
_cacheCallbacks[request.type]!(secret); _cacheCallbacks[request.type]!(secret);
} }
onSecretStored.add(keyId);
} }
} }
} }