Store team chat keys in the user proxy

pull/1/head
yflory 5 years ago
parent 9cf99a8b65
commit 74d7c00134

@ -767,12 +767,11 @@ define([
}; };
var openTeamChat = function (ctx, clientId, data, _cb) { var openTeamChat = function (ctx, clientId, data, _cb) {
var teams = ctx.store.modules['team']; var chatData = data;
var team = teams.getTeam(data.teamId);
if (!team) { return; }
var chatData = team.getChatData();
var chanId = chatData.channel; var chanId = chatData.channel;
var secret = chatData.secret;
if (!chanId || !secret) { return void cb({error: 'EINVAL'}); }
var cb = Util.once(Util.mkAsync(function () { var cb = Util.once(Util.mkAsync(function () {
ctx.emit('TEAMCHAT_READY', chanId, [clientId]); ctx.emit('TEAMCHAT_READY', chanId, [clientId]);
@ -791,7 +790,6 @@ define([
}); });
} }
var secret = chatData.secret;
if (secret.keys.cryptKey) { if (secret.keys.cryptKey) {
secret.keys.cryptKey = convertToUint8(secret.keys.cryptKey); secret.keys.cryptKey = convertToUint8(secret.keys.cryptKey);
} }

@ -10,12 +10,13 @@ define([
'/bower_components/chainpad-listmap/chainpad-listmap.js', '/bower_components/chainpad-listmap/chainpad-listmap.js',
'/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-crypto/crypto.js',
'/bower_components/chainpad-netflux/chainpad-netflux.js',
'/bower_components/chainpad/chainpad.dist.js', '/bower_components/chainpad/chainpad.dist.js',
'/bower_components/nthen/index.js', '/bower_components/nthen/index.js',
'/bower_components/tweetnacl/nacl-fast.min.js', '/bower_components/tweetnacl/nacl-fast.min.js',
], function (Util, Hash, Constants, Realtime, ], function (Util, Hash, Constants, Realtime,
ProxyManager, UserObject, SF, ProxyManager, UserObject, SF,
Listmap, Crypto, ChainPad, nThen) { Listmap, Crypto, CpNetflux, ChainPad, nThen) {
var Team = {}; var Team = {};
var Nacl = window.nacl; var Nacl = window.nacl;
@ -139,15 +140,15 @@ define([
}; };
team.getChatData = function () { team.getChatData = function () {
var hash = Util.find(proxy, ['metadata', 'chat']); var hash = Util.find(ctx.store.proxy, ['teams', id, 'keys' 'chat', 'hash']);
if (!hash) { if (!hash) { return {}; }
hash = proxy.metadata.chat = Hash.createRandomHash('chat');
}
var secret = Hash.getSecrets('chat', hash); var secret = Hash.getSecrets('chat', hash);
return { return {
teamId: id,
channel: secret.channel, channel: secret.channel,
secret: secret, secret: secret,
validateKey: secret.keys.validateKey validateKey: secret.keys.validateKey
// XXX owners: team owner + all admins?
}; };
}; };
@ -286,6 +287,7 @@ define([
var secret = Hash.getSecrets('team', hash, password); var secret = Hash.getSecrets('team', hash, password);
var keyPair = Nacl.sign.keyPair(); // keyPair.secretKey , keyPair.publicKey var keyPair = Nacl.sign.keyPair(); // keyPair.secretKey , keyPair.publicKey
// Crypto.create
var membersSecret = Hash.getSecrets('members'); var membersSecret = Hash.getSecrets('members');
var membersHashes = Hash.getHashes(membersSecret); var membersHashes = Hash.getHashes(membersSecret);
@ -304,43 +306,70 @@ define([
owners: [ctx.store.proxy.edPublic] owners: [ctx.store.proxy.edPublic]
}; };
nThen(function (waitFor) { nThen(function (waitFor) {
console.log('pin..'); // XXX add this to the reset list too
ctx.pinPads([secret.channel, membersSecret.channel], waitFor(function (obj) { ctx.pinPads([secret.channel, membersSecret.channel, chatSecret.channel], waitFor(function (obj) {
if (obj && obj.error) { if (obj && obj.error) {
waitFor.abort(); waitFor.abort();
return void cb(obj); return void cb(obj);
} }
})); }));
// XXX initialize the members channel with yourself, and mark it as owned! // XXX initialize the members channel with yourself, and mark it as owned!
var chatCfg = {
network: ctx.store.network,
channel: chatSecret.channel,
noChainPad: true,
crypto: crypto,
metadata: {
validateKey: secret.keys.validateKey,
owners: [ctx.store.proxy.edPublic],
}
};
var chatReady = waitFor();
var cpNf2;
chatCfg.onReady = function () {
if (cpNf2) { cpNf2.stop(); }
chatReady();
};
chatCfg.onError = function () { chatReady(); };
cpNf2 = CpNetflux.start(chatCfg);
}).nThen(function () { }).nThen(function () {
console.log('init proxy');
var lm = Listmap.create(config); var lm = Listmap.create(config);
var proxy = lm.proxy; var proxy = lm.proxy;
proxy.on('ready', function () { proxy.on('ready', function () {
console.log('ready');
// Store keys in our drive
var id = Util.createRandomInteger(); var id = Util.createRandomInteger();
// Store keys in our drive
var keys = { var keys = {
drive: {
edPrivate: Nacl.util.encodeBase64(keyPair.secretKey), edPrivate: Nacl.util.encodeBase64(keyPair.secretKey),
edPublic: Nacl.util.encodeBase64(keyPair.publicKey) edPublic: Nacl.util.encodeBase64(keyPair.publicKey)
},
chat: {
hash: chatHashes.editHash,
channel: chatSecret.channel
},
members: {
// XXX
}
}; };
ctx.store.proxy.teams[id] = { ctx.store.proxy.teams[id] = {
hash: hash, hash: hash,
password: password, password: password,
keys: keys, keys: keys,
members: membersHashes.editHash, //members: membersHashes.editHash,
metadata: {
name: data.name name: data.name
}
}; };
// Initialize the team drive // Initialize the team drive
proxy.drive = {}; proxy.drive = {};
// Create metadata // Create metadata
proxy.metadata = { proxy.metadata = {
chat: chatHashes.editHash, //chat: chatHashes.editHash,
name: name, //name: name,
members: membersHashes.viewHash, //members: membersHashes.viewHash,
}; };
// Add rpc key // Add rpc key
proxy.edPublic = Nacl.util.encodeBase64(keyPair.publicKey); //proxy.edPublic = Nacl.util.encodeBase64(keyPair.publicKey);
onReady(ctx, id, lm, { onReady(ctx, id, lm, {
edPrivate: keyPair.secretKey, edPrivate: keyPair.secretKey,
@ -403,6 +432,12 @@ define([
cb(); cb();
}; };
var openTeamChat = function (ctx, data, cId, cb) {
var team = ctx.teams[data.teamId];
if (!team) { return void cb({error: 'ENOENT'}); }
ctx.store.messenger.openTeamChat(team.getChatData(), cId, cb);
};
// Remove a client from all the team they're subscribed to // Remove a client from all the team they're subscribed to
var removeClient = function (ctx, cId) { var removeClient = function (ctx, cId) {
Object.keys(ctx.teams).forEach(function (id) { Object.keys(ctx.teams).forEach(function (id) {
@ -475,7 +510,7 @@ define([
return void cb(store.proxy.teams); return void cb(store.proxy.teams);
} }
if (cmd === 'OPEN_TEAM_CHAT') { if (cmd === 'OPEN_TEAM_CHAT') {
return void ctx.store.messenger.openTeamChat(data, clientId, cb); return void openTeamChat(ctx, data, clientId, cb);
} }
if (cmd === 'CREATE_TEAM') { if (cmd === 'CREATE_TEAM') {
return void createTeam(ctx, data, clientId, cb); return void createTeam(ctx, data, clientId, cb);

@ -342,6 +342,9 @@ define([
teamId: APP.team teamId: APP.team
}, function (obj) { }, function (obj) {
console.warn(obj); console.warn(obj);
if (obj && obj.error) {
return void UI.alert(Messages.error); // XXX
}
common.setTeamChat(obj.channel); common.setTeamChat(obj.channel);
MessengerUI.create($(container), common, true); MessengerUI.create($(container), common, true);
cb(content); cb(content);

Loading…
Cancel
Save