1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

Use static constructor that can be async (as looking up device is async)

Also fix static methods not to use this
This commit is contained in:
Bruno Windels
2020-04-01 19:00:57 +02:00
parent c2a0504980
commit ed210a4fb1

View File

@@ -107,20 +107,35 @@ const MODE_VERIFY_SELF_TRUSTED = 0x01; // We trust the master key
const MODE_VERIFY_SELF_UNTRUSTED = 0x02; // We do not trust the master key const MODE_VERIFY_SELF_UNTRUSTED = 0x02; // We do not trust the master key
export class QRCodeData { export class QRCodeData {
constructor(request, client) { constructor(mode, sharedSecret, otherUserMasterKey, otherDeviceKey, buffer) {
this._sharedSecret = QRCodeData._generateSharedSecret(); this._sharedSecret = sharedSecret;
this._mode = QRCodeData._determineMode(request, client); this._mode = mode;
this._otherUserMasterKey = null; this._otherUserMasterKey = otherUserMasterKey;
this._otherDeviceKey = null; this._otherDeviceKey = otherDeviceKey;
if (this._mode === MODE_VERIFY_OTHER_USER) { this._buffer = buffer;
}
static async create(request, client) {
const sharedSecret = QRCodeData._generateSharedSecret();
const mode = QRCodeData._determineMode(request, client);
let otherUserMasterKey = null;
let otherDeviceKey = null;
if (mode === MODE_VERIFY_OTHER_USER) {
const otherUserCrossSigningInfo = const otherUserCrossSigningInfo =
client.getStoredCrossSigningForUser(request.otherUserId); client.getStoredCrossSigningForUser(request.otherUserId);
this._otherUserMasterKey = otherUserCrossSigningInfo.getId("master"); otherUserMasterKey = otherUserCrossSigningInfo.getId("master");
} else if (this._mode === MODE_VERIFY_SELF_TRUSTED) { } else if (mode === MODE_VERIFY_SELF_TRUSTED) {
this._otherDeviceKey = QRCodeData._getOtherDeviceKey(request, client); otherDeviceKey = await QRCodeData._getOtherDeviceKey(request, client);
} }
const qrData = QRCodeData._generateQrData(request, client, this._mode); const qrData = QRCodeData._generateQrData(
this._buffer = QRCodeData._generateBuffer(qrData); request, client, mode,
sharedSecret,
otherUserMasterKey,
otherDeviceKey,
);
const buffer = QRCodeData._generateBuffer(qrData);
return new QRCodeData(mode, sharedSecret,
otherUserMasterKey, otherDeviceKey, buffer);
} }
get buffer() { get buffer() {
@@ -149,16 +164,19 @@ export class QRCodeData {
static _generateSharedSecret() { static _generateSharedSecret() {
const secretBytes = new Uint8Array(11); const secretBytes = new Uint8Array(11);
global.crypto.getRandomValues(secretBytes); global.crypto.getRandomValues(secretBytes);
this._sharedSecret = encodeUnpaddedBase64(secretBytes); return encodeUnpaddedBase64(secretBytes);
} }
static _getOtherDeviceKey(request, client) { static async _getOtherDeviceKey(request, client) {
const myUserId = client.getUserId(); const myUserId = client.getUserId();
const myDevices = client.getStoredDevicesForUser(myUserId) || [];
const otherDevice = request.targetDevice; const otherDevice = request.targetDevice;
const otherDeviceId = otherDevice ? otherDevice.deviceId : null; const otherDeviceId = otherDevice ? otherDevice.deviceId : null;
const device = myDevices.find(d => d.deviceId === otherDeviceId); const device = await client.getStoredDevice(myUserId, otherDeviceId);
return device.getFingerprint(); if (!device) {
throw new Error("could not find device " + otherDeviceId);
}
const key = device.getFingerprint();
return key;
} }
static _determineMode(request, client) { static _determineMode(request, client) {
@@ -178,7 +196,9 @@ export class QRCodeData {
return mode; return mode;
} }
static _generateQrData(request, client, mode) { static _generateQrData(request, client, mode,
encodedSharedSecret, otherUserMasterKey, otherDeviceKey,
) {
const myUserId = client.getUserId(); const myUserId = client.getUserId();
const transactionId = request.channel.transactionId; const transactionId = request.channel.transactionId;
const qrData = { const qrData = {
@@ -188,7 +208,7 @@ export class QRCodeData {
transactionId, transactionId,
firstKeyB64: '', // worked out shortly firstKeyB64: '', // worked out shortly
secondKeyB64: '', // worked out shortly secondKeyB64: '', // worked out shortly
secretB64: this.encodedSharedSecret, secretB64: encodedSharedSecret,
}; };
const myCrossSigningInfo = client.getStoredCrossSigningForUser(myUserId); const myCrossSigningInfo = client.getStoredCrossSigningForUser(myUserId);
@@ -198,18 +218,17 @@ export class QRCodeData {
// First key is our master cross signing key // First key is our master cross signing key
qrData.firstKeyB64 = myMasterKey; qrData.firstKeyB64 = myMasterKey;
// Second key is the other user's master cross signing key // Second key is the other user's master cross signing key
qrData.secondKeyB64 = this._otherUserMasterKey; qrData.secondKeyB64 = otherUserMasterKey;
} else if (mode === MODE_VERIFY_SELF_TRUSTED) { } else if (mode === MODE_VERIFY_SELF_TRUSTED) {
// First key is our master cross signing key // First key is our master cross signing key
qrData.firstKeyB64 = myMasterKey; qrData.firstKeyB64 = myMasterKey;
qrData.secondKeyB64 = this._otherDeviceKey; qrData.secondKeyB64 = otherDeviceKey;
} else if (mode === MODE_VERIFY_SELF_UNTRUSTED) { } else if (mode === MODE_VERIFY_SELF_UNTRUSTED) {
// First key is our device's key // First key is our device's key
qrData.firstKeyB64 = client.getDeviceEd25519Key(); qrData.firstKeyB64 = client.getDeviceEd25519Key();
// Second key is what we think our master cross signing key is // Second key is what we think our master cross signing key is
qrData.secondKeyB64 = myMasterKey; qrData.secondKeyB64 = myMasterKey;
} }
return qrData; return qrData;
} }