Support mailbox in the support and admin apps

This commit is contained in:
yflory
2019-06-27 17:08:12 +02:00
parent 22c9af6961
commit 93b4dac8bb
16 changed files with 507 additions and 161 deletions

View File

@@ -89,6 +89,18 @@ define([
if (!publicKey) { return; }
return uint8ArrayToHex(Hash.decodeBase64(publicKey).subarray(0,16));
};
Hash.getBoxPublicFromSecret = function (priv) {
if (!priv) { return; }
var u8_priv = Hash.decodeBase64(priv);
var pair = Nacl.box.keyPair.fromSecretKey(u8_priv);
return Hash.encodeBase64(pair.publicKey);
};
Hash.checkBoxKeyPair = function (priv, pub) {
if (!pub || !priv) { return false; }
var u8_priv = Hash.decodeBase64(priv);
var pair = Nacl.box.keyPair.fromSecretKey(u8_priv);
return pub === Hash.encodeBase64(pair.publicKey);
};
Hash.createRandomHash = function (type, password) {
var cryptor;

View File

@@ -620,6 +620,9 @@ define([
common.adminRpc = function (data, cb) {
postMessage("ADMIN_RPC", data, cb);
};
common.addAdminMailbox = function (data, cb) {
postMessage("ADMIN_ADD_MAILBOX", data, cb);
};
// Network
common.onNetworkDisconnect = Util.mkEvent();

View File

@@ -479,7 +479,8 @@ define([
thumbnails: disableThumbnails === false,
isDriveOwned: Boolean(Util.find(store, ['driveMetadata', 'owners'])),
support: Util.find(store.proxy, ['mailboxes', 'support', 'channel']),
pendingFriends: store.proxy.friends_pending || {}
pendingFriends: store.proxy.friends_pending || {},
supportPrivateKey: Util.find(store.proxy, ['mailboxes', 'supportadmin', 'keys', 'curvePrivate'])
}
};
cb(JSON.parse(JSON.stringify(metadata)));
@@ -1060,6 +1061,26 @@ define([
cb(res);
});
};
Store.addAdminMailbox = function (clientId, data, cb) {
var priv = data;
var pub = Hash.getBoxPublicFromSecret(priv);
if (!priv || !pub) { return void cb({error: 'EINVAL'}); }
var channel = Hash.getChannelIdFromKey(pub);
var mailboxes = store.proxy.mailboxes = store.proxy.mailboxes || {};
var box = mailboxes.supportadmin = {
channel: channel,
viewed: [],
lastKnownHash: '',
keys: {
curvePublic: pub,
curvePrivate: priv
}
};
store.mailbox.open('supportadmin', box, function () {
console.log('ready');
});
onSync(cb);
};
//////////////////////////////////////////////////////////////////
/////////////////////// PAD //////////////////////////////////////

View File

@@ -10,6 +10,7 @@ define([
var TYPES = [
'notifications',
'supportadmin',
'support'
];
var BLOCKING_TYPES = [
@@ -96,10 +97,10 @@ proxy.mailboxes = {
network.join(user.channel).then(function (wc) {
wc.bcast(ciphertext).then(function () {
cb();
wc.leave();
// If we've just sent a message to one of our mailboxes, we have to trigger the handler manually
// (the server won't send back our message to us)
// If it isn't one of our mailboxes, we can close it now
var box;
if (Object.keys(ctx.boxes).some(function (t) {
var _box = ctx.boxes[t];
@@ -110,6 +111,8 @@ proxy.mailboxes = {
})) {
var hash = ciphertext.slice(0, 64);
box.onMessage(text, null, null, null, hash, user.curvePublic);
} else {
wc.leave();
}
});
}, function (err) {
@@ -200,7 +203,7 @@ proxy.mailboxes = {
if (!Crypto.Mailbox) {
return void console.error("chainpad-crypto is outdated and doesn't support mailboxes.");
}
var keys = getMyKeys(ctx);
var keys = m.keys || getMyKeys(ctx);
if (!keys) { return void console.error("missing asymmetric encryption keys"); }
var crypto = Crypto.Mailbox.createEncryptor(keys);
var cfg = {
@@ -364,6 +367,7 @@ proxy.mailboxes = {
Object.keys(mailboxes).forEach(function (key) {
if (TYPES.indexOf(key) === -1) { return; }
var m = mailboxes[key];
console.log(key, m);
if (BLOCKING_TYPES.indexOf(key) === -1) {
openChannel(ctx, key, m, function () {
@@ -386,6 +390,11 @@ proxy.mailboxes = {
});
};
mailbox.open = function (key, m, cb) {
if (TYPES.indexOf(key) === -1) { return; }
openChannel(ctx, key, m, cb);
};
mailbox.dismiss = function (data, cb) {
dismiss(ctx, data, '', cb);
};

View File

@@ -84,6 +84,7 @@ define([
DELETE_ACCOUNT: Store.deleteAccount,
// Admin
ADMIN_RPC: Store.adminRpc,
ADMIN_ADD_MAILBOX: Store.addAdminMailbox,
};
Rpc.query = function (cmd, data, cb) {

View File

@@ -123,7 +123,7 @@ define([
var onMessage = function (data) {
// data = { type: 'type', content: {msg: 'msg', hash: 'hash'} }
console.log(data.content);
console.log(data.type, data.content);
pushMessage(data);
if (!history[data.type]) { history[data.type] = []; }
history[data.type].push(data.content);