mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 03:25:12 +01:00
more impl of SessionBackup
This commit is contained in:
parent
f2fedae6aa
commit
3941af93d2
@ -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();
|
||||
|
@ -177,6 +177,17 @@ export class HomeServerApi {
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user