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 31a52cb commit 1c91b3d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 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
24 changes: 24 additions & 0 deletions test/encryption/online_key_backup_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@ void main() {
true);
});

test('Load all Room Keys', () async {
if (!olmEnabled) return;
const roomId = '!726s6s6q:example.com';
const sessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU';
await client.encryption!.keyManager.loadAllKeysFromRoom(roomId);
expect(
client.encryption!.keyManager
.getInboundGroupSession(roomId, sessionId) !=
null,
true);
});

test('Load all Keys', () async {
if (!olmEnabled) return;
const roomId = '!726s6s6q:example.com';
const sessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU';
await client.encryption!.keyManager.loadAllKeys();
expect(
client.encryption!.keyManager
.getInboundGroupSession(roomId, sessionId) !=
null,
true);
});

test('upload key', () async {
if (!olmEnabled) return;
final session = olm.OutboundGroupSession();
Expand Down

0 comments on commit 1c91b3d

Please sign in to comment.