FurryChat/lib/utils/famedlysdk_store.dart

297 lines
9 KiB
Dart
Raw Normal View History

2020-10-15 13:51:24 +00:00
import 'dart:async';
2020-01-01 18:10:13 +00:00
import 'dart:convert';
2020-10-15 13:51:24 +00:00
import 'dart:core';
2020-01-01 18:10:13 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2020-01-26 11:17:54 +00:00
import 'package:flutter/foundation.dart';
2020-10-15 13:51:24 +00:00
import 'package:flutter/material.dart';
2020-01-26 11:17:54 +00:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:localstorage/localstorage.dart';
2020-05-13 13:58:59 +00:00
import 'package:olm/olm.dart' as olm; // needed for migration
2020-10-15 13:51:24 +00:00
import 'package:path_provider/path_provider.dart';
2020-05-13 13:58:59 +00:00
import 'package:random_string/random_string.dart';
2020-10-15 13:51:24 +00:00
import './database/shared.dart';
import 'platform_infos.dart';
2020-10-13 10:20:13 +00:00
Future<LocalStorage> getLocalStorage() async {
final directory = PlatformInfos.isBetaDesktop
? await getApplicationSupportDirectory()
: await getApplicationDocumentsDirectory();
final localStorage = LocalStorage('LocalStorage', directory.path);
await localStorage.ready;
return localStorage;
}
Future<Database> getDatabase(Client client) async {
2020-07-20 15:33:52 +00:00
while (_generateDatabaseLock) {
await Future.delayed(Duration(milliseconds: 50));
2020-05-13 13:58:59 +00:00
}
2020-07-20 15:33:52 +00:00
_generateDatabaseLock = true;
try {
if (_db != null) return _db;
final store = Store();
var password = await store.getItem('database-password');
var needMigration = false;
if (password == null || password.isEmpty) {
needMigration = true;
password = randomString(255);
}
_db = await constructDb(
logStatements: false,
filename: 'moor.sqlite',
password: password,
);
2020-10-04 12:04:45 +00:00
// Check if database is open:
debugPrint((await _db.customSelect('SELECT 1').get()).toString());
2020-07-20 15:33:52 +00:00
if (needMigration) {
2020-10-04 10:32:29 +00:00
debugPrint('[Moor] Start migration');
2020-07-20 15:33:52 +00:00
await migrate(client.clientName, _db, store);
await store.setItem('database-password', password);
}
return _db;
} finally {
_generateDatabaseLock = false;
2020-05-13 13:58:59 +00:00
}
}
2020-01-01 18:10:13 +00:00
Database _db;
2020-07-20 15:33:52 +00:00
bool _generateDatabaseLock = false;
2020-05-13 13:58:59 +00:00
Future<void> migrate(String clientName, Database db, Store store) async {
debugPrint('[Store] attempting old migration to moor...');
final oldKeys = await store.getAllItems();
if (oldKeys == null || oldKeys.isEmpty) {
debugPrint('[Store] empty store!');
return; // we are done!
}
final credentialsStr = oldKeys[clientName];
if (credentialsStr == null || credentialsStr.isEmpty) {
debugPrint('[Store] no credentials found!');
return; // no credentials
}
final Map<String, dynamic> credentials = json.decode(credentialsStr);
if (!credentials.containsKey('homeserver') ||
!credentials.containsKey('token') ||
!credentials.containsKey('userID')) {
debugPrint('[Store] invalid credentials!');
return; // invalid old store, we are done, too!
}
var clientId = 0;
final oldClient = await db.getClient(clientName);
if (oldClient == null) {
clientId = await db.insertClient(
clientName,
credentials['homeserver'],
credentials['token'],
credentials['userID'],
credentials['deviceID'],
credentials['deviceName'],
null,
credentials['olmAccount'],
);
} else {
clientId = oldClient.clientId;
await db.updateClient(
credentials['homeserver'],
credentials['token'],
credentials['userID'],
credentials['deviceID'],
credentials['deviceName'],
null,
credentials['olmAccount'],
clientId,
);
}
await db.clearCache(clientId);
debugPrint('[Store] Inserted/updated client, clientId = ${clientId}');
await db.transaction(() async {
// alright, we stored / updated the client and have the account ID, time to import everything else!
// user_device_keys and user_device_keys_key
debugPrint('[Store] Migrating user device keys...');
final deviceKeysListString = oldKeys['${clientName}.user_device_keys'];
if (deviceKeysListString != null && deviceKeysListString.isNotEmpty) {
Map<String, dynamic> rawUserDeviceKeys =
json.decode(deviceKeysListString);
for (final entry in rawUserDeviceKeys.entries) {
final map = entry.value;
await db.storeUserDeviceKeysInfo(
clientId, map['user_id'], map['outdated']);
for (final rawKey in map['device_keys'].entries) {
final jsonVaue = rawKey.value;
await db.storeUserDeviceKey(
clientId,
jsonVaue['user_id'],
jsonVaue['device_id'],
json.encode(jsonVaue),
jsonVaue['verified'],
jsonVaue['blocked']);
}
}
}
for (final entry in oldKeys.entries) {
final key = entry.key;
final value = entry.value;
if (value == null || value.isEmpty) {
continue;
}
// olm_sessions
final olmSessionsMatch =
RegExp(r'^\/clients\/([^\/]+)\/olm-sessions$').firstMatch(key);
if (olmSessionsMatch != null) {
if (olmSessionsMatch[1] != credentials['deviceID']) {
continue;
}
debugPrint('[Store] migrating olm sessions...');
final identityKey = json.decode(value);
for (final olmKey in identityKey.entries) {
final identKey = olmKey.key;
final sessions = olmKey.value;
for (final pickle in sessions) {
var sess = olm.Session();
sess.unpickle(credentials['userID'], pickle);
await db.storeOlmSession(
2020-06-25 14:29:06 +00:00
clientId, identKey, sess.session_id(), pickle, null);
2020-05-13 13:58:59 +00:00
sess?.free();
}
}
}
// outbound_group_sessions
final outboundGroupSessionsMatch = RegExp(
r'^\/clients\/([^\/]+)\/rooms\/([^\/]+)\/outbound_group_session$')
.firstMatch(key);
if (outboundGroupSessionsMatch != null) {
if (outboundGroupSessionsMatch[1] != credentials['deviceID']) {
continue;
}
final pickle = value;
final roomId = outboundGroupSessionsMatch[2];
debugPrint(
'[Store] Migrating outbound group sessions for room ${roomId}...');
final devicesString = oldKeys[
'/clients/${outboundGroupSessionsMatch[1]}/rooms/${roomId}/outbound_group_session_devices'];
var devices = <String>[];
if (devicesString != null) {
devices = List<String>.from(json.decode(devicesString));
}
await db.storeOutboundGroupSession(
clientId,
roomId,
pickle,
json.encode(devices),
2020-10-04 15:37:43 +00:00
DateTime.now().millisecondsSinceEpoch,
0,
);
2020-05-13 13:58:59 +00:00
}
// session_keys
final sessionKeysMatch =
RegExp(r'^\/clients\/([^\/]+)\/rooms\/([^\/]+)\/session_keys$')
.firstMatch(key);
if (sessionKeysMatch != null) {
if (sessionKeysMatch[1] != credentials['deviceID']) {
continue;
}
final roomId = sessionKeysMatch[2];
debugPrint('[Store] Migrating session keys for room ${roomId}...');
final map = json.decode(value);
for (final entry in map.entries) {
await db.storeInboundGroupSession(
clientId,
roomId,
entry.key,
entry.value['inboundGroupSession'],
json.encode(entry.value['content']),
2020-08-22 09:25:29 +00:00
json.encode(entry.value['indexes']),
null,
null);
2020-05-13 13:58:59 +00:00
}
}
}
});
}
// see https://github.com/mogol/flutter_secure_storage/issues/161#issuecomment-704578453
class AsyncMutex {
Completer<void> _completer;
Future<void> lock() async {
while (_completer != null) {
await _completer.future;
}
_completer = Completer<void>();
}
void unlock() {
assert(_completer != null);
final completer = _completer;
_completer = null;
completer.complete();
}
}
2020-05-13 13:58:59 +00:00
class Store {
2020-01-26 11:17:54 +00:00
final LocalStorage storage;
final FlutterSecureStorage secureStorage;
static final _mutex = AsyncMutex();
2020-01-01 18:10:13 +00:00
2020-05-13 13:58:59 +00:00
Store()
2020-01-30 13:45:35 +00:00
: storage = LocalStorage('LocalStorage'),
2020-09-26 18:27:15 +00:00
secureStorage = PlatformInfos.isMobile ? FlutterSecureStorage() : null;
2020-01-01 18:10:13 +00:00
2020-01-26 11:17:54 +00:00
Future<dynamic> getItem(String key) async {
2020-09-26 18:27:15 +00:00
if (!PlatformInfos.isMobile) {
2020-01-26 11:17:54 +00:00
await storage.ready;
try {
return await storage.getItem(key);
} catch (_) {
return null;
}
2020-01-26 11:17:54 +00:00
}
2020-03-29 10:06:25 +00:00
try {
await _mutex.lock();
2020-03-29 10:06:25 +00:00
return await secureStorage.read(key: key);
} catch (_) {
return null;
} finally {
_mutex.unlock();
2020-03-29 10:06:25 +00:00
}
2020-01-26 11:17:54 +00:00
}
Future<void> setItem(String key, String value) async {
2020-09-26 18:27:15 +00:00
if (!PlatformInfos.isMobile) {
2020-01-26 11:17:54 +00:00
await storage.ready;
return await storage.setItem(key, value);
}
2020-02-17 13:46:05 +00:00
if (value == null) {
return await secureStorage.delete(key: key);
} else {
try {
await _mutex.lock();
return await secureStorage.write(key: key, value: value);
} finally {
_mutex.unlock();
}
2020-02-17 13:46:05 +00:00
}
2020-01-26 11:17:54 +00:00
}
2020-05-13 13:58:59 +00:00
Future<Map<String, dynamic>> getAllItems() async {
2020-09-26 18:27:15 +00:00
if (!PlatformInfos.isMobile) {
2020-05-13 13:58:59 +00:00
try {
final rawStorage = await getLocalstorage('LocalStorage');
return json.decode(rawStorage);
} catch (_) {
return {};
2020-01-02 21:31:39 +00:00
}
2020-01-01 18:10:13 +00:00
}
2020-05-13 13:58:59 +00:00
try {
await _mutex.lock();
2020-05-13 13:58:59 +00:00
return await secureStorage.readAll();
} catch (_) {
return {};
} finally {
_mutex.unlock();
2020-01-02 11:27:02 +00:00
}
2020-03-29 10:06:25 +00:00
}
2020-01-01 18:10:13 +00:00
}