Save device mappings upon accepting friend request + various fixes

pull/548/head
sachaaaaa 6 years ago
parent 6415e33122
commit 1f3311bc5d

@ -12,6 +12,7 @@
Whisper, Whisper,
clipboard, clipboard,
libloki, libloki,
lokiFileServerAPI,
*/ */
/* eslint-disable more/no-then */ /* eslint-disable more/no-then */
@ -444,6 +445,8 @@
await window.Signal.Data.saveMessage(this.attributes, { await window.Signal.Data.saveMessage(this.attributes, {
Message: Whisper.Message, Message: Whisper.Message,
}); });
const pubKey = this.get('conversationId');
await libloki.storage.saveAllPairingAuthorisationsFor(pubKey);
conversation.onAcceptFriendRequest(); conversation.onAcceptFriendRequest();
}, },
async declineFriendRequest() { async declineFriendRequest() {

@ -594,6 +594,9 @@ function signatureToBase64(signature) {
return dcodeIO.ByteBuffer.wrap(signature).toString('base64'); return dcodeIO.ByteBuffer.wrap(signature).toString('base64');
} else if (isArrayBuffer(signature)) { } else if (isArrayBuffer(signature)) {
return arrayBufferToBase64(signature); return arrayBufferToBase64(signature);
} else if (typeof signature === 'string') {
// assume it's already base64
return signature;
} }
throw new Error( throw new Error(
'Invalid signature provided in createOrUpdatePairingAuthorisation. Needs to be either ArrayBuffer or ByteBuffer.' 'Invalid signature provided in createOrUpdatePairingAuthorisation. Needs to be either ArrayBuffer or ByteBuffer.'

@ -19,9 +19,10 @@ class LokiFileServerAPI {
async getUserDeviceMapping(pubKey) { async getUserDeviceMapping(pubKey) {
const annotations = await this._server.getUserAnnotations(pubKey); const annotations = await this._server.getUserAnnotations(pubKey);
return annotations.find( const deviceMapping = annotations.find(
annotation => annotation.type === DEVICE_MAPPING_ANNOTATION_KEY annotation => annotation.type === DEVICE_MAPPING_ANNOTATION_KEY
); );
return deviceMapping ? deviceMapping.value : null;
} }
async updateOurDeviceMapping() { async updateOurDeviceMapping() {

@ -176,7 +176,6 @@
async function validateAuthorisation(authorisation) { async function validateAuthorisation(authorisation) {
const { const {
type,
primaryDevicePubKey, primaryDevicePubKey,
secondaryDevicePubKey, secondaryDevicePubKey,
requestSignature, requestSignature,
@ -184,10 +183,8 @@
} = authorisation; } = authorisation;
const alreadySecondaryDevice = !!window.storage.get('isSecondaryDevice'); const alreadySecondaryDevice = !!window.storage.get('isSecondaryDevice');
const ourPubKey = textsecure.storage.user.getNumber(); const ourPubKey = textsecure.storage.user.getNumber();
const isRequest = const isRequest = !grantSignature;
type === textsecure.protobuf.PairingAuthorisationMessage.Type.REQUEST; const isGrant = !!grantSignature;
const isGrant =
type === textsecure.protobuf.PairingAuthorisationMessage.Type.GRANT;
if (!primaryDevicePubKey || !secondaryDevicePubKey) { if (!primaryDevicePubKey || !secondaryDevicePubKey) {
window.log.warn( window.log.warn(
'Received a pairing request with missing pubkeys. Ignored.' 'Received a pairing request with missing pubkeys. Ignored.'
@ -212,11 +209,18 @@
window.log.warn('Received a pairing request from ourselves. Ignored.'); window.log.warn('Received a pairing request from ourselves. Ignored.');
return false; return false;
} }
try { const verify = async (signature, signatureType) => {
const encoding = typeof signature === 'string' ? 'base64' : undefined;
await this.verifyPairingSignature( await this.verifyPairingSignature(
primaryDevicePubKey, primaryDevicePubKey,
secondaryDevicePubKey, secondaryDevicePubKey,
dcodeIO.ByteBuffer.wrap(requestSignature).toArrayBuffer(), dcodeIO.ByteBuffer.wrap(signature, encoding).toArrayBuffer(),
signatureType
);
};
try {
await verify(
requestSignature,
textsecure.protobuf.PairingAuthorisationMessage.Type.REQUEST textsecure.protobuf.PairingAuthorisationMessage.Type.REQUEST
); );
} catch (e) { } catch (e) {
@ -228,10 +232,8 @@
} }
if (isGrant) { if (isGrant) {
try { try {
await this.verifyPairingSignature( await verify(
primaryDevicePubKey, grantSignature,
secondaryDevicePubKey,
dcodeIO.ByteBuffer.wrap(grantSignature).toArrayBuffer(),
textsecure.protobuf.PairingAuthorisationMessage.Type.GRANT textsecure.protobuf.PairingAuthorisationMessage.Type.GRANT
); );
} catch (e) { } catch (e) {

@ -1,4 +1,4 @@
/* global window, libsignal, textsecure, Signal */ /* global window, libsignal, textsecure, Signal, lokiFileServerAPI */
// eslint-disable-next-line func-names // eslint-disable-next-line func-names
(function() { (function() {
@ -113,6 +113,33 @@
} }
} }
// fetches device mappings from server.
// if the device is a secondary device,
// fetch the device mappings for its primary device
async function saveAllPairingAuthorisationsFor(pubKey) {
const deviceMapping = await lokiFileServerAPI.getUserDeviceMapping(pubKey);
let { authorisations } = deviceMapping || {};
if (deviceMapping) {
if (deviceMapping.isPrimary !== '1') {
const { primaryDevicePubKey } =
authorisations.find(
authorisation => authorisation.secondaryDevicePubKey === pubKey
) || {};
if (primaryDevicePubKey) {
({ authorisations } =
(await lokiFileServerAPI.getUserDeviceMapping(
primaryDevicePubKey
)) || {});
}
await Promise.all(
authorisations.map(authorisation =>
savePairingAuthorisation(authorisation)
)
);
}
}
}
function savePairingAuthorisation(authorisation) { function savePairingAuthorisation(authorisation) {
return window.Signal.Data.createOrUpdatePairingAuthorisation(authorisation); return window.Signal.Data.createOrUpdatePairingAuthorisation(authorisation);
} }
@ -177,6 +204,7 @@
removeContactPreKeyBundle, removeContactPreKeyBundle,
verifyFriendRequestAcceptPreKey, verifyFriendRequestAcceptPreKey,
savePairingAuthorisation, savePairingAuthorisation,
saveAllPairingAuthorisationsFor,
removePairingAuthorisationForSecondaryPubKey, removePairingAuthorisationForSecondaryPubKey,
getGrantAuthorisationForSecondaryPubKey, getGrantAuthorisationForSecondaryPubKey,
getAuthorisationForSecondaryPubKey, getAuthorisationForSecondaryPubKey,

Loading…
Cancel
Save