replace sending of established, grant and link request messages

pull/1183/head
Audric Ackermann 5 years ago
parent 4e6584c62c
commit 2e78f755b4
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -625,8 +625,7 @@ async function createOrUpdatePairingAuthorisation(data) {
} }
async function getPairingAuthorisationsFor(pubKey) { async function getPairingAuthorisationsFor(pubKey) {
const authorisations = channels.getPairingAuthorisationsFor(pubKey); const authorisations = await channels.getPairingAuthorisationsFor(pubKey);
return authorisations.map(authorisation => ({ return authorisations.map(authorisation => ({
...authorisation, ...authorisation,
requestSignature: base64ToArrayBuffer(authorisation.requestSignature), requestSignature: base64ToArrayBuffer(authorisation.requestSignature),

@ -1,4 +1,4 @@
/* global window, textsecure, dcodeIO, StringView, ConversationController, _ */ /* global window, textsecure, dcodeIO, StringView, ConversationController, _, libsession */
/* eslint-disable no-bitwise */ /* eslint-disable no-bitwise */
// eslint-disable-next-line func-names // eslint-disable-next-line func-names
@ -55,12 +55,12 @@
} }
async function sendSessionEstablishedMessage(pubKey) { async function sendSessionEstablishedMessage(pubKey) {
// This message shouldn't be routed through multi-device. const user = libsession.Types.PubKey.from(pubKey);
// It needs to go directly to the pubKey specified.
const message = textsecure.OutgoingMessage.buildSessionEstablishedMessage( const sessionEstablished = new window.libsession.Messages.Outgoing.SessionEstablished(
pubKey { timestamp: Date.now() }
); );
await message.sendToNumber(pubKey, false); await libsession.messageQueue.send(user, sessionEstablished);
} }
async function sendBackgroundMessage(pubKey, debugMessageType) { async function sendBackgroundMessage(pubKey, debugMessageType) {
@ -71,36 +71,7 @@
await backgroundMessage.sendToNumber(pubKey, false); await backgroundMessage.sendToNumber(pubKey, false);
} }
function createPairingAuthorisationProtoMessage({ function sendUnpairingMessageToSecondary(pubKey) {
primaryDevicePubKey,
secondaryDevicePubKey,
requestSignature,
grantSignature,
}) {
if (!primaryDevicePubKey || !secondaryDevicePubKey || !requestSignature) {
throw new Error(
'createPairingAuthorisationProtoMessage: pubkeys missing'
);
}
if (requestSignature.constructor !== ArrayBuffer) {
throw new Error(
'createPairingAuthorisationProtoMessage expects a signature as ArrayBuffer'
);
}
if (grantSignature && grantSignature.constructor !== ArrayBuffer) {
throw new Error(
'createPairingAuthorisationProtoMessage expects a signature as ArrayBuffer'
);
}
return new textsecure.protobuf.PairingAuthorisationMessage({
requestSignature: new Uint8Array(requestSignature),
grantSignature: grantSignature ? new Uint8Array(grantSignature) : null,
primaryDevicePubKey,
secondaryDevicePubKey,
});
}
function sendUnpairingMessageToSecondary(pubKey) {
const unpairingMessage = textsecure.OutgoingMessage.buildUnpairingMessage( const unpairingMessage = textsecure.OutgoingMessage.buildUnpairingMessage(
pubKey pubKey
); );
@ -216,39 +187,6 @@
}); });
return syncMessage; return syncMessage;
} }
async function sendPairingAuthorisation(authorisation, recipientPubKey) {
const pairingAuthorisation = createPairingAuthorisationProtoMessage(
authorisation
);
const ourNumber = textsecure.storage.user.getNumber();
const ourConversation = await ConversationController.getOrCreateAndWait(
ourNumber,
'private'
);
// Send
const p = new Promise((resolve, reject) => {
const callback = result => {
// callback
if (result.errors.length > 0) {
reject(result.errors[0]);
} else {
resolve();
}
};
const pairingRequestMessage = textsecure.OutgoingMessage.buildPairingRequestMessage(
recipientPubKey,
ourNumber,
ourConversation,
authorisation,
pairingAuthorisation,
callback
);
pairingRequestMessage.sendToNumber(recipientPubKey, false);
});
return p;
}
function sendSessionRequestsToMembers(members = []) { function sendSessionRequestsToMembers(members = []) {
// For every member, see if we need to establish a session: // For every member, see if we need to establish a session:
members.forEach(memberPubKey => { members.forEach(memberPubKey => {
@ -284,8 +222,6 @@
sendSessionEstablishedMessage, sendSessionEstablishedMessage,
sendBackgroundMessage, sendBackgroundMessage,
sendSessionRequestsToMembers, sendSessionRequestsToMembers,
sendPairingAuthorisation,
createPairingAuthorisationProtoMessage,
sendUnpairingMessageToSecondary, sendUnpairingMessageToSecondary,
createContactSyncProtoMessage, createContactSyncProtoMessage,
createGroupSyncProtoMessage, createGroupSyncProtoMessage,

@ -545,37 +545,48 @@
primaryDevicePubKey, primaryDevicePubKey,
libloki.crypto.PairingType.REQUEST libloki.crypto.PairingType.REQUEST
); );
const authorisation = {
primaryDevicePubKey, const primaryDevice = libsession.Types.PubKey.from(primaryDevicePubKey);
secondaryDevicePubKey: ourPubKey,
requestSignature, const requestPairingMessage = new window.libsession.Messages.Outgoing.DeviceLinkRequestMessage(
}; {
await libloki.api.sendPairingAuthorisation( timestamp: Date.now(),
authorisation, primaryDevicePubKey,
primaryDevicePubKey secondaryDevicePubKey: ourPubKey,
requestSignature,
}
); );
await libsession.messageQueue.send(primaryDevice, requestPairingMessage);
}, },
async authoriseSecondaryDevice(secondaryDevicePubKey) { async authoriseSecondaryDevice(secondaryDeviceStr) {
const ourPubKey = textsecure.storage.user.getNumber(); const ourPubKey = textsecure.storage.user.getNumber();
if (secondaryDevicePubKey === ourPubKey) { if (secondaryDeviceStr === ourPubKey) {
throw new Error( throw new Error(
'Cannot register primary device pubkey as secondary device' 'Cannot register primary device pubkey as secondary device'
); );
} }
const secondaryDevicePubKey = libsession.Types.PubKey.from(
secondaryDeviceStr
);
// throws if invalid if (!secondaryDevicePubKey) {
this.validatePubKeyHex(secondaryDevicePubKey); window.console.error(
'Invalid secondary pubkey on authoriseSecondaryDevice'
);
return;
}
// we need a conversation for sending a message // we need a conversation for sending a message
const secondaryConversation = await ConversationController.getOrCreateAndWait( const secondaryConversation = await ConversationController.getOrCreateAndWait(
secondaryDevicePubKey, secondaryDeviceStr,
'private' 'private'
); );
const grantSignature = await libloki.crypto.generateSignatureForPairing( const grantSignature = await libloki.crypto.generateSignatureForPairing(
secondaryDevicePubKey, secondaryDeviceStr,
libloki.crypto.PairingType.GRANT libloki.crypto.PairingType.GRANT
); );
const authorisations = await libsession.Protocols.MultiDeviceProtocol.getPairingAuthorisations( const authorisations = await libsession.Protocols.MultiDeviceProtocol.getPairingAuthorisations(
secondaryDevicePubKey secondaryDeviceStr
); );
const existingAuthorisation = authorisations.some( const existingAuthorisation = authorisations.some(
pairing => pairing.secondaryDevicePubKey === secondaryDevicePubKey pairing => pairing.secondaryDevicePubKey === secondaryDevicePubKey
@ -588,7 +599,7 @@
const { requestSignature } = existingAuthorisation; const { requestSignature } = existingAuthorisation;
const authorisation = { const authorisation = {
primaryDevicePubKey: ourPubKey, primaryDevicePubKey: ourPubKey,
secondaryDevicePubKey, secondaryDevicePubKey: secondaryDeviceStr,
requestSignature, requestSignature,
grantSignature, grantSignature,
}; };
@ -597,13 +608,34 @@
await libsession.Protocols.MultiDeviceProtocol.savePairingAuthorisation( await libsession.Protocols.MultiDeviceProtocol.savePairingAuthorisation(
authorisation authorisation
); );
const ourConversation = await ConversationController.getOrCreateAndWait(
ourPubKey,
'private'
);
// We need to send the our profile to the secondary device
const { displayName } = ourConversation.getLokiProfile();
const avatarPointer = ourConversation.get('avatarPointer');
const profileKey = window.storage.get('profileKey');
const lokiProfile = {
displayName,
profileKey,
avatarPointer,
};
// Try to upload to the file server and then send a message // Try to upload to the file server and then send a message
try { try {
await lokiFileServerAPI.updateOurDeviceMapping(); await lokiFileServerAPI.updateOurDeviceMapping();
await libloki.api.sendPairingAuthorisation( const requestPairingMessage = new libsession.Messages.Outgoing.DeviceLinkGrantMessageParams(
authorisation, {
secondaryDevicePubKey timestamp: Date.now(),
...authorisation,
lokiProfile,
}
);
await libsession.messageQueue.send(
secondaryDevicePubKey,
requestPairingMessage
); );
} catch (e) { } catch (e) {
log.error( log.error(
@ -612,7 +644,7 @@
); );
// File server upload failed or message sending failed, we should rollback changes // File server upload failed or message sending failed, we should rollback changes
await libsession.Protocols.MultiDeviceProtocol.removePairingAuthorisations( await libsession.Protocols.MultiDeviceProtocol.removePairingAuthorisations(
secondaryDevicePubKey secondaryDeviceStr
); );
await lokiFileServerAPI.updateOurDeviceMapping(); await lokiFileServerAPI.updateOurDeviceMapping();
throw e; throw e;

@ -113,7 +113,6 @@ const DebugMessageType = {
OPEN_GROUP_SYNC_SEND: 'open-group-sync-send', OPEN_GROUP_SYNC_SEND: 'open-group-sync-send',
DEVICE_UNPAIRING_SEND: 'device-unpairing-send', DEVICE_UNPAIRING_SEND: 'device-unpairing-send',
PAIRING_REQUEST_SEND: 'pairing-request',
}; };
function OutgoingMessage( function OutgoingMessage(
@ -746,50 +745,6 @@ OutgoingMessage.buildUnpairingMessage = function buildUnpairingMessage(pubKey) {
return outgoingMessage; return outgoingMessage;
}; };
OutgoingMessage.buildPairingRequestMessage = function buildPairingRequestMessage(
pubKey,
ourNumber,
ourConversation,
authorisation,
pairingAuthorisation,
callback
) {
const content = new textsecure.protobuf.Content({
pairingAuthorisation,
});
const isGrant = authorisation.primaryDevicePubKey === ourNumber;
if (isGrant) {
// Send profile name to secondary device
const lokiProfile = ourConversation.getLokiProfile();
// profile.avatar is the path to the local image
// replace with the avatar URL
const avatarPointer = ourConversation.get('avatarPointer');
lokiProfile.avatar = avatarPointer;
const profile = new textsecure.protobuf.DataMessage.LokiProfile(
lokiProfile
);
const profileKey = window.storage.get('profileKey');
const dataMessage = new textsecure.protobuf.DataMessage({
profile,
profileKey,
});
content.dataMessage = dataMessage;
}
const debugMessageType = DebugMessageType.PAIRING_REQUEST_SEND;
const options = { messageType: 'pairing-request', debugMessageType };
const outgoingMessage = new textsecure.OutgoingMessage(
null, // server
Date.now(), // timestamp,
[pubKey], // numbers
content, // message
true, // silent
callback, // callback
options
);
return outgoingMessage;
};
OutgoingMessage.DebugMessageType = DebugMessageType; OutgoingMessage.DebugMessageType = DebugMessageType;
window.textsecure = window.textsecure || {}; window.textsecure = window.textsecure || {};

@ -1,9 +1,8 @@
import * as Messages from './messages'; import * as Messages from './messages';
import * as Protocols from './protocols'; import * as Protocols from './protocols';
import * as Types from './types'; import * as Types from './types';
import { MessageQueue } from './sending';
// TODO: Do we export class instances here? const messageQueue = new MessageQueue();
// E.g
// export const messageQueue = new MessageQueue()
export { Messages, Protocols, Types }; export { Messages, Protocols, Types, messageQueue };

@ -168,9 +168,7 @@ export class MessageQueue implements MessageQueueInterface {
} }
if (message instanceof SessionRequestMessage) { if (message instanceof SessionRequestMessage) {
void SessionProtocol.sendSessionRequest(message, device); return SessionProtocol.sendSessionRequest(message, device);
return;
} }
await this.pendingMessageCache.add(device, message); await this.pendingMessageCache.add(device, message);

Loading…
Cancel
Save