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