latest rpc code
This commit is contained in:
123
rpc.js
123
rpc.js
@@ -1,6 +1,4 @@
|
||||
/* Use Nacl for checking signatures of messages
|
||||
|
||||
*/
|
||||
/* Use Nacl for checking signatures of messages */
|
||||
var Nacl = require("tweetnacl");
|
||||
|
||||
var RPC = module.exports;
|
||||
@@ -15,43 +13,124 @@ var isValidChannel = function (chan) {
|
||||
return /^[a-fA-F0-9]/.test(chan);
|
||||
};
|
||||
|
||||
var checkSignature = function (signedMsg, publicKey) {
|
||||
if (!(signedMsg && publicKey)) { return null; }
|
||||
var makeCookie = function (seq) {
|
||||
return [
|
||||
Math.floor(new Date() / (1000*60*60*24)),
|
||||
process.pid, // jshint ignore:line
|
||||
seq
|
||||
].join('|');
|
||||
};
|
||||
|
||||
var parseCookie = function (cookie) {
|
||||
if (!(cookie && cookie.split)) { return null; }
|
||||
|
||||
var parts = cookie.split('|');
|
||||
if (parts.length !== 3) { return null; }
|
||||
|
||||
var c = {};
|
||||
c.time = new Date(parts[0]);
|
||||
c.pid = parts[1];
|
||||
c.seq = parts[2];
|
||||
return c;
|
||||
};
|
||||
|
||||
var isValidCookie = function (ctx, cookie) {
|
||||
var now = +new Date();
|
||||
if (now - cookie.time > 300000) { // 5 minutes
|
||||
return false;
|
||||
}
|
||||
|
||||
// different process. try harder
|
||||
if (process.pid !== cookie.pid) { // jshint ignore:line
|
||||
return false;
|
||||
}
|
||||
|
||||
//if (cookie.seq !==
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
var checkSignature = function (signedMsg, signature, publicKey) {
|
||||
if (!(signedMsg && publicKey)) { return false; }
|
||||
|
||||
var signedBuffer;
|
||||
var pubBuffer;
|
||||
var signatureBuffer;
|
||||
|
||||
try {
|
||||
signedBuffer = Nacl.util.decodeBase64(signedMsg);
|
||||
pubBuffer = Nacl.util.decodeBase64(publicKey);
|
||||
signedBuffer = Nacl.util.decodeUTF8(signedMsg);
|
||||
} catch (e) {
|
||||
console.log('invalid signedBuffer');
|
||||
console.log(signedMsg);
|
||||
return null;
|
||||
}
|
||||
|
||||
var opened = Nacl.sign.open(signedBuffer, pubBuffer);
|
||||
|
||||
if (opened) {
|
||||
var decoded = Nacl.util.encodeUTF8(opened);
|
||||
try {
|
||||
return JSON.parse(decoded);
|
||||
} catch (e) { } // fall through to return
|
||||
try {
|
||||
pubBuffer = Nacl.util.decodeBase64(publicKey);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return null;
|
||||
|
||||
try {
|
||||
signatureBuffer = Nacl.util.decodeBase64(signature);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pubBuffer.length !== 32) {
|
||||
console.log('public key length: ' + pubBuffer.length);
|
||||
console.log(publicKey);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (signatureBuffer.length !== 64) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Nacl.sign.detached.verify(signedBuffer, signatureBuffer, pubBuffer);
|
||||
};
|
||||
|
||||
RPC.create = function (config, cb) {
|
||||
// load pin-store...
|
||||
|
||||
console.log('loading rpc module...');
|
||||
var rpc = function (ctx, args, respond) {
|
||||
if (args.length < 2) {
|
||||
|
||||
var Cookies = {};
|
||||
|
||||
|
||||
|
||||
var rpc = function (ctx, data, respond) {
|
||||
if (!data.length) {
|
||||
return void respond("INSUFFICIENT_ARGS");
|
||||
} else if (data.length !== 1) {
|
||||
console.log(data.length);
|
||||
}
|
||||
|
||||
var signed = args[0];
|
||||
var publicKey = args[1];
|
||||
var msg = data[0].slice(0);
|
||||
var signature = msg.shift();
|
||||
var publicKey = msg.shift();
|
||||
var cookie = parseCookie(msg.shift());
|
||||
|
||||
var msg = checkSignature(signed, publicKey);
|
||||
if (!msg) {
|
||||
if (!cookie) {
|
||||
// no cookie is fine if the RPC is to get a cookie
|
||||
if (msg[0] !== 'COOKIE') {
|
||||
return void respond('NO_COOKIE');
|
||||
}
|
||||
} else if (!isValidCookie(cookie)) { // is it a valid cookie?
|
||||
return void respond('INVALID_COOKIE');
|
||||
}
|
||||
|
||||
var serialized = JSON.stringify(msg);
|
||||
|
||||
if (!(serialized && publicKey)) {
|
||||
return void respond('INVALID_MESSAGE_OR_PUBLIC_KEY');
|
||||
}
|
||||
|
||||
if (checkSignature(serialized, signature, publicKey) !== true) {
|
||||
return void respond("INVALID_SIGNATURE_OR_PUBLIC_KEY");
|
||||
}
|
||||
|
||||
if (!msg.length) {
|
||||
return void respond("INVALID_SIGNATURE_OR_PUBLIC_KEY");
|
||||
}
|
||||
|
||||
@@ -60,6 +139,8 @@ RPC.create = function (config, cb) {
|
||||
}
|
||||
|
||||
switch (msg[0]) {
|
||||
case 'COOKIE':
|
||||
return void respond(void 0, makeCookie());
|
||||
case 'ECHO':
|
||||
return void respond(void 0, msg);
|
||||
case 'RESET':
|
||||
|
||||
Reference in New Issue
Block a user