Remove multi device from libloki

pull/1176/head
Mikunj 5 years ago
parent 0275d43fa4
commit 7b7d8c0e2a

@ -31,18 +31,13 @@
this.remove(receipts);
return receipts;
},
async getTargetMessage(source, messages) {
async getTargetMessage(originalSource, messages) {
if (messages.length === 0) {
return null;
}
const authorisation = await libloki.storage.getGrantAuthorisationForSecondaryPubKey(
source
);
if (authorisation) {
// eslint-disable-next-line no-param-reassign
source = authorisation.primaryDevicePubKey;
}
const primary = await window.libsession.Protocols.MultiDeviceProtocol.getPrimaryDevice(originalSource);
const source = primary.key;
const message = messages.find(
item => !item.isIncoming() && source === item.get('conversationId')

@ -211,10 +211,9 @@
return true;
}
const ourDevices = await window.libloki.storage.getPairedDevicesFor(
this.ourNumber
);
return ourDevices.includes(this.id);
const ourDevices = await window.libsession.Protocols.MultiDeviceProtocol.getAllDevices(this.ourNumber);
return ourDevices.some(device => device.key === this.id);
},
isOurLocalDevice() {
return this.id === this.ourNumber;

@ -197,7 +197,7 @@
// exists in group, but hasn't yet synced with its other devices.
const getDevicesForRemoved = async () => {
const promises = notPresentInNew.map(member =>
libloki.storage.getPairedDevicesFor(member)
window.libsession.Protocols.MultiDeviceProtocol.getAllDevices(member)
);
const devices = _.flatten(await Promise.all(promises));

@ -74,15 +74,6 @@
}
}
// Returns the primary device pubkey for this secondary device pubkey
// or the same pubkey if there is no other device
async function getPrimaryDevicePubkey(pubKey) {
const authorisation = await window.libloki.storage.getGrantAuthorisationForSecondaryPubKey(
pubKey
);
return authorisation ? authorisation.primaryDevicePubKey : pubKey;
}
async function sendSessionEstablishedMessage(pubKey) {
// This message shouldn't be routed through multi-device.
// It needs to go directly to the pubKey specified.
@ -330,7 +321,6 @@
createContactSyncProtoMessage,
createGroupSyncProtoMessage,
createOpenGroupsSyncProtoMessage,
getPrimaryDevicePubkey,
debug,
};
})();

@ -114,65 +114,6 @@
}
}
// fetches device mappings from server.
async function getPrimaryDeviceMapping(pubKey) {
if (typeof lokiFileServerAPI === 'undefined') {
// If this is not defined then we are initiating a pairing
return [];
}
const deviceMapping = await lokiFileServerAPI.getUserDeviceMapping(pubKey);
if (!deviceMapping) {
return [];
}
let authorisations = deviceMapping.authorisations || [];
if (deviceMapping.isPrimary === '0') {
const { primaryDevicePubKey } =
authorisations.find(
authorisation =>
authorisation && authorisation.secondaryDevicePubKey === pubKey
) || {};
if (primaryDevicePubKey) {
// do NOT call getprimaryDeviceMapping recursively
// in case both devices are out of sync and think they are
// each others' secondary pubkey.
const primaryDeviceMapping = await lokiFileServerAPI.getUserDeviceMapping(
primaryDevicePubKey
);
if (!primaryDeviceMapping) {
return [];
}
({ authorisations } = primaryDeviceMapping);
}
}
// filter out any invalid authorisations
return authorisations.filter(a => a && typeof a === 'object') || [];
}
// if the device is a secondary device,
// fetch the device mappings for its primary device
async function saveAllPairingAuthorisationsFor() {
throw new Error('Use MultiDeviceProtocol instead.');
}
async function savePairingAuthorisation() {
throw new Error('Use MultiDeviceProtocol instead.');
}
// Transforms signatures from base64 to ArrayBuffer!
async function getGrantAuthorisationForSecondaryPubKey() {
throw new Error('Use MultiDeviceProtocol instead.');
}
// Transforms signatures from base64 to ArrayBuffer!
async function getAuthorisationForSecondaryPubKey() {
throw new Error('Use MultiDeviceProtocol instead.');
}
function getSecondaryDevicesFor() {
throw new Error('Use MultiDeviceProtocol instead.');
}
function getGuardNodes() {
return window.Signal.Data.getGuardNodes();
}
@ -181,27 +122,11 @@
return window.Signal.Data.updateGuardNodes(nodes);
}
async function getAllDevicePubKeysForPrimaryPubKey() {
throw new Error('Use MultiDeviceProtocol instead.');
}
function getPairedDevicesFor() {
throw new Error('use MultiDeviceProtocol instead');
}
window.libloki.storage = {
getPreKeyBundleForContact,
saveContactPreKeyBundle,
removeContactPreKeyBundle,
verifyFriendRequestAcceptPreKey,
getAllDevicePubKeysForPrimaryPubKey,
getPairedDevicesFor,
getSecondaryDevicesFor,
getPrimaryDeviceMapping,
saveAllPairingAuthorisationsFor,
savePairingAuthorisation,
getGrantAuthorisationForSecondaryPubKey,
getAuthorisationForSecondaryPubKey,
getGuardNodes,
updateGuardNodes,
};

@ -574,9 +574,8 @@
secondaryDevicePubKey,
libloki.crypto.PairingType.GRANT
);
const existingAuthorisation = await libloki.storage.getAuthorisationForSecondaryPubKey(
secondaryDevicePubKey
);
const authorisations = await libsession.Protocols.MultiDeviceProtocol.getPairingAuthorisations(secondaryDevicePubKey);
const existingAuthorisation = authorisations.some(pairing => pairing.secondaryDevicePubKey === secondaryDevicePubKey);
if (!existingAuthorisation) {
throw new Error(
'authoriseSecondaryDevice: request signature missing from database!'
@ -589,8 +588,9 @@
requestSignature,
grantSignature,
};
// Update authorisation in database with the new grant signature
await libloki.storage.savePairingAuthorisation(authorisation);
await libsession.Protocols.MultiDeviceProtocol.savePairingAuthorisation(authorisation);
// Try to upload to the file server and then send a message
try {

@ -968,7 +968,7 @@ MessageReceiver.prototype.extend({
if (valid) {
// Pairing dialog is open and is listening
if (Whisper.events.isListenedTo('devicePairingRequestReceived')) {
await window.libloki.storage.savePairingAuthorisation(pairingRequest);
await window.libsession.Protocols.MultiDeviceProtocol.savePairingAuthorisation(pairingRequest);
Whisper.events.trigger(
'devicePairingRequestReceived',
pairingRequest.secondaryDevicePubKey
@ -1014,7 +1014,7 @@ MessageReceiver.prototype.extend({
window.storage.remove('secondaryDeviceStatus');
window.storage.put('isSecondaryDevice', true);
window.storage.put('primaryDevicePubKey', primaryDevicePubKey);
await libloki.storage.savePairingAuthorisation(pairingAuthorisation);
await window.libsession.Protocols.MultiDeviceProtocol.savePairingAuthorisation(pairingAuthorisation);
const primaryConversation = await ConversationController.getOrCreateAndWait(
primaryDevicePubKey,
'private'

@ -431,13 +431,13 @@ OutgoingMessage.prototype = {
const ourPubKey = textsecure.storage.user.getNumber();
const ourPrimaryPubkey = window.storage.get('primaryDevicePubKey');
const secondaryPubKeys =
(await window.libloki.storage.getSecondaryDevicesFor(ourPubKey)) || [];
(await window.libsession.Protocols.MultiDeviceProtocol.getSecondaryDevices(ourPubKey)) || [];
let aliasedPubkey = devicePubKey;
if (devicePubKey === ourPubKey) {
aliasedPubkey = 'OUR_PUBKEY'; // should not happen
} else if (devicePubKey === ourPrimaryPubkey) {
aliasedPubkey = 'OUR_PRIMARY_PUBKEY';
} else if (secondaryPubKeys.includes(devicePubKey)) {
} else if (secondaryPubKeys.some(device => device.key === devicePubKey)) {
aliasedPubkey = 'OUR SECONDARY PUBKEY';
}
libloki.api.debug.logSessionMessageSending(

@ -537,12 +537,12 @@ MessageSender.prototype = {
window.storage.get('primaryDevicePubKey') ||
textsecure.storage.user.getNumber();
const allOurDevices = (
await libloki.storage.getAllDevicePubKeysForPrimaryPubKey(
await window.libsession.Protocols.MultiDeviceProtocol.getAllDevices(
primaryDeviceKey
)
)
// Don't send to ourselves
.filter(pubKey => pubKey !== textsecure.storage.user.getNumber());
.filter(pubKey => pubKey.key !== textsecure.storage.user.getNumber());
if (allOurDevices.length === 0) {
return null;
}

@ -15,6 +15,8 @@ import { SignalService } from './../protobuf';
import { removeFromCache } from './cache';
import { toNumber } from 'lodash';
import { DataMessage } from '../session/messages/outgoing';
import { MultiDeviceProtocol } from '../session/protocols';
import { PubKey } from '../session/types';
export { handleEndSession, handleMediumGroupUpdate };
@ -265,7 +267,7 @@ async function handleSecondaryDeviceFriendRequest(pubKey: string) {
if (!c || !(await c.isFriendWithAnyDevice())) {
return false;
}
await window.libloki.storage.savePairingAuthorisation(authorisation);
await MultiDeviceProtocol.savePairingAuthorisation(authorisation);
return true;
}
@ -601,14 +603,11 @@ export async function handleDataMessage(
const source = envelope.senderIdentity || senderPubKey;
const isOwnDevice = async (pubkey: string) => {
const primaryDevice = window.storage.get('primaryDevicePubKey');
const secondaryDevices = await window.libloki.storage.getPairedDevicesFor(
primaryDevice
);
const isOwnDevice = async (device: string) => {
const pubKey = new PubKey(device);
const allDevices = await MultiDeviceProtocol.getAllDevices(pubKey);
const allDevices = [primaryDevice, ...secondaryDevices];
return allDevices.includes(pubkey);
return allDevices.some(device => PubKey.isEqual(device, pubKey));
};
const ownDevice = await isOwnDevice(source);
@ -773,13 +772,7 @@ export async function handleMessageEvent(event: any): Promise<void> {
// - group.id if it is a group message
let conversationId = id;
const authorisation = await window.libloki.storage.getGrantAuthorisationForSecondaryPubKey(
source
);
const primarySource =
(authorisation && authorisation.primaryDevicePubKey) || source;
const primarySource = await MultiDeviceProtocol.getPrimaryDevice(source);
if (isGroupMessage) {
/* handle one part of the group logic here:
handle requesting info of a new group,
@ -789,7 +782,7 @@ export async function handleMessageEvent(event: any): Promise<void> {
const shouldReturn = await preprocessGroupMessage(
source,
message.group,
primarySource
primarySource.key
);
// handleGroupMessage() can process fully a message in some cases
@ -800,9 +793,9 @@ export async function handleMessageEvent(event: any): Promise<void> {
}
}
if (source !== ourNumber && authorisation) {
if (source !== ourNumber) {
// Ignore auth from our devices
conversationId = authorisation.primaryDevicePubKey;
conversationId = primarySource.key;
}
// the conversation with the primary device of that source (can be the same as conversationOrigin)

Loading…
Cancel
Save