Merge pull request #530 from sachaaaaa/update_our_device_mapping

[multi-device] Add high-level function to update device mapping
pull/541/head
sachaaaaa 6 years ago committed by GitHub
commit 2544507084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -75,7 +75,8 @@ module.exports = {
createOrUpdatePairingAuthorisation, createOrUpdatePairingAuthorisation,
removePairingAuthorisationForSecondaryPubKey, removePairingAuthorisationForSecondaryPubKey,
getAuthorisationForPubKey, getAuthorisationForSecondaryPubKey,
getGrantAuthorisationsForPrimaryPubKey,
getSecondaryDevicesFor, getSecondaryDevicesFor,
getPrimaryDeviceFor, getPrimaryDeviceFor,
getPairedDevicesFor, getPairedDevicesFor,
@ -1369,7 +1370,7 @@ async function removeAllSignedPreKeys() {
} }
const PAIRING_AUTHORISATIONS_TABLE = 'pairingAuthorisations'; const PAIRING_AUTHORISATIONS_TABLE = 'pairingAuthorisations';
async function getAuthorisationForPubKey(pubKey, options) { async function getAuthorisationForSecondaryPubKey(pubKey, options) {
const granted = options && options.granted; const granted = options && options.granted;
let filter = ''; let filter = '';
if (granted) { if (granted) {
@ -1389,6 +1390,16 @@ async function getAuthorisationForPubKey(pubKey, options) {
return jsonToObject(row.json); 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) { async function createOrUpdatePairingAuthorisation(data) {
const { primaryDevicePubKey, secondaryDevicePubKey, grantSignature } = data; const { primaryDevicePubKey, secondaryDevicePubKey, grantSignature } = data;
@ -1423,13 +1434,10 @@ async function removePairingAuthorisationForSecondaryPubKey(pubKey) {
} }
async function getSecondaryDevicesFor(primaryDevicePubKey) { async function getSecondaryDevicesFor(primaryDevicePubKey) {
const rows = await db.all( const authorisations = await getGrantAuthorisationsForPrimaryPubKey(
`SELECT secondaryDevicePubKey FROM ${PAIRING_AUTHORISATIONS_TABLE} WHERE primaryDevicePubKey = $primaryDevicePubKey AND isGranted = 1 ORDER BY secondaryDevicePubKey ASC;`, primaryDevicePubKey
{
$primaryDevicePubKey: primaryDevicePubKey,
}
); );
return map(rows, row => row.secondaryDevicePubKey); return map(authorisations, row => row.secondaryDevicePubKey);
} }
async function getPrimaryDeviceFor(secondaryDevicePubKey) { async function getPrimaryDeviceFor(secondaryDevicePubKey) {

@ -828,7 +828,9 @@
Whisper.events.on('devicePairingRequestRejected', async pubKey => { Whisper.events.on('devicePairingRequestRejected', async pubKey => {
await window.libloki.storage.removeContactPreKeyBundle(pubKey); await window.libloki.storage.removeContactPreKeyBundle(pubKey);
await window.libloki.storage.removePairingAuthorisationForSecondaryPubKey(pubKey); await window.libloki.storage.removePairingAuthorisationForSecondaryPubKey(
pubKey
);
}); });
} }

@ -91,8 +91,9 @@ module.exports = {
createOrUpdatePairingAuthorisation, createOrUpdatePairingAuthorisation,
removePairingAuthorisationForSecondaryPubKey, removePairingAuthorisationForSecondaryPubKey,
getGrantAuthorisationForPubKey, getGrantAuthorisationForSecondaryPubKey,
getAuthorisationForPubKey, getAuthorisationForSecondaryPubKey,
getGrantAuthorisationsForPrimaryPubKey,
getSecondaryDevicesFor, getSecondaryDevicesFor,
getPrimaryDeviceFor, getPrimaryDeviceFor,
getPairedDevicesFor, getPairedDevicesFor,
@ -609,38 +610,24 @@ async function createOrUpdatePairingAuthorisation(data) {
} }
async function removePairingAuthorisationForSecondaryPubKey(pubKey) { async function removePairingAuthorisationForSecondaryPubKey(pubKey) {
if (!pubKey){ if (!pubKey) {
return; return;
} }
await channels.removePairingAuthorisationForSecondaryPubKey(pubKey); await channels.removePairingAuthorisationForSecondaryPubKey(pubKey);
} }
async function getGrantAuthorisationForPubKey(pubKey) { async function getGrantAuthorisationForSecondaryPubKey(pubKey) {
const authorisation = await channels.getAuthorisationForPubKey(pubKey, { return channels.getAuthorisationForSecondaryPubKey(pubKey, {
granted: true, granted: true,
}); });
if (!authorisation) {
return null;
}
return {
...authorisation,
requestSignature: base64ToArrayBuffer(authorisation.requestSignature),
grantSignature: base64ToArrayBuffer(authorisation.grantSignature),
};
} }
async function getAuthorisationForPubKey(pubKey) { async function getGrantAuthorisationsForPrimaryPubKey(pubKey) {
const authorisation = await channels.getAuthorisationForPubKey(pubKey); return channels.getGrantAuthorisationsForPrimaryPubKey(pubKey);
if (!authorisation) { }
return null;
} function getAuthorisationForSecondaryPubKey(pubKey) {
return { return channels.getAuthorisationForSecondaryPubKey(pubKey);
...authorisation,
requestSignature: base64ToArrayBuffer(authorisation.requestSignature),
grantSignature: authorisation.grantSignature
? base64ToArrayBuffer(authorisation.grantSignature)
: null,
};
} }
function getSecondaryDevicesFor(primaryDevicePubKey) { function getSecondaryDevicesFor(primaryDevicePubKey) {

@ -1,3 +1,7 @@
/* global storage: false */
/* global libloki: false */
/* global Signal: false */
const LokiAppDotNetAPI = require('./loki_app_dot_net_api'); const LokiAppDotNetAPI = require('./loki_app_dot_net_api');
const DEVICE_MAPPING_ANNOTATION_KEY = 'network.loki.messenger.devicemapping'; 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 = { const content = {
isPrimary: isPrimary ? '1' : '0', isPrimary: isPrimary ? '1' : '0',
authorisations, authorisations,

@ -1,4 +1,4 @@
/* global window, libsignal, textsecure */ /* global window, libsignal, textsecure, Signal */
// eslint-disable-next-line func-names // eslint-disable-next-line func-names
(function() { (function() {
@ -118,15 +118,47 @@
} }
function removePairingAuthorisationForSecondaryPubKey(pubKey) { function removePairingAuthorisationForSecondaryPubKey(pubKey) {
return window.Signal.Data.removePairingAuthorisationForSecondaryPubKey(pubKey); return window.Signal.Data.removePairingAuthorisationForSecondaryPubKey(
pubKey
);
} }
function getGrantAuthorisationForSecondaryPubKey(secondaryPubKey) { // Transforms signatures from base64 to ArrayBuffer!
return window.Signal.Data.getGrantAuthorisationForPubKey(secondaryPubKey); 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) { // Transforms signatures from base64 to ArrayBuffer!
return window.Signal.Data.getAuthorisationForPubKey(secondaryPubKey); 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) { function getSecondaryDevicesFor(primaryDevicePubKey) {

Loading…
Cancel
Save