feat: Add methods to load all room keys from online key backup

This makes it possible to load
and sync all room keys
right after the bootstrap if the
app wants to do this.
This commit is contained in:
Krille 2023-10-19 16:44:27 +02:00 committed by Krille-chan
parent 3c237f4984
commit 4e310f16d5
3 changed files with 56 additions and 5 deletions

View File

@ -667,6 +667,31 @@ class KeyManager {
}
}
/// Loads and stores all keys from the online key backup. This may take a
/// while for older and big accounts.
Future<void> loadAllKeys() async {
final info = await getRoomKeysBackupInfo();
final ret = await client.getRoomKeys(info.version);
await loadFromResponse(ret);
}
/// Loads all room keys for a single room and stores them. This may take a
/// while for older and big rooms.
Future<void> loadAllKeysFromRoom(String roomId) async {
final info = await getRoomKeysBackupInfo();
final ret = await client.getRoomKeysByRoomId(roomId, info.version);
final keys = RoomKeys.fromJson({
'rooms': {
roomId: {
'sessions': ret.sessions.map((k, s) => MapEntry(k, s.toJson())),
},
},
});
await loadFromResponse(keys);
}
/// Loads a single key for the specified room from the online key backup
/// and stores it.
Future<void> loadSingleKey(String roomId, String sessionId) async {
final info = await getRoomKeysBackupInfo();
final ret =

View File

@ -67,9 +67,35 @@ void main() {
.request(client.getRoomById(roomId)!, sessionId, senderKey);
expect(
client.encryption!.keyManager
.getInboundGroupSession(roomId, sessionId) !=
null,
true);
.getInboundGroupSession(roomId, sessionId)
?.sessionId,
sessionId);
});
test('Load all Room Keys', () async {
if (!olmEnabled) return;
final keyManager = client.encryption!.keyManager;
const roomId = '!getroomkeys726s6s6q:example.com';
const sessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU';
expect(keyManager.getInboundGroupSession(roomId, sessionId), null);
await client.encryption!.keyManager.loadAllKeysFromRoom(roomId);
expect(
keyManager.getInboundGroupSession(roomId, sessionId)?.sessionId,
sessionId,
);
});
test('Load all Keys', () async {
if (!olmEnabled) return;
final keyManager = client.encryption!.keyManager;
const roomId = '!getallkeys726s6s6q:example.com';
const sessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU';
expect(keyManager.getInboundGroupSession(roomId, sessionId), null);
await client.encryption!.keyManager.loadAllKeys();
expect(
keyManager.getInboundGroupSession(roomId, sessionId)?.sessionId,
sessionId,
);
});
test('upload key', () async {

View File

@ -1936,7 +1936,7 @@ class FakeMatrixApi extends BaseClient {
'mac': 'QzKV/fgAs4U',
},
},
'/client/v3/room_keys/keys/${Uri.encodeComponent('!726s6s6q:example.com')}?version=5':
'/client/v3/room_keys/keys/${Uri.encodeComponent('!getroomkeys726s6s6q:example.com')}?version=5':
(var req) => {
'sessions': {
'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU': {
@ -1955,7 +1955,7 @@ class FakeMatrixApi extends BaseClient {
},
'/client/v3/room_keys/keys?version=5': (var req) => {
'rooms': {
'!726s6s6q:example.com': {
'!getallkeys726s6s6q:example.com': {
'sessions': {
'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU': {
'first_message_index': 0,