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) {
|
async getSession(roomId, sessionId) {
|
||||||
const sessionResponse = await this._hsApi.roomKey(this._backupInfo.version, roomId, sessionId).response();
|
const sessionResponse = await this._hsApi.roomKeyForRoomAndSession(this._backupInfo.version, roomId, sessionId).response();
|
||||||
let sessionInfo;
|
|
||||||
const decryption = new this._olm.PkDecryption();
|
const decryption = new this._olm.PkDecryption();
|
||||||
try {
|
try {
|
||||||
decryption.init_with_private_key(this._privateKey);
|
decryption.init_with_private_key(this._privateKey);
|
||||||
sessionInfo = this._decryption.decrypt(
|
const sessionInfo = this._decryption.decrypt(
|
||||||
sessionResponse.session_data.ephemeral,
|
sessionResponse.session_data.ephemeral,
|
||||||
sessionResponse.session_data.mac,
|
sessionResponse.session_data.mac,
|
||||||
sessionResponse.session_data.ciphertext,
|
sessionResponse.session_data.ciphertext,
|
||||||
);
|
);
|
||||||
|
return JSON.parse(sessionInfo);
|
||||||
} finally {
|
} finally {
|
||||||
decryption.free();
|
decryption.free();
|
||||||
}
|
}
|
||||||
return JSON.parse(sessionInfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async fromSecretStorage({olm, secretStorage, hsApi}) {
|
static async fromSecretStorage({olm, secretStorage, hsApi}) {
|
||||||
const backupInfo = await hsApi.roomKeysVersion().response();
|
|
||||||
const base64PrivateKey = await secretStorage.readSecret("m.megolm_backup.v1");
|
const base64PrivateKey = await secretStorage.readSecret("m.megolm_backup.v1");
|
||||||
if (base64PrivateKey) {
|
if (base64PrivateKey) {
|
||||||
const privateKey = base64.decode(base64PrivateKey);
|
const privateKey = base64.decode(base64PrivateKey);
|
||||||
|
const backupInfo = await hsApi.roomKeysVersion().response();
|
||||||
|
const expectedPubKey = backupInfo.auth_data.public_key;
|
||||||
const decryption = new olm.PkDecryption();
|
const decryption = new olm.PkDecryption();
|
||||||
try {
|
try {
|
||||||
const pubKey = decryption.init_with_private_key(this._privateKey);
|
const pubKey = decryption.init_with_private_key(privateKey);
|
||||||
if (pubKey !== backupInfo.auth_data.public_key) {
|
if (pubKey !== expectedPubKey) {
|
||||||
throw new Error(`Bad backup key, public key does not match. Calculated ${pubKey} but expected ${backupInfo.auth_data.public_key}`);
|
throw new Error(`Bad backup key, public key does not match. Calculated ${pubKey} but expected ${expectedPubKey}`);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
decryption.free();
|
decryption.free();
|
||||||
|
@ -176,6 +176,17 @@ export class HomeServerApi {
|
|||||||
sendToDevice(type, payload, txnId, options = null) {
|
sendToDevice(type, payload, txnId, options = null) {
|
||||||
return this._put(`/sendToDevice/${encodeURIComponent(type)}/${encodeURIComponent(txnId)}`, null, payload, options);
|
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() {
|
get mediaRepository() {
|
||||||
return this._mediaRepository;
|
return this._mediaRepository;
|
||||||
|
Loading…
Reference in New Issue
Block a user