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