fix: also delete db on logout

we already cleared it out, but to change the db enc pw on the app we need to delete it completely first
This commit is contained in:
td 2024-07-26 22:19:02 +05:30
parent 87ce08d394
commit 22694a81d8
No known key found for this signature in database
GPG Key ID: 62A30523D4D6CE28
10 changed files with 63 additions and 18 deletions

View File

@ -1799,6 +1799,7 @@ class Client extends MatrixApi {
} catch (e, s) {
Logs().e('Unable to clear database', e, s);
} finally {
await database?.delete();
_database = null;
}

View File

@ -335,6 +335,6 @@ abstract class DatabaseApi {
Future<CachedPresence?> getPresence(String userId);
/// Deletes the whole database. The database needs to be created again after
/// this. Used for migration only.
/// this.
Future<void> delete();
}

View File

@ -83,8 +83,14 @@ class BoxCollection with ZoneTransactionMixin {
return _db.close();
}
static Future<void> delete(String path, [dynamic factory]) =>
(factory ?? window.indexedDB!).deleteDatabase(path);
@Deprecated('use collection.deleteDatabase now')
static Future<void> delete(String name, [dynamic factory]) =>
(factory ?? window.indexedDB!).deleteDatabase(name);
Future<void> deleteDatabase(String name, [dynamic factory]) async {
await close();
await (factory ?? window.indexedDB).deleteDatabase(name);
}
}
class Box<V> {

View File

@ -54,6 +54,7 @@ import 'package:matrix/src/database/database_file_storage_stub.dart'
class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
static const int version = 9;
final String name;
late BoxCollection _collection;
late Box<String> _clientBox;
late Box<Map> _accountDataBox;
@ -1623,10 +1624,13 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
}
@override
Future<void> delete() => BoxCollection.delete(
name,
sqfliteFactory ?? idbFactory,
);
Future<void> delete() async {
// database?.path is null on web
await _collection.deleteDatabase(
database?.path ?? name,
sqfliteFactory ?? idbFactory,
);
}
@override
Future<void> markUserProfileAsOutdated(userId) async {

View File

@ -68,8 +68,14 @@ class BoxCollection with ZoneTransactionMixin {
Future<void> close() => _db.close();
@Deprecated('use collection.deleteDatabase now')
static Future<void> delete(String path, [dynamic factory]) =>
(factory ?? databaseFactory).deleteDatabase(path);
Future<void> deleteDatabase(String path, [dynamic factory]) async {
await close();
await (factory ?? databaseFactory).deleteDatabase(path);
}
}
class Box<V> {

View File

@ -41,5 +41,6 @@ dev_dependencies:
file: ">=6.1.1 <8.0.0"
import_sorter: ^4.6.0
lints: ^4.0.0
path: ^1.9.0
sqflite_common_ffi: 2.3.2+1 # v2.3.3 doesn't support dart v3.2.x
test: ^1.15.7

View File

@ -100,9 +100,9 @@ void main() {
});
test(
'Box.delete',
'Collection.deleteDatabase',
() async {
await BoxCollection.delete(
await collection.deleteDatabase(
db?.path ?? '',
isWeb ? null : databaseFactoryFfi,
);

View File

@ -18,11 +18,13 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:canonical_json/canonical_json.dart';
import 'package:collection/collection.dart';
import 'package:olm/olm.dart' as olm;
import 'package:path/path.dart' show join;
import 'package:test/test.dart';
import 'package:matrix/matrix.dart';
@ -31,16 +33,35 @@ import 'fake_client.dart';
import 'fake_database.dart';
void main() {
late Client matrix;
// key @test:fakeServer.notExisting
const pickledOlmAccount =
'N2v1MkIFGcl0mQpo2OCwSopxPQJ0wnl7oe7PKiT4141AijfdTIhRu+ceXzXKy3Kr00nLqXtRv7kid6hU4a+V0rfJWLL0Y51+3Rp/ORDVnQy+SSeo6Fn4FHcXrxifJEJ0djla5u98fBcJ8BSkhIDmtXRPi5/oJAvpiYn+8zMjFHobOeZUAxYR0VfQ9JzSYBsSovoQ7uFkNks1M4EDUvHtuyg3RxViwdNxs3718fyAqQ/VSwbXsY0Nl+qQbF+nlVGHenGqk5SuNl1P6e1PzZxcR0IfXA94Xij1Ob5gDv5YH4UCn9wRMG0abZsQP0YzpDM0FLaHSCyo9i5JD/vMlhH+nZWrgAzPPCTNGYewNV8/h3c+VyJh8ZTx/fVi6Yq46Fv+27Ga2ETRZ3Qn+Oyx6dLBjnBZ9iUvIhqpe2XqaGA1PopOz8iDnaZitw';
const identityKey = '7rvl3jORJkBiK4XX1e5TnGnqz068XfYJ0W++Ml63rgk';
const fingerprintKey = 'gjL//fyaFHADt9KBADGag8g7F8Up78B/K1zXeiEPLJo';
group('client path', () {
late Client clientOnPath;
final dbPath = join(Directory.current.path, 'test.sqlite');
setUp(() async {
expect(await File(dbPath).exists(), false);
clientOnPath = await getClient(
databasePath: dbPath,
);
expect(await File(dbPath).exists(), true);
});
test('logout', () async {
expect(await File(dbPath).exists(), true);
await clientOnPath.logout();
expect(await File(dbPath).exists(), false);
});
});
/// All Tests related to the Login
group('Client', tags: 'olm', () {
group('client mem', tags: 'olm', () {
late Client matrix;
Logs().level = Level.error;
/// Check if all Elements get created

View File

@ -26,12 +26,16 @@ const ssssKey = 'EsT9 RzbW VhPW yqNp cC7j ViiW 5TZB LuY4 ryyv 9guN Ysmr WDPH';
const pickledOlmAccount =
'N2v1MkIFGcl0mQpo2OCwSopxPQJ0wnl7oe7PKiT4141AijfdTIhRu+ceXzXKy3Kr00nLqXtRv7kid6hU4a+V0rfJWLL0Y51+3Rp/ORDVnQy+SSeo6Fn4FHcXrxifJEJ0djla5u98fBcJ8BSkhIDmtXRPi5/oJAvpiYn+8zMjFHobOeZUAxYR0VfQ9JzSYBsSovoQ7uFkNks1M4EDUvHtuyg3RxViwdNxs3718fyAqQ/VSwbXsY0Nl+qQbF+nlVGHenGqk5SuNl1P6e1PzZxcR0IfXA94Xij1Ob5gDv5YH4UCn9wRMG0abZsQP0YzpDM0FLaHSCyo9i5JD/vMlhH+nZWrgAzPPCTNGYewNV8/h3c+VyJh8ZTx/fVi6Yq46Fv+27Ga2ETRZ3Qn+Oyx6dLBjnBZ9iUvIhqpe2XqaGA1PopOz8iDnaZitw';
Future<Client> getClient(
{Duration sendTimelineEventTimeout = const Duration(minutes: 1)}) async {
/// only use `path` if you explicitly if you need a db on path instead of in mem
Future<Client> getClient({
Duration sendTimelineEventTimeout = const Duration(minutes: 1),
String? databasePath,
}) async {
final client = Client(
'testclient',
httpClient: FakeMatrixApi(),
databaseBuilder: getDatabase,
databaseBuilder: (client) =>
getDatabase(client, databasePath: databasePath),
onSoftLogout: (client) => client.refreshAccessToken(),
sendTimelineEventTimeout: sendTimelineEventTimeout,
);

View File

@ -22,7 +22,8 @@ import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:matrix/matrix.dart';
Future<DatabaseApi> getDatabase(Client? c) => getMatrixSdkDatabase(c);
Future<DatabaseApi> getDatabase(Client? c, {String? databasePath}) =>
getMatrixSdkDatabase(c, path: databasePath);
bool hiveInitialized = false;
@ -44,9 +45,10 @@ Future<HiveCollectionsDatabase> getHiveCollectionsDatabase(Client? c) async {
}
// ignore: deprecated_member_use_from_same_package
Future<MatrixSdkDatabase> getMatrixSdkDatabase(Client? c) async {
Future<MatrixSdkDatabase> getMatrixSdkDatabase(Client? c,
{String? path}) async {
final database = await databaseFactoryFfi.openDatabase(
':memory:',
path ?? ':memory:',
options: OpenDatabaseOptions(singleInstance: false),
);
final db = MatrixSdkDatabase(