summaryrefslogtreecommitdiff
path: root/backend/node/src/clipperz.js
Side-by-side diff
Diffstat (limited to 'backend/node/src/clipperz.js') (more/less context) (ignore whitespace changes)
-rw-r--r--backend/node/src/clipperz.js6
1 files changed, 6 insertions, 0 deletions
diff --git a/backend/node/src/clipperz.js b/backend/node/src/clipperz.js
index 842de31..2a3df73 100644
--- a/backend/node/src/clipperz.js
+++ b/backend/node/src/clipperz.js
@@ -1,68 +1,70 @@
var FS = require('fs');
var CRYPTO = require('crypto');
var BIGNUM = require('bignum');
var ASYNC = require('async');
+var EXPRESS = require('express');
+var PATH = require('path');
var express_store = require('express-session').Store;
function clipperz_hash(v) {
return CRYPTO.createHash('sha256').update(
CRYPTO.createHash('sha256').update(v).digest('binary')
).digest('hex');
};
function clipperz_random() {
for(var r = '';r.length<64;r+=''+BIGNUM(Math.floor(Math.random()*1e18)).toString(16));
return r.substr(0,64);
};
function clipperz_store(PG) {
var rv = function(o) { express_store.call(this,o); }
rv.prototype.get = function(sid,cb) { PG.Q(
"SELECT s_data FROM clipperz.thesession WHERE s_id=$1",[sid],
function(e,r) { cb(e,(e||!r.rowCount)?null:JSON.parse(r.rows[0].s_data)); }
) };
rv.prototype.set = function(sid,data,cb) {
var d = JSON.stringify(data);
PG.Q(
"UPDATE clipperz.thesession SET s_data=$1, s_mtime=current_timestamp"
+" WHERE s_id=$2",[d,sid], function(e,r) {
if(e) return cb(e);
if(r.rowCount) return cb();
PG.Q("INSERT INTO clipperz.thesession (s_id,s_data) VALUES ($1,$2)",[sid,d],cb);
});
};
rv.prototype.destroy = function(sid,cb) { PG.Q(
"DELETE FROM clipperz.thesession WHERE s_id=$1",[sid],cb
) };
rv.prototype.length = function(cb) { PG.Q(
"SELECT count(*) AS c FROM clipperz.thesession", function(e,r) {
cb(e,e?null:r.rows[0].c);
}
) };
rv.prototype.clear = function(cb) { PQ.Q(
"TRUNCATE clipperz.thesession", cb
) };
rv.prototype.__proto__ = express_store.prototype;
return rv;
}
var srp_g = BIGNUM(2);
var srp_n = BIGNUM("115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3",16);
var srp_k = BIGNUM("64398bff522814e306a97cb9bfc4364b7eed16a8c17c5208a40a2bad2933c8e",16);
var srp_hn = "597626870978286801440197562148588907434001483655788865609375806439877501869636875571920406529";
var n123 = '112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00';
var CLIPPERZ = module.exports = function(CONFIG) {
var LOGGER = CONFIG.logger||{trace:function(){}};
var PG = {
url: CONFIG.psql,
PG: require('pg').native,
Q: function(q,a,cb) {
if('function'===typeof a) cb=a,a=[];
LOGGER.trace({query:q,args:a},'SQL: %s',q);
PG.PG.connect(PG.url,function(e,C,D) {
if(e) return cb(e);
var t0=new Date();
C.query(q,a,function(e,r) {
@@ -518,67 +520,71 @@ var CLIPPERZ = module.exports = function(CONFIG) {
+" FROM clipperz.theuser WHERE u_id=$1",[req.session.u],function(e,r) {
if(e) return cb(e);
if(!r.rowCount) return cb(new Error("user's gone AWOL"));
r = r.rows[0];
return cb(null,{u:r.u_name,d:{s:r.u_srp_s,v:r.u_srp_v, version:r.u_authversion,
maxNumberOfRecords: '100', userDetails: r.u_header,
statistics: r.u_statistics, userDetailsVersion: r.u_version
}});
});
},
records: function(cb) {
PG.Q(
"SELECT"
+" r.r_id, r.r_ref, r_data, r_version, r_ctime, r_mtime, r_atime,"
+" rv.rv_id, rv.rv_ref AS rv_ref, rv_header, rv_data, rv_version, rv_ctime, rv_mtime, rv_atime"
+" FROM"
+" clipperz.therecord AS r"
+" LEFT JOIN clipperz.therecordversion AS rv USING (r_id)"
+" WHERE r.u_id=$1"
+" ORDER BY r.r_id ASC, rv.rv_id ASC", [req.session.u],function(e,r) {
if(e) return cb(e);
var rv = {};
r.rows.forEach(function(r) {
if(!rv[r.r_ref]) rv[r.r_ref] = {
data: r.r_data, version: r.r_version,
creationDate: r.r_ctime.toString(),
updateDate: r.r_mtime.toString(),
accessDate: r.r_atime.toString(),
versions: {}
};
if(!r.rv_id) return;
rv[r.r_ref].versions[rv[r.r_ref].currentVersion=r.rv_ref] = {
header: r.rv_header, data: r.rv_data, version: r.rv_version,
creationDate: r.rv_ctime.toString(),
updateDate: r.rv_mtime.toString(),
accessDate: r.rv_atime.toString()
};
});
cb(null,rv);
});
},
html: function(cb) {
FS.readFile(CONFIG.dump_template,{encoding:'utf-8'},cb);
}
},function(e,r) {
if(e) return cb(e);
var d = new Date();
res.attachment('Clipperz_'+d.getFullYear()+'_'+(d.getMonth()+1)+'_'+d.getDate()+'.html');
var ojs = { users: {
catchAllUser: { __masterkey_test_value__: 'masterkey', s: n123, v: n123 }
} };
r.u.d.records = r.records;
ojs.users[r.u.u] = r.u.d;
res.send(r.html.replace('/*offline_data_placeholder*/',
"_clipperz_dump_data_="+JSON.stringify(ojs)
+";"
+"Clipperz.PM.Proxy.defaultProxy = new Clipperz.PM.Proxy.Offline();"
+"Clipperz.Crypto.PRNG.defaultRandomGenerator().fastEntropyAccumulationForTestingPurpose();"));
});
}
};
rv.__defineGetter__('session_store',function(){ return function(o) { return new (clipperz_store(PG))(o) } });
+ (rv.router = require('express').Router())
+ .post('/json',rv.json).get('/dump',rv.dump)
+ .use(EXPRESS.static(PATH.join(__dirname,'htdocs/')));
+
return rv;
};