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,
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) {

@ -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
);
});
}

@ -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) {

@ -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,

@ -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) {

Loading…
Cancel
Save