From 5a8ab00a96980dcfe69073101a74887b704ab295 Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Thu, 26 Sep 2019 16:27:29 +1000 Subject: [PATCH 1/2] Rename pairing getters to specify secondary or primary. Add getGrantAuthForPrimary. --- app/sql.js | 24 ++++++++++++++++-------- js/background.js | 4 +++- js/modules/data.js | 37 ++++++++++++------------------------- libloki/storage.js | 44 ++++++++++++++++++++++++++++++++++++++------ 4 files changed, 69 insertions(+), 40 deletions(-) diff --git a/app/sql.js b/app/sql.js index 3d2fe1bae..7c95b94c5 100644 --- a/app/sql.js +++ b/app/sql.js @@ -75,7 +75,8 @@ module.exports = { createOrUpdatePairingAuthorisation, removePairingAuthorisationForSecondaryPubKey, - getAuthorisationForPubKey, + getAuthorisationForSecondaryPubKey, + getGrantAuthorisationsForPrimaryPubKey, getSecondaryDevicesFor, getPrimaryDeviceFor, getPairedDevicesFor, @@ -1369,7 +1370,7 @@ async function removeAllSignedPreKeys() { } const PAIRING_AUTHORISATIONS_TABLE = 'pairingAuthorisations'; -async function getAuthorisationForPubKey(pubKey, options) { +async function getAuthorisationForSecondaryPubKey(pubKey, options) { const granted = options && options.granted; let filter = ''; if (granted) { @@ -1389,6 +1390,16 @@ async function getAuthorisationForPubKey(pubKey, options) { return jsonToObject(row.json); } +async function getGrantAuthorisationsForPrimaryPubKey(primaryDevicePubKey) { + const rows = await db.all( + `SELECT json FROM ${PAIRING_AUTHORISATIONS_TABLE} WHERE primaryDevicePubKey = $primaryDevicePubKey AND isGranted = 1 ORDER BY secondaryDevicePubKey ASC;`, + { + $primaryDevicePubKey: primaryDevicePubKey, + } + ); + return map(rows, row => jsonToObject(row.json)); +} + async function createOrUpdatePairingAuthorisation(data) { const { primaryDevicePubKey, secondaryDevicePubKey, grantSignature } = data; @@ -1423,13 +1434,10 @@ async function removePairingAuthorisationForSecondaryPubKey(pubKey) { } async function getSecondaryDevicesFor(primaryDevicePubKey) { - const rows = await db.all( - `SELECT secondaryDevicePubKey FROM ${PAIRING_AUTHORISATIONS_TABLE} WHERE primaryDevicePubKey = $primaryDevicePubKey AND isGranted = 1 ORDER BY secondaryDevicePubKey ASC;`, - { - $primaryDevicePubKey: primaryDevicePubKey, - } + const authorisations = await getGrantAuthorisationsForPrimaryPubKey( + primaryDevicePubKey ); - return map(rows, row => row.secondaryDevicePubKey); + return map(authorisations, row => row.secondaryDevicePubKey); } async function getPrimaryDeviceFor(secondaryDevicePubKey) { diff --git a/js/background.js b/js/background.js index b243a91b7..761ce402d 100644 --- a/js/background.js +++ b/js/background.js @@ -828,7 +828,9 @@ Whisper.events.on('devicePairingRequestRejected', async pubKey => { await window.libloki.storage.removeContactPreKeyBundle(pubKey); - await window.libloki.storage.removePairingAuthorisationForSecondaryPubKey(pubKey); + await window.libloki.storage.removePairingAuthorisationForSecondaryPubKey( + pubKey + ); }); } diff --git a/js/modules/data.js b/js/modules/data.js index c2dca9695..91a5523c0 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -91,8 +91,9 @@ module.exports = { createOrUpdatePairingAuthorisation, removePairingAuthorisationForSecondaryPubKey, - getGrantAuthorisationForPubKey, - getAuthorisationForPubKey, + getGrantAuthorisationForSecondaryPubKey, + getAuthorisationForSecondaryPubKey, + getGrantAuthorisationsForPrimaryPubKey, getSecondaryDevicesFor, getPrimaryDeviceFor, getPairedDevicesFor, @@ -609,38 +610,24 @@ async function createOrUpdatePairingAuthorisation(data) { } async function removePairingAuthorisationForSecondaryPubKey(pubKey) { - if (!pubKey){ + if (!pubKey) { return; } await channels.removePairingAuthorisationForSecondaryPubKey(pubKey); } -async function getGrantAuthorisationForPubKey(pubKey) { - const authorisation = await channels.getAuthorisationForPubKey(pubKey, { +async function getGrantAuthorisationForSecondaryPubKey(pubKey) { + return channels.getAuthorisationForSecondaryPubKey(pubKey, { granted: true, }); - if (!authorisation) { - return null; - } - return { - ...authorisation, - requestSignature: base64ToArrayBuffer(authorisation.requestSignature), - grantSignature: base64ToArrayBuffer(authorisation.grantSignature), - }; } -async function getAuthorisationForPubKey(pubKey) { - const authorisation = await channels.getAuthorisationForPubKey(pubKey); - if (!authorisation) { - return null; - } - return { - ...authorisation, - requestSignature: base64ToArrayBuffer(authorisation.requestSignature), - grantSignature: authorisation.grantSignature - ? base64ToArrayBuffer(authorisation.grantSignature) - : null, - }; +async function getGrantAuthorisationsForPrimaryPubKey(pubKey) { + return channels.getGrantAuthorisationsForPrimaryPubKey(pubKey); +} + +function getAuthorisationForSecondaryPubKey(pubKey) { + return channels.getAuthorisationForSecondaryPubKey(pubKey); } function getSecondaryDevicesFor(primaryDevicePubKey) { diff --git a/libloki/storage.js b/libloki/storage.js index b83545827..25237cd02 100644 --- a/libloki/storage.js +++ b/libloki/storage.js @@ -1,4 +1,4 @@ -/* global window, libsignal, textsecure */ +/* global window, libsignal, textsecure, Signal */ // eslint-disable-next-line func-names (function() { @@ -118,15 +118,47 @@ } function removePairingAuthorisationForSecondaryPubKey(pubKey) { - return window.Signal.Data.removePairingAuthorisationForSecondaryPubKey(pubKey); + return window.Signal.Data.removePairingAuthorisationForSecondaryPubKey( + pubKey + ); } - function getGrantAuthorisationForSecondaryPubKey(secondaryPubKey) { - return window.Signal.Data.getGrantAuthorisationForPubKey(secondaryPubKey); + // Transforms signatures from base64 to ArrayBuffer! + async function getGrantAuthorisationForSecondaryPubKey(secondaryPubKey) { + const authorisation = await window.Signal.Data.getGrantAuthorisationForSecondaryPubKey( + secondaryPubKey + ); + if (!authorisation) { + return null; + } + return { + ...authorisation, + requestSignature: Signal.Crypto.base64ToArrayBuffer( + authorisation.requestSignature + ), + grantSignature: Signal.Crypto.base64ToArrayBuffer( + authorisation.grantSignature + ), + }; } - function getAuthorisationForSecondaryPubKey(secondaryPubKey) { - return window.Signal.Data.getAuthorisationForPubKey(secondaryPubKey); + // Transforms signatures from base64 to ArrayBuffer! + async function getAuthorisationForSecondaryPubKey(secondaryPubKey) { + const authorisation = await window.Signal.Data.getAuthorisationForSecondaryPubKey( + secondaryPubKey + ); + if (!authorisation) { + return null; + } + return { + ...authorisation, + requestSignature: Signal.Crypto.base64ToArrayBuffer( + authorisation.requestSignature + ), + grantSignature: authorisation.grantSignature + ? Signal.Crypto.base64ToArrayBuffer(authorisation.grantSignature) + : null, + }; } function getSecondaryDevicesFor(primaryDevicePubKey) { From de6e4e4920a88bbdade12c32923c4ca3d037dd5f Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Thu, 26 Sep 2019 16:27:53 +1000 Subject: [PATCH 2/2] Add updateOurDeviceMapping that wraps setOurDeviceMapping --- js/modules/loki_file_server_api.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/js/modules/loki_file_server_api.js b/js/modules/loki_file_server_api.js index 5b110e199..e59a80a64 100644 --- a/js/modules/loki_file_server_api.js +++ b/js/modules/loki_file_server_api.js @@ -1,3 +1,7 @@ +/* global storage: false */ +/* global libloki: false */ +/* global Signal: false */ + const LokiAppDotNetAPI = require('./loki_app_dot_net_api'); const DEVICE_MAPPING_ANNOTATION_KEY = 'network.loki.messenger.devicemapping'; @@ -21,7 +25,22 @@ class LokiFileServerAPI { ); } - setOurDeviceMapping(authorisations, isPrimary) { + async updateOurDeviceMapping() { + const isPrimary = !storage.get('isSecondaryDevice'); + let authorisations; + if (isPrimary) { + authorisations = await Signal.Data.getGrantAuthorisationsForPrimaryPubKey( + this.ourKey + ); + } else { + authorisations = [ + await libloki.storage.getGrantAuthorisationForSecondaryPubKey(this.ourKey), + ]; + } + return this._setOurDeviceMapping(authorisations, isPrimary); + } + + _setOurDeviceMapping(authorisations, isPrimary) { const content = { isPrimary: isPrimary ? '1' : '0', authorisations,