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