summaryrefslogtreecommitdiff
path: root/backend
Unidiff
Diffstat (limited to 'backend') (more/less context) (ignore whitespace changes)
-rw-r--r--backend/node/properties/node.properties.json5
-rw-r--r--backend/node/src/app.js51
-rw-r--r--backend/node/src/clipperz.data.sql5
-rw-r--r--backend/node/src/clipperz.js575
-rw-r--r--backend/node/src/clipperz.schema.sql67
-rw-r--r--backend/node/src/conf.json3
-rw-r--r--backend/node/src/package.json15
7 files changed, 721 insertions, 0 deletions
diff --git a/backend/node/properties/node.properties.json b/backend/node/properties/node.properties.json
new file mode 100644
index 0000000..b5ecc1e
--- a/dev/null
+++ b/backend/node/properties/node.properties.json
@@ -0,0 +1,5 @@
1{
2 "request.path": "/json",
3 "dump.path": "/dump",
4 "should.pay.toll": "false"
5}
diff --git a/backend/node/src/app.js b/backend/node/src/app.js
new file mode 100644
index 0000000..61c2c72
--- a/dev/null
+++ b/backend/node/src/app.js
@@ -0,0 +1,51 @@
1var BUNYAN = require('bunyan');
2var LOGGER = BUNYAN.createLogger({
3 name: 'clipperz',
4 streams: [
5 { name: "console", stream:process.stderr,level:'trace'}
6 ],
7 serializers: {
8 req: BUNYAN.stdSerializers.req,
9 res: BUNYAN.stdSerializers.res,
10 err: BUNYAN.stdSerializers.err
11 },
12 src: true
13});
14
15
16var EXPRESS = require('express');
17var HTTP = require('http');
18var PATH = require('path');
19
20
21var CLIPPERZ = require('./clipperz');
22var CONF = require('./conf');
23var clipperz = CLIPPERZ({
24 psql: CONF.psql||'postgresql:///clipperz',
25 logger: LOGGER,
26 dump_template: PATH.join(__dirname,'htdocs/beta/index.html')
27});
28
29
30var app = EXPRESS();
31
32app.set('port', process.env.PORT || 3000);
33app.use(EXPRESS.logger('dev'));
34app.use(EXPRESS.urlencoded());
35app.use(EXPRESS.methodOverride());
36app.use(EXPRESS.cookieParser('your secret here'));
37app.use(EXPRESS.session({secret:'99 little bugs in the code', key:'sid', store: clipperz.session_store() }));
38app.use(app.router);
39app.use(EXPRESS.static(PATH.join(__dirname, 'htdocs/')));
40if ('development' == app.get('env')) {
41 app.use(EXPRESS.errorHandler());
42}
43
44
45app.post('/json',clipperz.json);
46app.get('/beta/dump',clipperz.dump);
47
48
49HTTP.createServer(app).listen(app.get('port'), function(){
50 LOGGER.info({port:app.get('port')},"Listener established");
51});
diff --git a/backend/node/src/clipperz.data.sql b/backend/node/src/clipperz.data.sql
new file mode 100644
index 0000000..482b196
--- a/dev/null
+++ b/backend/node/src/clipperz.data.sql
@@ -0,0 +1,5 @@
1INSERT INTO clipperz.otpstatus (otps_code,otps_name,otps_desc) VALUES
2 ('ACTIVE','Active','Active'),
3 ('REQUESTED','Requested','Requested'),
4 ('USED','Used','Used'),
5 ('DISABLED','Disabled','Disabled');
diff --git a/backend/node/src/clipperz.js b/backend/node/src/clipperz.js
new file mode 100644
index 0000000..04b054a
--- a/dev/null
+++ b/backend/node/src/clipperz.js
@@ -0,0 +1,575 @@
1var FS = require('fs');
2var CRYPTO = require('crypto');
3var BIGNUM = require('bignum');
4var ASYNC = require('async');
5
6var express_store = require('express').session.Store;
7
8function clipperz_hash(v) {
9 return CRYPTO.createHash('sha256').update(
10 CRYPTO.createHash('sha256').update(v).digest('binary')
11 ).digest('hex');
12};
13function clipperz_random() {
14 for(var r = '';r.length<64;r+=''+BIGNUM(Math.floor(Math.random()*1e18)).toString(16));
15 return r.substr(0,64);
16};
17function clipperz_store(PG) {
18 var rv = function(o) { express_store.call(this,o); }
19 rv.prototype.get = function(sid,cb) { PG.Q(
20 "SELECT s_data FROM clipperz.thesession WHERE s_id=$1",[sid],
21 function(e,r) { cb(e,(e||!r.rowCount)?null:JSON.parse(r.rows[0].s_data)); }
22 ) };
23 rv.prototype.set = function(sid,data,cb) {
24 var d = JSON.stringify(data);
25 PG.Q(
26 "UPDATE clipperz.thesession SET s_data=$1, s_mtime=current_timestamp"
27 +" WHERE s_id=$2",[d,sid], function(e,r) {
28 if(e) return cb(e);
29 if(r.rowCount) return cb();
30 PG.Q("INSERT INTO clipperz.thesession (s_id,s_data) VALUES ($1,$2)",[sid,d],cb);
31 });
32 };
33 rv.prototype.destroy = function(sid,cb) { PG.Q(
34 "DELETE FROM clipperz.thesession WHERE s_id=$1",[sid],cb
35 ) };
36 rv.prototype.length = function(cb) { PG.Q(
37 "SELECT count(*) AS c FROM clipperz.thesession", function(e,r) {
38 cb(e,e?null:r.rows[0].c);
39 }
40 ) };
41 rv.prototype.length = function(cb) { PQ.Q(
42 "DELETE FROM clipperz.thesession", cb
43 ) };
44 rv.prototype.__proto__ = express_store.prototype;
45 return rv;
46}
47
48var srp_g = BIGNUM(2);
49var srp_n = BIGNUM("115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3",16);
50var n123 = '112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00';
51
52
53var CLIPPERZ = module.exports = function(CONFIG) {
54
55 var LOGGER = CONFIG.logger||{trace:function(){}};
56
57 var PG = {
58 url: CONFIG.psql,
59 PG: require('pg').native,
60 Q: function(q,a,cb) {
61 if('function'===typeof a) cb=a,a=[];
62 LOGGER.trace({query:q,args:a},'SQL: %s',q);
63 PG.PG.connect(PG.url,function(e,C,D) {
64 if(e) return cb(e);
65 var t0=new Date();
66 C.query(q,a,function(e,r) {
67 var t1=new Date(), dt=t1-t0;
68 D();
69 LOGGER.trace({query:q,args:a,ms:dt,rows:r&&r.rowCount,err:e},"SQL query '%s' took %dms",q,dt);
70 cb(e,r);
71 });
72 });
73 },
74 T: function(cb) {
75 PG.PG.connect(PG.url,function(e,C,D) {
76 if(e) return cb(e);
77 C.query('BEGIN',function(e){
78 if(e) return D(),cb(e);
79 LOGGER.trace('SQL: transaction begun');
80 cb(null,{
81 Q: function(q,a,cb) {
82 LOGGER.trace({query:q,args:a},'SQL: %s',q);
83 if(this.over) return cb(new Error('game over'));
84 if('function'===typeof a) cb=a,a=[];
85 var t0=new Date();
86 C.query(q,a,function(e,r) {
87 var t1=new Date(), dt=t1-t0;
88 LOGGER.trace({query:q,args:a,ms:dt,rows:r&&r.rowCount,err:e},"SQL query '%s' took %dms",q,dt);
89 cb(e,r);
90 });
91 },
92 commit: function(cb) {
93 LOGGER.trace('SQL: commit');
94 if(this.over) return cb(new Error('game over'));
95 return (this.over=true),C.query('COMMIT',function(e){D();cb&&cb(e)});
96 },
97 rollback: function(cb) {
98 LOGGER.trace('SQL: rollback');
99 if(this.over) return cb(new Error('game over'));
100 return (this.over=true),C.query('ROLLBACK',function(e){D();cb&&cb(e)});
101 },
102 end: function(e,cb) {
103 if(e) return LOGGER.trace(e,"rolling back transaction due to an error"),this.rollback(cb);
104 this.commit(cb);
105 }
106 });
107 });
108 });
109 }
110 };
111
112
113 var rv = {
114
115 json: function clipperz_json(req,res,cb) {
116 var method = req.body.method, pp = JSON.parse(req.body.parameters).parameters;
117 var message = pp.message;
118 var ppp = pp.parameters;
119 res.res = function(o) { return res.json({result:o}) };
120 LOGGER.trace({method:method,parameters:pp},"JSON request");
121
122 switch(method) {
123 case 'registration':
124 switch(message) {
125 case 'completeRegistration': return PG.Q(
126 "INSERT INTO clipperz.theuser"
127 +" (u_name, u_srp_s,u_srp_v, u_authversion,u_header,u_statistics,u_version,u_lock)"
128 +" VALUES ($1, $2,$3, $4,$5,$6,$7,$8)",
129 [pp.credentials.C, pp.credentials.s, pp.credentials.v,
130 pp.credentials.version,pp.user.header, pp.user.statistics,
131 pp.user.version, pp.user.lock], function(e,r) {
132 if(e) return cb(e);
133 res.res({lock:pp.user.lock,result:'done'});
134 });
135 }
136 break;
137
138 case 'handshake':
139 switch(message) {
140 case 'connect': return ASYNC.auto({
141 u: function(cb) { PG.Q(
142 "SELECT u_id, u_srp_s, u_srp_v FROM clipperz.theuser WHERE u_name=$1",
143 [ppp.C], function(e,r) {
144 if(e) return cb(e);
145 if(!r.rowCount) return cb(null,{u_id:null,u_srp_s:n123,u_srp_v:n123});
146 cb(null,r.rows[0]);
147 }) },
148 otp: ['u',function(cb,r) {
149 if(!req.session.otp) return cb();
150 if(req.session.u!=r.u.u_id) return cb(new Error('user/OTP mismatch'));
151 PG.Q(
152 "UPDATE clipperz.theotp AS otp"
153 +" SET"
154 +" otps_id=CASE WHEN s.otps_code='REQUESTED' THEN ("
155 +" SELECT ss.otps_id FROM clipperz.otpstatus AS ss WHERE ss.otps_code='USED'"
156 +" ) ELSE otp.otps_id END,"
157 +" otp_utime=current_timestamp"
158 +" FROM clipperz.otpstatus AS s, clipperz.theotp AS o"
159 +" WHERE"
160 +" o.otp_id=otp.otp_id AND otp.otps_id=s.otps_id"
161 +" AND otp.otp_id=$1 AND otp.u_id=$2"
162 +" RETURNING o.otps_id!=otp.otps_id AS yes, o.otp_ref",
163 [ req.session.otp, req.session.u ],
164 function(e,r) {
165 if(e) return cb(e);
166 if(!r.rowCount) return cb(new Error('no OTP found'));
167 r=r.rows[0];
168 if(!r.yes) return cb(new Error('OTP is in a sorry state'));
169 cb(null,{ref:r.otp_ref});
170 });
171 }]
172 },function(e,r) {
173 if(e) return cb(e);
174 req.session.C = ppp.C; req.session.A = ppp.A;
175 req.session.s = r.u.u_srp_s; req.session.v = r.u.u_srp_v;
176 req.session.u = r.u.u_id;
177 req.session.b = clipperz_random();
178 req.session.B = BIGNUM(req.session.v,16).add(srp_g.powm(BIGNUM(req.session.b,16),srp_n)).toString(16);
179 var rv = {s:req.session.s,B:req.session.B}
180 if(r.otp && r.otp.otp_ref) rv.oneTimePassword=r.otp.otp_ref;
181 res.res(rv);
182 });
183
184 case 'credentialCheck':
185 var u = clipperz_hash(BIGNUM(req.session.B,16).toString(10));
186 var A = BIGNUM(req.session.A,16);
187 var S = A.mul(BIGNUM(req.session.v,16).powm(BIGNUM(u,16),srp_n)).powm(
188 BIGNUM(req.session.b,16), srp_n);
189 var K = clipperz_hash(S.toString(10));
190 var M1 = clipperz_hash(A.toString(10)+BIGNUM(req.session.B,16).toString(10)+K.toString(16));
191 if(M1!=ppp.M1) return res.res({error:'?'});
192 req.session.K = K;
193 var M2 = clipperz_hash(A.toString(10)+M1+K.toString(16));
194 return res.res({M2:M2,connectionId:'',loginInfo:{latest:{},current:{}},offlineCopyNeeded:false,lock:'----'});
195
196 case 'oneTimePassword': return PG.Q(
197 "UPDATE clipperz.theotp AS otp"
198 +" SET"
199 +" otps_id = CASE WHEN s.otps_code!='ACTIVE' THEN s.otps_id ELSE ("
200 +" SELECT ss.otps_id FROM clipperz.otpstatus AS ss WHERE ss.otps_code=CASE"
201 +" WHEN otp.otp_key_checksum=$2 THEN 'REQUESTED'"
202 +" ELSE 'DISABLED' END"
203 +" ) END,"
204 +" otp_data = CASE WHEN s.otps_code='ACTIVE' THEN '' ELSE otp.otp_data END,"
205 +" otp_utime = current_timestamp,"
206 +" otp_rtime = CASE WHEN otp.otp_key_checksum=$2 THEN current_timestamp ELSE otp.otp_rtime END"
207 +" FROM clipperz.otpstatus AS s, clipperz.theotp AS o"
208 +" WHERE"
209 +" o.otp_id=otp.otp_id AND otp.otps_id=s.otps_id AND otp.otp_key=$1"
210 +" RETURNING otp.u_id, s.otps_code, otp.otp_id, otp.otp_key_checksum, o.otp_data, otp.otp_version",
211 [ ppp.oneTimePasswordKey, ppp.oneTimePasswordKeyChecksum ],
212 function(e,r) {
213 if(e) return cb(e);
214 if(!r.rowCount) return cb(new Error('OTP not found'));
215 r=r.rows[0];
216 if(r.otp_key_checksum!=ppp.oneTimePasswordKeyChecksum)
217 return cb(new Error('OTP was disabled because of checksum mismatch'));
218 if(r.otps_code!='ACTIVE')
219 return cb(new Error("OTP wasn't active, sorry"));
220 req.session.u=r.u_id; req.session.otp=r.otp_id;
221 res.res({data:r.otp_data,version:r.otp_version});
222 });
223 }
224 break;
225
226 case 'message':
227 if(!req.session.K) return res.res({result:'EXCEPTION',message:"effectively, we're missing a aconnection"});
228 if(req.session.K!=pp.srpSharedSecret) return res.res({error:'Wrong shared secret!'});
229 switch(message) {
230 case 'getUserDetails': return ASYNC.parallel({
231 u: function(cb) {
232 PG.Q("SELECT u_header,u_statistics,u_version FROM clipperz.theuser WHERE u_id=$1",
233 [req.session.u],function(e,r) {
234 if(e) return cb(e);
235 if(!r.rowCount) return cb(new Error("user's gone AWOL"));
236 cb(null,r.rows[0]);
237 });
238 },
239 stats: function(cb) {
240 PG.Q("SELECT r_ref,r_mtime FROM clipperz.therecord WHERE u_id=$1",
241 [req.session.u],function(e,r) {
242 if(e) return cb(e);
243 cb(null,r.rows.reduce(function(p,r){p[r.r_ref]={updateDate:r.r_mtime};return p},{}));
244 });
245 }
246 },function(e,r) {
247 if(e) return cb(e);
248 res.res({header:r.u.u_header,statistics:r.u.u_statistics,version:r.u.u_version,recordsStats:r.stats});
249 });
250
251 case 'saveChanges': return PG.T(function(e,T) {
252 if(e) return cb(e);
253 ASYNC.auto({
254 user: function(cb) {
255 T.Q(
256 "UPDATE clipperz.theuser"
257 +" SET u_header=$1, u_statistics=$2, u_version=$3, u_lock=COALESCE($4,u_lock)"
258 +" WHERE u_id=$5"
259 +" RETURNING u_lock",[ppp.user.header,ppp.user.statistics,ppp.user.version,ppp.user.lock||null,req.session.u],
260 function(e,r) {
261 if(e) return cb(e);
262 if(!r.rowCount) return cb(new Error("user's gone AWOL"));
263 cb(null,r.rows[0]);
264 });
265 },
266 updaterecords: function(cb) {
267 if(!(ppp.records && ppp.records.updated && ppp.records.updated.length)) return cb();
268 ASYNC.each(ppp.records.updated,function(r,cb) {
269 ASYNC.auto({
270 updater: function(cb) {
271 T.Q(
272 "UPDATE clipperz.therecord"
273 +" SET r_data=$2, r_version=$3, r_mtime=current_timestamp"
274 +" WHERE r_ref=$1 AND u_id=$4 RETURNING r_id",
275 [r.record.reference,r.record.data,r.record.version,req.session.u], function(e,r) {
276 if(e) return cb(e);
277 return cb(null,r.rows.length?r.rows[0]:null);
278 });
279 },
280 insertr: ['updater',function(cb,rr) {
281 if(rr.updater) return cb();
282 T.Q(
283 "INSERT INTO clipperz.therecord"
284 +" (u_id,r_ref,r_data,r_version)"
285 +" VALUES ($1,$2,$3,$4) RETURNING r_id",[req.session.u,r.record.reference,r.record.data,r.record.version],
286 function(e,r) {
287 if(e) return cb(e);
288 return cb(null,r.rows[0]);
289 });
290 }],
291 updatev: ['updater','insertr',function(cb,rr) {
292 var crv=r.currentRecordVersion;
293 T.Q(
294 "UPDATE clipperz.therecordversion"
295 +" SET rv_ref=$1, rv_data=$2, rv_version=$3,"
296 +" rv_previous_id=COALESCE($4,rv_previous_id),"
297 +" rv_previous_key=$5, r_id=$6, rv_mtime=current_timestamp"
298 +" WHERE"
299 +" rv_id=(SELECT rv_id FROM clipperz.therecordversion WHERE r_id=$6 ORDER BY r_id ASC LIMIT 1)"
300 +" RETURNING rv_id",
301 [crv.reference,crv.data,crv.version,
302 crv.previousVersion||null,crv.previousVersionKey,
303 (rr.updater||rr.insertr).r_id],
304 function(e,r) {
305 if(e) return cb(e);
306 return cb(null,r.rows.length?r.rows[0]:null);
307 });
308 }],
309 insertv: ['updatev',function(cb,rr) {
310 if(rr.updatev) return cb();
311 var crv=r.currentRecordVersion;
312 T.Q(
313 "INSERT INTO clipperz.therecordversion"
314 +" (r_id,rv_ref,rv_data,rv_version,rv_previous_id,rv_previous_key)"
315 +" VALUES ($1,$2,$3,$4,$5,$6) RETURNING rv_id",
316 [(rr.updater||rr.insertr).r_id,
317 crv.reference, crv.data, crv.version,
318 crv.previousVersion||null,crv.previousVersionKey],
319 function(e,r) {
320 if(e) return cb(e);
321 return cb(null,r.rows[0]);
322 });
323 }]
324 },cb);
325 },cb);
326 },
327 deleterecords: function(cb) {
328 if(!(ppp.records && ppp.records.deleted && ppp.records.deleted.length)) return cb();
329 T.Q(
330 "DELETE FROM clipperz.therecord"
331 +" WHERE r_ref = ANY($1::text[]) AND u_id=$2",
332 [ '{'+ppp.records.deleted.join(',')+'}', req.session.u ], cb);
333 }
334 },function(e,r) {
335 T.end(e, function(e) {
336 if(e) return cb(e);
337 res.res({result:'done',lock:r.user.u_lock});
338 });
339 });
340 });
341
342 case 'getRecordDetail': return ASYNC.auto({ // TODO: could be done in one query instead
343 record: function(cb) {
344 PG.Q(
345 "SELECT r_id,r_ref,r_data,r_version, r_ctime, r_mtime, r_atime FROM clipperz.therecord WHERE r_ref=$1",
346 [ppp.reference],function(e,r) {
347 if(e) return cb(e);
348 if(!r.rowCount) return cb(new Error('no record found'));
349 return cb(null,r.rows[0]);
350 });
351 },
352 version: ['record',function(cb,r) {
353 PG.Q(
354 "SELECT rv_ref, rv_data, rv_header, rv_version, rv_ctime, rv_mtime, rv_atime"
355 +" FROM clipperz.therecordversion WHERE r_id=$1 ORDER BY rv_id ASC LIMIT 1",
356 [r.record.r_id],function(e,r) {
357 if(e) return cb(e);
358 if(!r.rowCount) return cb(new Error('no record version found'));
359 return cb(null,r.rows[0]);
360 });
361 }]
362 },function(e,r) {
363 if(e) return cb(e);
364 var v = {};
365 v[r.version.rv_ref] = {
366 reference: r.version.rv_ref,
367 data: r.version.rv_data, header: r.version.rv_header,
368 version: r.version.rv_version,
369 creationDate: r.version.rv_ctime, updateDate: r.version.rv_mtime, accessDate: r.version.rv_atime
370 };
371 res.res({ versions: v, currentVersion: r.version.rv_ref, reference: r.record.r_ref,
372 data: r.record.r_data, version: r.record.r_version,
373 creationDate: r.record.r_ctime, updateDate: r.record.r_mtime, accessDate: r.record.r_atime,
374 oldestUsedEncryptedVersion: '---' });
375 });
376
377 case 'addNewOneTimePassword': return PG.T(function(e,T) {
378 if(e) return cb(e);
379 ASYNC.parallel({
380 otp: function(cb) {
381 var otp = ppp.oneTimePassword;
382 T.Q(
383 "INSERT INTO clipperz.theotp"
384 +" (u_id,otp_ref,otp_key,otp_key_checksum,otp_data,otp_version,otps_id)"
385 +" SELECT $1,$2,$3,$4,$5,$6,otps_id FROM clipperz.otpstatus"
386 +" WHERE otps_code='ACTIVE'",
387 [ req.session.u, otp.reference, otp.key, otp.keyChecksum,
388 otp.data, otp.version], function(e,r) {
389 if(e) return cb(e);
390 if(!r.rowCount) return cb(new Error('no user or status'));
391 cb();
392 });
393 },
394 user: function(cb) {
395 var u = ppp.user;
396 T.Q(
397 "UPDATE clipperz.theuser"
398 +" SET u_header=$1, u_statistics=$2, u_version=$3, u_lock=COALESCE($4,u_lock)"
399 +" WHERE u_id=$5",
400 [ u.header, u.statistics, u.version, u.lock||null, req.session.u],
401 function(e,r) {
402 if(e) return cb(e);
403 if(!r.rowCount) return cb(new Error("user's gone AWOL"));
404 cb();
405 });
406 }
407 },function(e,r) {
408 T.end(e, function(e) {
409 if(e) return cb(e);
410 res.res({result:'done',lock:ppp.user.lock});
411 });
412 });
413 });
414
415 case 'updateOneTimePasswords': return PG.T(function(e,T) {
416 if(e) return cb(e);
417 ASYNC.parallel({
418 otp: function(cb) {
419 T.Q(
420 "DELETE FROM clipperz.theotp"
421 +" WHERE u_id=$1"
422 +" AND NOT otp_ref = ANY($2::text[])",
423 [ req.session.u,'{'+ppp.oneTimePasswords.join(',')+'}' ],cb);
424 },
425 user: function(cb) {
426 var u = ppp.user;
427 T.Q(
428 "UPDATE clipperz.theuser"
429 +" SET u_header=$1, u_statistics=$2, u_version=$3, u_lock=COALESCE($4,u_lock)"
430 +" WHERE u_id=$5",
431 [ u.header, u.statistics, u.version, u.lock||null, req.session.u],
432 function(e,r) {
433 if(e) return cb(e);
434 if(!r.rowCount) return cb(new Error("user's gone AWOL"));
435 cb();
436 });
437 }
438 },function(e,r) {
439 T.end(e, function(e) {
440 if(e) return cb(e);
441 res.res({result:ppp.user.lock});
442 });
443 });
444 });
445
446 case 'upgradeUserCredentials': return PG.T(function(e,T) {
447 if(e) return cb(e);
448 ASYNC.parallel({
449 user: function(cb) {
450 var u = ppp.user, c = ppp.credentials;
451 T.Q(
452 "UPDATE clipperz.theuser"
453 +" SET u_header=$1, u_statistics=$2, u_version=$3, u_lock=COALESCE($4,u_lock),"
454 +" u_name=$5, u_srp_s=$6, u_srp_v=$7, u_authversion=$8"
455 +" WHERE u_id=$9 RETURNING u_lock",
456 [ u.header,u.statistics,u.version,u.lock||null,
457 c.C,c.s,c.v,c.version, req.session.u ],function(e,r) {
458 if(e) return cb(e);
459 if(!r.rowCount) return cb(new Error("user's gone AWOL"));
460 cb(e,r.rows[0]);
461 });
462 },
463 otp: function(cb) {
464 var otps=ppp.oneTimePasswords;
465 if(!otps) return cb();
466 ASYNC.each(Object.keys(otps),function(r,cb) {
467 T.Q(
468 "UPDATE clipperz.theotp"
469 +" SET otp_data=$1, otp_utime=current_timestamp WHERE otp_ref=$2 AND u_id=$3",
470 [ otps[r], r, req.session.u ], function(e,r) {
471 if(e) return cb(e);
472 if(!r.rowCount) return cb(new Error("OTP's gone AWOL"));
473 cb();
474 });
475 },cb);
476 }
477 },function(e,r) {
478 T.end(e, function(e) {
479 if(e) return cb(e);
480 res.res({result:'done',lock:r.user.u_lock});
481 });
482 });
483 });
484
485 case 'deleteUser': return PG.Q(
486 "DELETE FROM clipperz.theuser WHERE u_id=$1",
487 [req.session.u],function(e,r) {
488 if(e) return cb(e);
489 res.res({result:'ok'});
490 });
491
492 case 'echo': return res.res({result:ppp});
493 case 'getOneTimePasswordsDetails': return res.res({});
494 case 'getLoginHistory': return res.res({result:[]});
495 }
496 break;
497 case 'logout': return req.session.destroy(function(e){res.res({})});
498 }
499 cb();
500 },
501
502 dump: function(req,res,cb) {
503 if(!req.session.u) return cb(new Error('logging in helps'));
504 return ASYNC.parallel({
505 u: function(cb) {
506 PG.Q(
507 "SELECT"
508 +" u_name, u_srp_s, u_srp_v, u_authversion, u_header, u_statistics, u_version"
509 +" FROM clipperz.theuser WHERE u_id=$1",[req.session.u],function(e,r) {
510 if(e) return cb(e);
511 if(!r.rowCount) return cb(new Error("user's gone AWOL"));
512 r = r.rows[0];
513 return cb(null,{u:r.u_name,d:{s:r.u_srp_s,v:r.u_srp_v, version:r.u_authversion,
514 maxNumberOfRecords: '100', userDetails: r.u_header,
515 statistics: r.u_statistics, userDetailsVersion: r.u_version
516 }});
517 });
518 },
519 records: function(cb) {
520 PG.Q(
521 "SELECT"
522 +" r.r_id, r.r_ref, r_data, r_version, r_ctime, r_mtime, r_atime,"
523 +" rv.rv_id, rv.rv_ref AS rv_ref, rv_header, rv_data, rv_version, rv_ctime, rv_mtime, rv_atime"
524 +" FROM"
525 +" clipperz.therecord AS r"
526 +" LEFT JOIN clipperz.therecordversion AS rv USING (r_id)"
527 +" WHERE r.u_id=$1"
528 +" ORDER BY r.r_id ASC, rv.rv_id ASC", [req.session.u],function(e,r) {
529 if(e) return cb(e);
530 var rv = {};
531 r.rows.forEach(function(r) {
532 if(!rv[r.r_ref]) rv[r.r_ref] = {
533 data: r.r_data, version: r.r_version,
534 creationDate: r.r_ctime.toString(),
535 updateDate: r.r_mtime.toString(),
536 accessDate: r.r_atime.toString(),
537 versions: {}
538 };
539 if(!r.rv_id) return;
540 rv[r.r_ref].versions[rv[r.r_ref].currentVersion=r.rv_ref] = {
541 header: r.rv_header, data: r.rv_data, version: r.rv_version,
542 creationDate: r.rv_ctime.toString(),
543 updateDate: r.rv_mtime.toString(),
544 accessDate: r.rv_atime.toString()
545 };
546 });
547 cb(null,rv);
548 });
549 },
550 html: function(cb) {
551 FS.readFile(CONFIG.dump_template,{encoding:'utf-8'},cb);
552 }
553 },function(e,r) {
554 if(e) return cb(e);
555 var d = new Date();
556 res.attachment('Clipperz_'+d.getFullYear()+'_'+(d.getMonth()+1)+'_'+d.getDate()+'.html');
557 var ojs = { users: {
558 catchAllUser: { __masterkey_test_value__: 'masterkey', s: n123, v: n123 }
559 } };
560 r.u.d.records = r.records;
561 ojs.users[r.u.u] = r.u.d;
562 res.send(r.html.replace('/*offline_data_placeholder*/',
563 "_clipperz_dump_data_="+JSON.stringify(ojs)
564 +";"
565 +"Clipperz.PM.Proxy.defaultProxy = new Clipperz.PM.Proxy.Offline();"
566 +"Clipperz.Crypto.PRNG.defaultRandomGenerator().fastEntropyAccumulationForTestingPurpose();"));
567 });
568 }
569
570 };
571 rv.__defineGetter__('session_store',function(){ return function(o) { return new (clipperz_store(PG))(o) } });
572
573 return rv;
574
575};
diff --git a/backend/node/src/clipperz.schema.sql b/backend/node/src/clipperz.schema.sql
new file mode 100644
index 0000000..591828a
--- a/dev/null
+++ b/backend/node/src/clipperz.schema.sql
@@ -0,0 +1,67 @@
1CREATE SCHEMA clipperz;
2
3CREATE TABLE clipperz.theuser (
4 u_id serial PRIMARY KEY,
5 u_name varchar NOT NULL UNIQUE,
6 u_srp_s varchar NOT NULL,
7 u_srp_v varchar NOT NULL,
8 u_header varchar NOT NULL,
9 u_statistics varchar NOT NULL,
10 u_authversion varchar NOT NULL,
11 u_version varchar NOT NULL,
12 u_lock varchar NOT NULL
13);
14
15CREATE TABLE clipperz.therecord (
16 r_id serial PRIMARY KEY,
17 u_id integer NOT NULL REFERENCES clipperz.theuser(u_id) ON UPDATE CASCADE ON DELETE CASCADE,
18 r_ref varchar NOT NULL UNIQUE,
19 r_data varchar NOT NULL,
20 r_version varchar NOT NULL,
21 r_ctime timestamp NOT NULL DEFAULT current_timestamp,
22 r_mtime timestamp NOT NULL DEFAULT current_timestamp,
23 r_atime timestamp NOT NULL DEFAULT current_timestamp
24);
25CREATE INDEX therecord_u_id_key ON clipperz.therecord (u_id);
26
27CREATE TABLE clipperz.therecordversion (
28 rv_id serial PRIMARY KEY,
29 r_id integer NOT NULL REFERENCES clipperz.therecord (r_id) ON UPDATE CASCADE ON DELETE CASCADE,
30 rv_ref varchar NOT NULL UNIQUE,
31 rv_header varchar,
32 rv_data varchar NOT NULL,
33 rv_version varchar NOT NULL,
34 rv_previous_key varchar NOT NULL,
35 rv_previous_id varchar,
36 rv_ctime timestamp NOT NULL DEFAULT current_timestamp,
37 rv_mtime timestamp NOT NULL DEFAULT current_timestamp,
38 rv_atime timestamp NOT NULL DEFAULT current_timestamp
39);
40
41CREATE TABLE clipperz.otpstatus (
42 otps_id serial PRIMARY KEY,
43 otps_code varchar NOT NULL,
44 otps_name varchar NOT NULL,
45 otps_desc varchar NOT NULL
46);
47
48CREATE TABLE clipperz.theotp (
49 otp_id serial PRIMARY KEY,
50 u_id integer REFERENCES clipperz.theuser (u_id) ON UPDATE CASCADE ON DELETE CASCADE,
51 otps_id integer REFERENCES clipperz.otpstatus (otps_id) ON UPDATE CASCADE ON DELETE CASCADE,
52 otp_ref varchar NOT NULL UNIQUE,
53 otp_key varchar NOT NULL UNIQUE,
54 otp_key_checksum varchar NOT NULL,
55 otp_data varchar NOT NULL,
56 otp_version varchar NOT NULL,
57 otp_ctime timestamp NOT NULL DEFAULT current_timestamp,
58 otp_rtime timestamp NOT NULL DEFAULT current_timestamp,
59 otp_utime timestamp NOT NULL DEFAULT current_timestamp
60);
61
62CREATE TABLE clipperz.thesession (
63 s_id varchar PRIMARY KEY,
64 s_data varchar,
65 s_ctime timestamp DEFAULT current_timestamp,
66 s_mtime timestamp DEFAULT current_timestamp
67);
diff --git a/backend/node/src/conf.json b/backend/node/src/conf.json
new file mode 100644
index 0000000..bc1f65e
--- a/dev/null
+++ b/backend/node/src/conf.json
@@ -0,0 +1,3 @@
1{
2 "psql": "postgresql:///clipperz"
3}
diff --git a/backend/node/src/package.json b/backend/node/src/package.json
new file mode 100644
index 0000000..825f756
--- a/dev/null
+++ b/backend/node/src/package.json
@@ -0,0 +1,15 @@
1{
2 "name": "clipperz",
3 "version": "0.0.0",
4 "private": true,
5 "scripts": {
6 "start": "node app 2>&1 | ./node_modules/.bin/bunyan"
7 },
8 "dependencies": {
9 "express": "3.4.4",
10 "async": "~0.2.9",
11 "bignum": "~0.6.2",
12 "pg": "~2.8.3",
13 "bunyan": "~0.22.0"
14 }
15}