more impl of SessionBackup

This commit is contained in:
Bruno Windels 2020-09-17 14:19:57 +02:00
parent f2fedae6aa
commit 3941af93d2
2 changed files with 19 additions and 8 deletions

View file

@ -25,32 +25,32 @@ export class SessionBackup {
}
async getSession(roomId, sessionId) {
const sessionResponse = await this._hsApi.roomKey(this._backupInfo.version, roomId, sessionId).response();
let sessionInfo;
const sessionResponse = await this._hsApi.roomKeyForRoomAndSession(this._backupInfo.version, roomId, sessionId).response();
const decryption = new this._olm.PkDecryption();
try {
decryption.init_with_private_key(this._privateKey);
sessionInfo = this._decryption.decrypt(
const sessionInfo = this._decryption.decrypt(
sessionResponse.session_data.ephemeral,
sessionResponse.session_data.mac,
sessionResponse.session_data.ciphertext,
);
return JSON.parse(sessionInfo);
} finally {
decryption.free();
}
return JSON.parse(sessionInfo);
}
static async fromSecretStorage({olm, secretStorage, hsApi}) {
const backupInfo = await hsApi.roomKeysVersion().response();
const base64PrivateKey = await secretStorage.readSecret("m.megolm_backup.v1");
if (base64PrivateKey) {
const privateKey = base64.decode(base64PrivateKey);
const backupInfo = await hsApi.roomKeysVersion().response();
const expectedPubKey = backupInfo.auth_data.public_key;
const decryption = new olm.PkDecryption();
try {
const pubKey = decryption.init_with_private_key(this._privateKey);
if (pubKey !== backupInfo.auth_data.public_key) {
throw new Error(`Bad backup key, public key does not match. Calculated ${pubKey} but expected ${backupInfo.auth_data.public_key}`);
const pubKey = decryption.init_with_private_key(privateKey);
if (pubKey !== expectedPubKey) {
throw new Error(`Bad backup key, public key does not match. Calculated ${pubKey} but expected ${expectedPubKey}`);
}
} finally {
decryption.free();

View file

@ -176,6 +176,17 @@ export class HomeServerApi {
sendToDevice(type, payload, txnId, options = null) {
return this._put(`/sendToDevice/${encodeURIComponent(type)}/${encodeURIComponent(txnId)}`, null, payload, options);
}
roomKeysVersion(version = null, options = null) {
if (!version) {
version = "";
}
return this._get(`/room_keys/version/${encodeURIComponent(version)}`, null, null, options);
}
roomKeyForRoomAndSession(version, roomId, sessionId, options = null) {
return this._get(`/room_keys/keys/${encodeURIComponent(roomId)}/${encodeURIComponent(sessionId)}`, {version}, null, options);
}
get mediaRepository() {
return this._mediaRepository;