verifyUserObjectDeviceMap removed notFoundHandler from prototype/reduced branching depth/returns users pub keys not found

pull/539/head
Ryan Tharp 6 years ago
parent 60cefc7fe1
commit cad9a89636

@ -58,40 +58,46 @@ class LokiFileServerAPI {
return users; return users;
} }
async verifyUserObjectDeviceMap( async verifyUserObjectDeviceMap(pubKeys, isRequest, iterator) {
pubKeys,
isRequest,
iterator,
notFoundHandler
) {
const users = await this.getDeviceMappingForUsers(pubKeys); const users = await this.getDeviceMappingForUsers(pubKeys);
// log.info('verifyUserObjectDeviceMap Found', users.length, 'users') // log.info('verifyUserObjectDeviceMap Found', users.length, 'users')
// go through each user and find deviceMap annotations // go through each user and find deviceMap annotations
const notFoundUsers = [];
users.forEach(user => { users.forEach(user => {
let found = false; let found = false;
if (user.annotations) { if (!user.annotations || !user.annotations.length) {
log.info(
`verifyUserObjectDeviceMap no annotation for ${user.username}`
);
return;
}
user.annotations.forEach(note => { user.annotations.forEach(note => {
if (note.type === 'network.loki.messenger.devicemapping') { if (note.type !== 'network.loki.messenger.devicemapping') {
// is desired type return;
}
// isn't desired type
// request is slave => primary type...
if ( if (
(isRequest && note.value.isPrimary === '0') || (isRequest && note.value.isPrimary !== '0') ||
(!isRequest && note.value.isPrimary !== '0') (!isRequest && note.value.isPrimary === '0')
) { ) {
/* log.info(`verifyUserObjectDeviceMap found wrong type of` +
`relationship ${user.username}`); */
// console.log(`https://file.lokinet.org/users/@${user.username}?prettyPrint=1&include_annotations=1`);
return;
}
const { authorisations } = note.value; const { authorisations } = note.value;
if (Array.isArray(authorisations)) { if (!Array.isArray(authorisations)) {
return;
}
authorisations.forEach(auth => { authorisations.forEach(auth => {
// log.info('devmap auth', auth); // log.info('devmap auth', auth);
// only skip, if in secondary search mode // only skip, if in secondary search mode
if ( if (isRequest && auth.secondaryDevicePubKey !== user.username) {
isRequest &&
auth.secondaryDevicePubKey !== user.username
) {
// this is not the authorization we're looking for // this is not the authorization we're looking for
log.info( log.info(
`Request and ${auth.secondaryDevicePubKey} != ${ `Request and ${auth.secondaryDevicePubKey} != ${user.username}`
user.username
}`
); );
return; return;
} }
@ -107,10 +113,8 @@ class LokiFileServerAPI {
'base64' 'base64'
).toArrayBuffer(), ).toArrayBuffer(),
isRequest isRequest
? textsecure.protobuf.PairingAuthorisationMessage.Type ? textsecure.protobuf.PairingAuthorisationMessage.Type.REQUEST
.REQUEST : textsecure.protobuf.PairingAuthorisationMessage.Type.GRANT
: textsecure.protobuf.PairingAuthorisationMessage.Type
.GRANT
); );
// log.info('auth is valid for', user.username) // log.info('auth is valid for', user.username)
if (iterator(user.username, auth)) { if (iterator(user.username, auth)) {
@ -118,27 +122,19 @@ class LokiFileServerAPI {
} }
} catch (e) { } catch (e) {
log.warn( log.warn(
`Invalid signature on pubkey ${ `Invalid signature on pubkey ${user.username} authorization ${
user.username
} authorization ${
auth.secondaryDevicePubKey auth.secondaryDevicePubKey
} isRequest ${isRequest}` } isRequest ${isRequest}`
); );
} }
}); // end forEach authorisations }); // end forEach authorisations
}
}
}
}); // end forEach annotations }); // end forEach annotations
} if (!found) {
if (notFoundHandler) { notFoundUsers.push(user.username);
if (found) {
return;
}
notFoundHandler(user.username);
} }
}); // end forEach users }); // end forEach users
// log.info('done with users', users.length); // log.info('done with users', users.length);
return notFoundUsers;
} }
// verifies list of pubKeys for any deviceMappings // verifies list of pubKeys for any deviceMappings
@ -176,13 +172,14 @@ class LokiFileServerAPI {
// no valid primary pubkeys to check // no valid primary pubkeys to check
if (!primaryPubKeys.length) { if (!primaryPubKeys.length) {
// log.warn(`no valid primary pubkeys to check ${pubKeys}`);
return []; return [];
} }
const verifiedPrimaryPKs = []; const verifiedPrimaryPKs = [];
// get a list of all of primary pubKeys to verify the secondaryDevice assertion // get a list of all of primary pubKeys to verify the secondaryDevice assertion
await this.verifyUserObjectDeviceMap( const notFoundUsers = await this.verifyUserObjectDeviceMap(
primaryPubKeys, primaryPubKeys,
false, false,
(primaryKey, auth) => { (primaryKey, auth) => {
@ -205,9 +202,11 @@ class LokiFileServerAPI {
return false; return false;
} }
return true; return true;
}, }
primaryPubKey => { ); // end verifyUserObjectDeviceMap
// if not verified remove this user pubkey from newSlavePrimaryMap
// remove from newSlavePrimaryMap if no valid mapping is found
notFoundUsers.forEach(primaryPubKey => {
Object.keys(newSlavePrimaryMap).forEach(slaveKey => { Object.keys(newSlavePrimaryMap).forEach(slaveKey => {
if (newSlavePrimaryMap[slaveKey] === primaryPubKey) { if (newSlavePrimaryMap[slaveKey] === primaryPubKey) {
log.warn( log.warn(
@ -216,8 +215,7 @@ class LokiFileServerAPI {
delete newSlavePrimaryMap[slaveKey]; delete newSlavePrimaryMap[slaveKey];
} }
}); });
} });
); // end verifyUserObjectDeviceMap
// make new map final // make new map final
window.lokiPublicChatAPI.slavePrimaryMap = newSlavePrimaryMap; window.lokiPublicChatAPI.slavePrimaryMap = newSlavePrimaryMap;

Loading…
Cancel
Save