Skip to content

Commit

Permalink
feat: Add methods to load all room keys from online key backup
Browse files Browse the repository at this point in the history
This makes it possible to load
and sync all room keys
right after the bootstrap if the
app wants to do this.
  • Loading branch information
krille-chan committed Oct 20, 2023
1 parent 3c237f4 commit 4e310f1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
25 changes: 25 additions & 0 deletions lib/encryption/key_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
32 changes: 29 additions & 3 deletions test/encryption/online_key_backup_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions test/fake_matrix_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -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,
Expand Down

0 comments on commit 4e310f1

Please sign in to comment.