author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
commit | ef68436ac04da078ffdcacd7e1f785473a303d45 (patch) (unidiff) | |
tree | c403752d66a2c4775f00affd4fa8431b29c5b68c /frontend/gamma/js/Clipperz/PM/Proxy | |
parent | 597ecfbc0249d83e1b856cbd558340c01237a360 (diff) | |
download | clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.zip clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.gz clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.bz2 |
First version of the newly restructured repository
Diffstat (limited to 'frontend/gamma/js/Clipperz/PM/Proxy') (more/less context) (ignore whitespace changes)
-rwxr-xr-x | frontend/gamma/js/Clipperz/PM/Proxy/Proxy.JSON.js | 94 | ||||
-rw-r--r-- | frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.DataStore.js | 811 | ||||
-rw-r--r-- | frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.js | 67 | ||||
-rw-r--r-- | frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Test.js | 167 |
4 files changed, 1139 insertions, 0 deletions
diff --git a/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.JSON.js b/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.JSON.js new file mode 100755 index 0000000..65b46de --- a/dev/null +++ b/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.JSON.js | |||
@@ -0,0 +1,94 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer to http://www.clipperz.com | ||
12 | |||
13 | * Javascript Crypto Library is free software: you can redistribute | ||
14 | it and/or modify it under the terms of the GNU Affero General Public | ||
15 | License as published by the Free Software Foundation, either version | ||
16 | 3 of the License, or (at your option) any later version. | ||
17 | |||
18 | * Javascript Crypto Library is distributed in the hope that it will | ||
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
21 | See the GNU Affero General Public License for more details. | ||
22 | |||
23 | * You should have received a copy of the GNU Affero General Public | ||
24 | License along with Javascript Crypto Library. If not, see | ||
25 | <http://www.gnu.org/licenses/>. | ||
26 | |||
27 | */ | ||
28 | |||
29 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
30 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | ||
31 | |||
32 | //============================================================================= | ||
33 | |||
34 | Clipperz.PM.Proxy.JSON = function(args) { | ||
35 | Clipperz.PM.Proxy.JSON.superclass.constructor.call(this, args); | ||
36 | |||
37 | this._url = args.url || Clipperz.Base.exception.raise('MandatoryParameter'); | ||
38 | |||
39 | return this; | ||
40 | } | ||
41 | |||
42 | Clipperz.Base.extend(Clipperz.PM.Proxy.JSON, Clipperz.PM.Proxy, { | ||
43 | |||
44 | 'toString': function() { | ||
45 | return "Clipperz.PM.Proxy.JSON"; | ||
46 | }, | ||
47 | |||
48 | //========================================================================= | ||
49 | |||
50 | 'url': function () { | ||
51 | return this._url; | ||
52 | }, | ||
53 | |||
54 | //========================================================================= | ||
55 | |||
56 | 'sendMessage': function(aFunctionName, someParameters) { | ||
57 | vardeferredResult; | ||
58 | var parameters; | ||
59 | |||
60 | parameters = { | ||
61 | method: aFunctionName, | ||
62 | // version: someParameters['version'], | ||
63 | // message: someParameters['message'], | ||
64 | parameters: Clipperz.Base.serializeJSON(someParameters) | ||
65 | }; | ||
66 | |||
67 | deferredResult = new Clipperz.Async.Deferred("Proxy.JSON.sendMessage", {trace:false}); | ||
68 | deferredResult.addCallbackPass(MochiKit.Signal.signal, Clipperz.Signal.NotificationCenter, 'remoteRequestSent'); | ||
69 | deferredResult.addCallback(MochiKit.Async.doXHR, this.url(), { | ||
70 | method:'POST', | ||
71 | sendContent:MochiKit.Base.queryString(parameters), | ||
72 | headers:{"Content-Type":"application/x-www-form-urlencoded"} | ||
73 | }); | ||
74 | deferredResult.addCallbackPass(MochiKit.Signal.signal, Clipperz.Signal.NotificationCenter, 'remoteRequestReceived'); | ||
75 | // deferredResult.addCallback(MochiKit.Async.evalJSONRequest); | ||
76 | deferredResult.addCallback(MochiKit.Base.itemgetter('responseText')); | ||
77 | deferredResult.addCallback(Clipperz.Base.evalJSON); | ||
78 | deferredResult.addCallback(function (someValues) { | ||
79 | if (someValues['result'] == 'EXCEPTION') { | ||
80 | throw someValues['message']; | ||
81 | } | ||
82 | |||
83 | return someValues; | ||
84 | }) | ||
85 | // return MochiKit.Base.evalJSON(req.responseText); | ||
86 | deferredResult.callback(); | ||
87 | |||
88 | return deferredResult; | ||
89 | }, | ||
90 | |||
91 | //========================================================================= | ||
92 | __syntaxFix__: "syntax fix" | ||
93 | |||
94 | }); | ||
diff --git a/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.DataStore.js b/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.DataStore.js new file mode 100644 index 0000000..de8e7f6 --- a/dev/null +++ b/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.DataStore.js | |||
@@ -0,0 +1,811 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer to http://www.clipperz.com | ||
12 | |||
13 | * Javascript Crypto Library is free software: you can redistribute | ||
14 | it and/or modify it under the terms of the GNU Affero General Public | ||
15 | License as published by the Free Software Foundation, either version | ||
16 | 3 of the License, or (at your option) any later version. | ||
17 | |||
18 | * Javascript Crypto Library is distributed in the hope that it will | ||
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
21 | See the GNU Affero General Public License for more details. | ||
22 | |||
23 | * You should have received a copy of the GNU Affero General Public | ||
24 | License along with Javascript Crypto Library. If not, see | ||
25 | <http://www.gnu.org/licenses/>. | ||
26 | |||
27 | */ | ||
28 | |||
29 | try { if (typeof(Clipperz.PM.Proxy.Offline) == 'undefined') { throw ""; }} catch (e) { | ||
30 | throw "Clipperz.PM.Proxy.Offline.DataStore depends on Clipperz.PM.Proxy.Offline!"; | ||
31 | } | ||
32 | |||
33 | //============================================================================= | ||
34 | |||
35 | Clipperz.PM.Proxy.Offline.DataStore = function(args) { | ||
36 | args = args || {}; | ||
37 | |||
38 | this._data = args.data || (typeof(_clipperz_dump_data_) != 'undefined' ? _clipperz_dump_data_ : null); | ||
39 | this._isReadOnly = (typeof(args.readOnly) == 'undefined' ? true : args.readOnly); | ||
40 | this._shouldPayTolls = args.shouldPayTolls || false; | ||
41 | |||
42 | this._tolls = {}; | ||
43 | this._currentStaticConnection = null; | ||
44 | |||
45 | return this; | ||
46 | } | ||
47 | |||
48 | Clipperz.Base.extend(Clipperz.PM.Proxy.Offline.DataStore, Object, { | ||
49 | |||
50 | //------------------------------------------------------------------------- | ||
51 | |||
52 | 'isReadOnly': function () { | ||
53 | return this._isReadOnly; | ||
54 | }, | ||
55 | |||
56 | //------------------------------------------------------------------------- | ||
57 | |||
58 | 'shouldPayTolls': function() { | ||
59 | return this._shouldPayTolls; | ||
60 | }, | ||
61 | |||
62 | //------------------------------------------------------------------------- | ||
63 | |||
64 | 'data': function () { | ||
65 | return this._data; | ||
66 | }, | ||
67 | |||
68 | //------------------------------------------------------------------------- | ||
69 | |||
70 | 'tolls': function () { | ||
71 | return this._tolls; | ||
72 | }, | ||
73 | |||
74 | //========================================================================= | ||
75 | |||
76 | 'resetData': function() { | ||
77 | this._data = { | ||
78 | 'users': { | ||
79 | 'catchAllUser': { | ||
80 | __masterkey_test_value__: 'masterkey', | ||
81 | s: '112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00', | ||
82 | v: '112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00' | ||
83 | } | ||
84 | } | ||
85 | }; | ||
86 | }, | ||
87 | |||
88 | //------------------------------------------------------------------------- | ||
89 | |||
90 | 'setupWithEncryptedData': function(someData) { | ||
91 | this._data = Clipperz.Base.deepClone(someData); | ||
92 | }, | ||
93 | |||
94 | //------------------------------------------------------------------------- | ||
95 | |||
96 | 'setupWithData': function(someData) { | ||
97 | var deferredResult; | ||
98 | var resultData; | ||
99 | var i, c; | ||
100 | |||
101 | //Clipperz.log(">>> Proxy.Test.setupWithData"); | ||
102 | resultData = this._data; | ||
103 | |||
104 | deferredResult = new Clipperz.Async.Deferred("Proxy.Test.seupWithData", {trace:false}); | ||
105 | c = someData['users'].length; | ||
106 | |||
107 | for (i=0; i<c; i++) { | ||
108 | varnewConnection; | ||
109 | varrecordConfiguration; | ||
110 | |||
111 | deferredResult.addMethod(this, 'userSerializedEncryptedData', someData['users'][i]); | ||
112 | deferredResult.addCallback(MochiKit.Base.bind(function(aUserSerializationContext) { | ||
113 | //console.log("SERIALIZED USER", aUserSerializationContext); | ||
114 | resultData['users'][aUserSerializationContext['credentials']['C']] = { | ||
115 | 's': aUserSerializationContext['credentials']['s'], | ||
116 | 'v': aUserSerializationContext['credentials']['v'], | ||
117 | 'version': aUserSerializationContext['data']['connectionVersion'], | ||
118 | 'userDetails': aUserSerializationContext['encryptedData']['user']['header'], | ||
119 | 'userDetailsVersion':aUserSerializationContext['encryptedData']['user']['version'], | ||
120 | 'statistics': aUserSerializationContext['encryptedData']['user']['statistics'], | ||
121 | 'lock': aUserSerializationContext['encryptedData']['user']['lock'], | ||
122 | 'records': this.rearrangeRecordsData(aUserSerializationContext['encryptedData']['records']) | ||
123 | } | ||
124 | }, this)); | ||
125 | } | ||
126 | |||
127 | deferredResult.addCallback(MochiKit.Base.bind(function() { | ||
128 | //console.log("this._data", resultData); | ||
129 | this._data = resultData; | ||
130 | }, this)); | ||
131 | |||
132 | deferredResult.callback(); | ||
133 | //Clipperz.log("<<< Proxy.Test.setupWithData"); | ||
134 | |||
135 | return deferredResult; | ||
136 | }, | ||
137 | |||
138 | //========================================================================= | ||
139 | |||
140 | 'getTollForRequestType': function (aRequestType) { | ||
141 | varresult; | ||
142 | vartargetValue; | ||
143 | var cost; | ||
144 | |||
145 | targetValue = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32).toHexString().substring(2); | ||
146 | switch (aRequestType) { | ||
147 | case 'REGISTER': | ||
148 | cost = 5; | ||
149 | break; | ||
150 | case 'CONNECT': | ||
151 | cost = 5; | ||
152 | break; | ||
153 | case 'MESSAGE': | ||
154 | cost = 2; | ||
155 | break; | ||
156 | } | ||
157 | |||
158 | result = { | ||
159 | requestType: aRequestType, | ||
160 | targetValue: targetValue, | ||
161 | cost: cost | ||
162 | } | ||
163 | |||
164 | if (this.shouldPayTolls()) { | ||
165 | this.tolls()[targetValue] = result; | ||
166 | } | ||
167 | |||
168 | return result; | ||
169 | }, | ||
170 | |||
171 | //------------------------------------------------------------------------- | ||
172 | |||
173 | 'checkToll': function (aFunctionName, someParameters) { | ||
174 | if (this.shouldPayTolls()) { | ||
175 | var localToll; | ||
176 | vartollParameters; | ||
177 | |||
178 | tollParameters = someParameters['toll']; | ||
179 | localToll = this.tolls()[tollParameters['targetValue']]; | ||
180 | |||
181 | if (localToll != null) { | ||
182 | if (! Clipperz.PM.Toll.validate(tollParameters['targetValue'], tollParameters['toll'], localToll['cost'])) { | ||
183 | throw "Toll value too low."; | ||
184 | }; | ||
185 | } else { | ||
186 | throw "Missing toll"; | ||
187 | } | ||
188 | } | ||
189 | }, | ||
190 | |||
191 | //========================================================================= | ||
192 | |||
193 | 'currentStaticConnection': function () { | ||
194 | if (this._currentStaticConnection == null) { | ||
195 | this._currentStaticConnection = {}; | ||
196 | } | ||
197 | |||
198 | return this._currentStaticConnection; | ||
199 | }, | ||
200 | |||
201 | //------------------------------------------------------------------------- | ||
202 | |||
203 | 'getConnectionForRequest': function (aFunctionName, someParameters) { | ||
204 | varresult; | ||
205 | |||
206 | if (this.shouldPayTolls()) { | ||
207 | if ((typeof(someParameters['toll']) != 'undefined') && (typeof(someParameters['toll']['targetValue']) != 'undefined')) { | ||
208 | result = this.tolls()[someParameters['toll']['targetValue']]['connection']; | ||
209 | if (typeof(result) == 'undefined') { | ||
210 | result = {}; | ||
211 | } | ||
212 | } else { | ||
213 | result = {}; | ||
214 | } | ||
215 | } else { | ||
216 | result = this.currentStaticConnection(); | ||
217 | } | ||
218 | |||
219 | return result; | ||
220 | }, | ||
221 | |||
222 | //------------------------------------------------------------------------- | ||
223 | |||
224 | 'storeConnectionForRequestWithConnectionAndResponse': function (aFunctionName, someParameters, aConnection, aResponse) { | ||
225 | if (this.shouldPayTolls()) { | ||
226 | if ((typeof(aResponse['toll']) != 'undefined') | ||
227 | &&(typeof(aResponse['toll']['targetValue']) != 'undefined') | ||
228 | &&(typeof(this.tolls()[aResponse['toll']['targetValue']]) != 'undefined') | ||
229 | ) { | ||
230 | this.tolls()[aResponse['toll']['targetValue']]['connection'] = aConnection; | ||
231 | } | ||
232 | } | ||
233 | }, | ||
234 | |||
235 | //========================================================================= | ||
236 | |||
237 | 'processMessage': function (aFunctionName, someParameters) { | ||
238 | var result; | ||
239 | varconnection; | ||
240 | |||
241 | connection = this.getConnectionForRequest(aFunctionName, someParameters); | ||
242 | |||
243 | switch(aFunctionName) { | ||
244 | case 'knock': | ||
245 | result = this._knock(connection, someParameters); | ||
246 | break; | ||
247 | case 'registration': | ||
248 | this.checkToll(aFunctionName, someParameters); | ||
249 | result = this._registration(connection, someParameters.parameters); | ||
250 | break; | ||
251 | case 'handshake': | ||
252 | this.checkToll(aFunctionName, someParameters); | ||
253 | result = this._handshake(connection, someParameters.parameters); | ||
254 | break; | ||
255 | case 'message': | ||
256 | this.checkToll(aFunctionName, someParameters); | ||
257 | result = this._message(connection, someParameters.parameters); | ||
258 | break; | ||
259 | case 'logout': | ||
260 | this._currentStaticConnection = null; | ||
261 | result = this._logout(connection, someParameters.parameters); | ||
262 | break; | ||
263 | } | ||
264 | |||
265 | this.storeConnectionForRequestWithConnectionAndResponse(aFunctionName, someParameters, connection, result); | ||
266 | |||
267 | return MochiKit.Async.succeed(result); | ||
268 | }, | ||
269 | |||
270 | //========================================================================= | ||
271 | |||
272 | '_knock': function(aConnection, someParameters) { | ||
273 | var result; | ||
274 | |||
275 | result = { | ||
276 | toll: this.getTollForRequestType(someParameters['requestType']) | ||
277 | } | ||
278 | |||
279 | return result; | ||
280 | }, | ||
281 | |||
282 | //------------------------------------------------------------------------- | ||
283 | |||
284 | '_registration': function(aConnection, someParameters) { | ||
285 | if (this.isReadOnly() == false) { | ||
286 | if (typeof(this.data()['users'][someParameters['credentials']['C']]) == 'undefined') { | ||
287 | this.data()['users'][someParameters['credentials']['C']] = { | ||
288 | 's': someParameters['credentials']['s'], | ||
289 | 'v': someParameters['credentials']['v'], | ||
290 | 'version':someParameters['credentials']['version'], | ||
291 | 'lock': Clipperz.Crypto.Base.generateRandomSeed(), | ||
292 | 'userDetails': someParameters['user']['header'], | ||
293 | 'statistics': someParameters['user']['statistics'], | ||
294 | 'userDetailsVersion':someParameters['user']['version'], | ||
295 | 'records':{} | ||
296 | } | ||
297 | } else { | ||
298 | throw "user already exists"; | ||
299 | } | ||
300 | } else { | ||
301 | throw Clipperz.PM.Proxy.Offline.DataStore.exception.ReadOnly; | ||
302 | } | ||
303 | |||
304 | result = { | ||
305 | result: { | ||
306 | 'lock': this.data()['users'][someParameters['credentials']['C']]['lock'], | ||
307 | 'result':'done' | ||
308 | }, | ||
309 | toll: this.getTollForRequestType('CONNECT') | ||
310 | } | ||
311 | |||
312 | return result; | ||
313 | }, | ||
314 | |||
315 | //------------------------------------------------------------------------- | ||
316 | |||
317 | '_handshake': function(aConnection, someParameters) { | ||
318 | var result; | ||
319 | varnextTollRequestType; | ||
320 | |||
321 | result = {}; | ||
322 | if (someParameters.message == "connect") { | ||
323 | var userData; | ||
324 | var randomBytes; | ||
325 | var v; | ||
326 | |||
327 | userData = this.data()['users'][someParameters.parameters.C]; | ||
328 | |||
329 | if ((typeof(userData) != 'undefined') && (userData['version'] == someParameters.version)) { | ||
330 | aConnection['userData'] = userData; | ||
331 | aConnection['C'] = someParameters.parameters.C; | ||
332 | } else { | ||
333 | aConnection['userData'] = this.data()['users']['catchAllUser']; | ||
334 | } | ||
335 | |||
336 | randomBytes = Clipperz.Crypto.Base.generateRandomSeed(); | ||
337 | aConnection['b'] = new Clipperz.Crypto.BigInt(randomBytes, 16); | ||
338 | v = new Clipperz.Crypto.BigInt(aConnection['userData']['v'], 16); | ||
339 | aConnection['B'] = v.add(Clipperz.Crypto.SRP.g().powerModule(aConnection['b'], Clipperz.Crypto.SRP.n())); | ||
340 | |||
341 | aConnection['A'] = someParameters.parameters.A; | ||
342 | |||
343 | result['s'] = aConnection['userData']['s']; | ||
344 | result['B'] = aConnection['B'].asString(16); | ||
345 | |||
346 | nextTollRequestType = 'CONNECT'; | ||
347 | } else if (someParameters.message == "credentialCheck") { | ||
348 | var v, u, S, A, K, M1; | ||
349 | |||
350 | v = new Clipperz.Crypto.BigInt(aConnection['userData']['v'], 16); | ||
351 | u = new Clipperz.Crypto.BigInt(Clipperz.PM.Crypto.encryptingFunctions.versions[someParameters.version].hash(new Clipperz.ByteArray(aConnection['B'].asString(10))).toHexString(), 16); | ||
352 | A = new Clipperz.Crypto.BigInt(aConnection['A'], 16); | ||
353 | S = (A.multiply(v.powerModule(u, Clipperz.Crypto.SRP.n()))).powerModule(aConnection['b'], Clipperz.Crypto.SRP.n()); | ||
354 | |||
355 | K = Clipperz.PM.Crypto.encryptingFunctions.versions[someParameters.version].hash(new Clipperz.ByteArray(S.asString(10))).toHexString().slice(2); | ||
356 | |||
357 | M1 = Clipperz.PM.Crypto.encryptingFunctions.versions[someParameters.version].hash(new Clipperz.ByteArray(A.asString(10) + aConnection['B'].asString(10) + K)).toHexString().slice(2); | ||
358 | if (someParameters.parameters.M1 == M1) { | ||
359 | var M2; | ||
360 | |||
361 | M2 = Clipperz.PM.Crypto.encryptingFunctions.versions[someParameters.version].hash(new Clipperz.ByteArray(A.asString(10) + someParameters.parameters.M1 + K)).toHexString().slice(2); | ||
362 | result['M2'] = M2; | ||
363 | } else { | ||
364 | throw new Error("Client checksum verification failed! Expected <" + M1 + ">, received <" + someParameters.parameters.M1 + ">.", "Error"); | ||
365 | } | ||
366 | |||
367 | nextTollRequestType = 'MESSAGE'; | ||
368 | } else if (someParameters.message == "oneTimePassword") { | ||
369 | var otpData; | ||
370 | |||
371 | otpData = this.data()['onetimePasswords'][someParameters.parameters.oneTimePasswordKey]; | ||
372 | |||
373 | try { | ||
374 | if (typeof(otpData) != 'undefined') { | ||
375 | if (otpData['status'] == 'ACTIVE') { | ||
376 | if (otpData['key_checksum'] == someParameters.parameters.oneTimePasswordKeyChecksum) { | ||
377 | result = { | ||
378 | 'data': otpData['data'], | ||
379 | 'version':otpData['version'] | ||
380 | } | ||
381 | |||
382 | otpData['status'] = 'REQUESTED'; | ||
383 | } else { | ||
384 | otpData['status'] = 'DISABLED'; | ||
385 | throw "The requested One Time Password has been disabled, due to a wrong keyChecksum"; | ||
386 | } | ||
387 | } else { | ||
388 | throw "The requested One Time Password was not active"; | ||
389 | } | ||
390 | } else { | ||
391 | throw "The requested One Time Password has not been found" | ||
392 | } | ||
393 | } catch (exception) { | ||
394 | result = { | ||
395 | 'data': Clipperz.PM.Crypto.randomKey(), | ||
396 | 'version':Clipperz.PM.Connection.communicationProtocol.currentVersion | ||
397 | } | ||
398 | } | ||
399 | nextTollRequestType = 'CONNECT'; | ||
400 | } else { | ||
401 | MochiKit.Logging.logError("Clipperz.PM.Proxy.Test.handshake - unhandled message: " + someParameters.message); | ||
402 | } | ||
403 | |||
404 | result = { | ||
405 | result: result, | ||
406 | toll: this.getTollForRequestType(nextTollRequestType) | ||
407 | } | ||
408 | |||
409 | return result; | ||
410 | }, | ||
411 | |||
412 | //------------------------------------------------------------------------- | ||
413 | |||
414 | '_message': function(aConnection, someParameters) { | ||
415 | var result; | ||
416 | |||
417 | result = {}; | ||
418 | |||
419 | //===================================================================== | ||
420 | // | ||
421 | // R E A D - O N L Y M e t h o d s | ||
422 | // | ||
423 | //===================================================================== | ||
424 | if (someParameters.message == 'getUserDetails') { | ||
425 | var recordsStats; | ||
426 | var recordReference; | ||
427 | |||
428 | recordsStats = {}; | ||
429 | for (recordReference in aConnection['userData']['records']) { | ||
430 | recordsStats[recordReference] = { | ||
431 | 'updateDate': aConnection['userData']['records'][recordReference]['updateDate'] | ||
432 | } | ||
433 | } | ||
434 | |||
435 | result['header'] = this.userDetails(aConnection); | ||
436 | result['statistics'] = this.statistics(aConnection); | ||
437 | result['maxNumberOfRecords'] = aConnection['userData']['maxNumberOfRecords']; | ||
438 | result['version'] = aConnection['userData']['userDetailsVersion']; | ||
439 | result['recordsStats'] = recordsStats; | ||
440 | |||
441 | if (this.isReadOnly() == false) { | ||
442 | varlock; | ||
443 | |||
444 | if (typeof(aConnection['userData']['lock']) == 'undefined') { | ||
445 | aConnection['userData']['lock'] = "<<LOCK>>"; | ||
446 | } | ||
447 | |||
448 | result['lock'] = aConnection['userData']['lock']; | ||
449 | } | ||
450 | |||
451 | //===================================================================== | ||
452 | } else if (someParameters.message == 'getRecordDetail') { | ||
453 | /* | ||
454 | varrecordData; | ||
455 | var currentVersionData; | ||
456 | |||
457 | recordData = this.userData()['records'][someParameters['parameters']['reference']]; | ||
458 | result['reference'] = someParameters['parameters']['reference']; | ||
459 | result['data'] = recordData['data']; | ||
460 | result['version'] = recordData['version']; | ||
461 | result['creationData'] = recordData['creationDate']; | ||
462 | result['updateDate'] = recordData['updateDate']; | ||
463 | result['accessDate'] = recordData['accessDate']; | ||
464 | |||
465 | currentVersionData = recordData['versions'][recordData['currentVersion']]; | ||
466 | |||
467 | result['currentVersion'] = {}; | ||
468 | result['currentVersion']['reference'] = recordData['currentVersion']; | ||
469 | result['currentVersion']['version'] = currentVersionData['version']; | ||
470 | result['currentVersion']['header'] = currentVersionData['header']; | ||
471 | result['currentVersion']['data'] = currentVersionData['data']; | ||
472 | result['currentVersion']['creationData'] = currentVersionData['creationDate']; | ||
473 | result['currentVersion']['updateDate'] = currentVersionData['updateDate']; | ||
474 | result['currentVersion']['accessDate'] = currentVersionData['accessDate']; | ||
475 | if (typeof(currentVersionData['previousVersion']) != 'undefined') { | ||
476 | result['currentVersion']['previousVersionKey'] = currentVersionData['previousVersionKey']; | ||
477 | result['currentVersion']['previousVersion'] = currentVersionData['previousVersion']; | ||
478 | } | ||
479 | */ | ||
480 | MochiKit.Base.update(result, aConnection['userData']['records'][someParameters['parameters']['reference']]); | ||
481 | result['reference'] = someParameters['parameters']['reference']; | ||
482 | |||
483 | //===================================================================== | ||
484 | // | ||
485 | // R E A D - W R I T E M e t h o d s | ||
486 | // | ||
487 | //===================================================================== | ||
488 | } else if (someParameters.message == 'upgradeUserCredentials') { | ||
489 | if (this.isReadOnly() == false) { | ||
490 | var parameters; | ||
491 | var credentials; | ||
492 | |||
493 | parameters = someParameters['parameters']; | ||
494 | credentials = parameters['credentials']; | ||
495 | |||
496 | if ((credentials['C'] == null) | ||
497 | ||(credentials['s'] == null) | ||
498 | ||(credentials['v'] == null) | ||
499 | ||(credentials['version'] != Clipperz.PM.Connection.communicationProtocol.currentVersion) | ||
500 | ) { | ||
501 | result = Clipperz.PM.DataModel.User.exception.CredentialUpgradeFailed; | ||
502 | } else { | ||
503 | varoldCValue; | ||
504 | oldCValue = aConnection['C']; | ||
505 | |||
506 | this.data()['users'][credentials['C']] = aConnection['userData']; | ||
507 | aConnection['C'] = credentials['C']; | ||
508 | |||
509 | aConnection['userData']['s'] = credentials['s']; | ||
510 | aConnection['userData']['v'] = credentials['v']; | ||
511 | aConnection['userData']['version'] = credentials['version']; | ||
512 | |||
513 | aConnection['userData']['userDetails'] = parameters['user']['header']; | ||
514 | aConnection['userData']['userDetailsVersion'] = parameters['user']['version']; | ||
515 | aConnection['userData']['statistics'] = parameters['user']['statistics']; | ||
516 | |||
517 | aConnection['userData']['lock'] = parameters['user']['lock']; | ||
518 | |||
519 | delete this.data()['users'][oldCValue]; | ||
520 | |||
521 | result = {result:"done", parameters:parameters}; | ||
522 | } | ||
523 | } else { | ||
524 | throw Clipperz.PM.Proxy.Offline.DataStore.exception.ReadOnly; | ||
525 | } | ||
526 | //===================================================================== | ||
527 | /* } else if (someParameters.message == 'updateData') { | ||
528 | if (this.isReadOnly() == false) { | ||
529 | var i, c; | ||
530 | |||
531 | //console.log("###==============================================================="); | ||
532 | //console.log("###>>>", Clipperz.Base.serializeJSON(someParameters)); | ||
533 | //console.log("###--- userData", Clipperz.Base.serializeJSON(this.userData())); | ||
534 | if (this.userData()['lock']!= someParameters['parameters']['user']['lock']) { | ||
535 | throw "the lock attribute is not processed correctly" | ||
536 | } | ||
537 | |||
538 | this.userData()['userDetails'] = someParameters['parameters']['user']['header']; | ||
539 | this.userData()['statistics'] = someParameters['parameters']['user']['statistics']; | ||
540 | this.userData()['userDetailsVersions']= someParameters['parameters']['user']['version']; | ||
541 | |||
542 | c = someParameters['parameters']['records'].length; | ||
543 | for (i=0; i<c; i++) { | ||
544 | var currentRecord; | ||
545 | var currentRecordData; | ||
546 | |||
547 | currentRecordData = someParameters['parameters']['records'][i]; | ||
548 | currentRecord = this.userData()['records'][currentRecordData['record']['reference']]; | ||
549 | |||
550 | if (currentRecord == null) { | ||
551 | } | ||
552 | |||
553 | currentRecord['data'] = currentRecordData['record']['data']; | ||
554 | currentRecord['version'] = currentRecordData['record']['version']; | ||
555 | currentRecord['currentVersion'] = currentRecordData['currentRecordVersion']['reference']; | ||
556 | |||
557 | currentRecord['versions'][currentRecordData['currentRecordVersion']['reference']] = { | ||
558 | 'data': currentRecordData['currentRecordVersion']['data'], | ||
559 | 'version': currentRecordData['currentRecordVersion']['version'], | ||
560 | 'previousVersion': currentRecordData['currentRecordVersion']['previousVersion'], | ||
561 | 'previousVersionKey':currentRecordData['currentRecordVersion']['previousVersionKey'] | ||
562 | } | ||
563 | } | ||
564 | |||
565 | this.userData()['lock'] = Clipperz.PM.Crypto.randomKey(); | ||
566 | result['lock'] = this.userData()['lock']; | ||
567 | result['result'] = 'done'; | ||
568 | //console.log("###<<< userData", Clipperz.Base.serializeJSON(this.userData())); | ||
569 | } else { | ||
570 | throw Clipperz.PM.Proxy.Offline.DataStore.exception.ReadOnly; | ||
571 | } | ||
572 | */ //===================================================================== | ||
573 | } else if (someParameters.message == 'saveChanges') { | ||
574 | if (this.isReadOnly() == false) { | ||
575 | var i, c; | ||
576 | |||
577 | //console.log("###==============================================================="); | ||
578 | //console.log("###>>>", someParameters); | ||
579 | //console.log("###>>>", Clipperz.Base.serializeJSON(someParameters)); | ||
580 | //console.log("###--- userData", Clipperz.Base.serializeJSON(this.userData())); | ||
581 | //console.log("###==============================================================="); | ||
582 | //console.log("--- userData.lock ", this.userData()['lock']); | ||
583 | //console.log("--- parameters.lock", someParameters['parameters']['user']['lock']); | ||
584 | if (aConnection['userData']['lock']!= someParameters['parameters']['user']['lock']) { | ||
585 | throw "the lock attribute is not processed correctly" | ||
586 | } | ||
587 | |||
588 | aConnection['userData']['userDetails'] = someParameters['parameters']['user']['header']; | ||
589 | aConnection['userData']['statistics'] = someParameters['parameters']['user']['statistics']; | ||
590 | aConnection['userData']['userDetailsVersions']= someParameters['parameters']['user']['version']; | ||
591 | |||
592 | c = someParameters['parameters']['records']['updated'].length; | ||
593 | for (i=0; i<c; i++) { | ||
594 | var currentRecord; | ||
595 | var currentRecordData; | ||
596 | |||
597 | currentRecordData = someParameters['parameters']['records']['updated'][i]; | ||
598 | currentRecord = aConnection['userData']['records'][currentRecordData['record']['reference']]; | ||
599 | |||
600 | if ( | ||
601 | (typeof(aConnection['userData']['records'][currentRecordData['record']['reference']]) == 'undefined') | ||
602 | && | ||
603 | (typeof(currentRecordData['currentRecordVersion']) == 'undefined') | ||
604 | ) { | ||
605 | //console.log("######## SHIT HAPPENS"); | ||
606 | throw "Record added without a recordVersion"; | ||
607 | } | ||
608 | |||
609 | if (currentRecord == null) { | ||
610 | currentRecord = {}; | ||
611 | currentRecord['versions'] = {}; | ||
612 | currentRecord['creationDate']= Clipperz.PM.Date.formatDateWithUTCFormat(new Date()); | ||
613 | currentRecord['accessDate'] = Clipperz.PM.Date.formatDateWithUTCFormat(new Date()); | ||
614 | |||
615 | aConnection['userData']['records'][currentRecordData['record']['reference']] = currentRecord; | ||
616 | } | ||
617 | |||
618 | currentRecord['data'] = currentRecordData['record']['data']; | ||
619 | currentRecord['version']= currentRecordData['record']['version']; | ||
620 | currentRecord['updateDate']= Clipperz.PM.Date.formatDateWithUTCFormat(new Date()); | ||
621 | |||
622 | if (typeof(currentRecordData['currentRecordVersion']) != 'undefined') { | ||
623 | currentRecord['currentVersion'] = currentRecordData['currentRecordVersion']['reference']; | ||
624 | currentRecord['versions'][currentRecordData['currentRecordVersion']['reference']] = { | ||
625 | 'data': currentRecordData['currentRecordVersion']['data'], | ||
626 | 'version': currentRecordData['currentRecordVersion']['version'], | ||
627 | 'previousVersion': currentRecordData['currentRecordVersion']['previousVersion'], | ||
628 | 'previousVersionKey':currentRecordData['currentRecordVersion']['previousVersionKey'], | ||
629 | 'creationDate':Clipperz.PM.Date.formatDateWithUTCFormat(new Date()), | ||
630 | 'updateDate':Clipperz.PM.Date.formatDateWithUTCFormat(new Date()), | ||
631 | 'accessDate':Clipperz.PM.Date.formatDateWithUTCFormat(new Date()) | ||
632 | } | ||
633 | } | ||
634 | } | ||
635 | |||
636 | c = someParameters['parameters']['records']['deleted'].length; | ||
637 | for (i=0; i<c; i++) { | ||
638 | var currentRecordReference; | ||
639 | |||
640 | currentRecordReference = someParameters['parameters']['records']['deleted'][i]; | ||
641 | //console.log("DELETING records", currentRecordReference); | ||
642 | delete aConnection['userData']['records'][currentRecordReference]; | ||
643 | } | ||
644 | |||
645 | aConnection['userData']['lock'] = Clipperz.PM.Crypto.randomKey(); | ||
646 | result['lock'] = aConnection['userData']['lock']; | ||
647 | result['result'] = 'done'; | ||
648 | //console.log("###<<< userData", Clipperz.Base.serializeJSON(this.userData())); | ||
649 | } else { | ||
650 | throw Clipperz.PM.Proxy.Offline.DataStore.exception.ReadOnly; | ||
651 | } | ||
652 | |||
653 | //===================================================================== | ||
654 | // | ||
655 | // U N H A N D L E D M e t h o d | ||
656 | // | ||
657 | //===================================================================== | ||
658 | } else { | ||
659 | MochiKit.Logging.logError("Clipperz.PM.Proxy.Test.message - unhandled message: " + someParameters.message); | ||
660 | } | ||
661 | |||
662 | result = { | ||
663 | result: result, | ||
664 | toll: this.getTollForRequestType('MESSAGE') | ||
665 | } | ||
666 | |||
667 | // return MochiKit.Async.succeed(result); | ||
668 | return result; | ||
669 | }, | ||
670 | |||
671 | //------------------------------------------------------------------------- | ||
672 | |||
673 | '_logout': function(someParameters) { | ||
674 | // return MochiKit.Async.succeed({result: 'done'}); | ||
675 | return {result: 'done'}; | ||
676 | }, | ||
677 | |||
678 | //========================================================================= | ||
679 | //######################################################################### | ||
680 | |||
681 | 'isTestData': function(aConnection) { | ||
682 | return (typeof(aConnection['userData']['__masterkey_test_value__']) != 'undefined'); | ||
683 | }, | ||
684 | |||
685 | 'userDetails': function(aConnection) { | ||
686 | var result; | ||
687 | |||
688 | if (this.isTestData(aConnection)) { | ||
689 | var serializedHeader; | ||
690 | var version; | ||
691 | |||
692 | //MochiKit.Logging.logDebug("### test data"); | ||
693 | version = aConnection['userData']['userDetailsVersion']; | ||
694 | serializedHeader = Clipperz.Base.serializeJSON(aConnection['userData']['userDetails']); | ||
695 | result = Clipperz.PM.Crypto.encryptingFunctions.versions[version].encrypt(aConnection['userData']['__masterkey_test_value__'], serializedHeader); | ||
696 | } else { | ||
697 | //MochiKit.Logging.logDebug("### NOT test data"); | ||
698 | result = aConnection['userData']['userDetails']; | ||
699 | } | ||
700 | |||
701 | return result; | ||
702 | }, | ||
703 | |||
704 | 'statistics': function(aConnection) { | ||
705 | var result; | ||
706 | |||
707 | if (aConnection['userData']['statistics'] != null) { | ||
708 | if (this.isTestData(aConnection)) { | ||
709 | var serializedStatistics; | ||
710 | var version; | ||
711 | |||
712 | version = aConnection['userData']['userDetailsVersion']; | ||
713 | serializedStatistics = Clipperz.Base.serializeJSON(aConnection['userData']['statistics']); | ||
714 | result = Clipperz.PM.Crypto.encryptingFunctions.versions[version].encrypt(aConnection['userData']['__masterkey_test_value__'], serializedStatistics); | ||
715 | } else { | ||
716 | result = aConnection['userData']['statistics']; | ||
717 | } | ||
718 | } else { | ||
719 | result = null; | ||
720 | } | ||
721 | |||
722 | return result; | ||
723 | }, | ||
724 | |||
725 | /* | ||
726 | 'userSerializedEncryptedData': function(someData) { | ||
727 | var deferredResult; | ||
728 | var deferredContext; | ||
729 | |||
730 | deferredContext = { 'data': someData }; | ||
731 | |||
732 | deferredResult = new Clipperz.Async.Deferred('Proxy.Test.serializeUserEncryptedData', {trace:false}); | ||
733 | deferredResult.addCallback(MochiKit.Base.bind(function(aDeferredContext) { | ||
734 | aDeferredContext['user'] = this.createUserUsingConfigurationData(aDeferredContext['data']); | ||
735 | return aDeferredContext; | ||
736 | }, this)); | ||
737 | deferredResult.addCallback(function(aDeferredContext) { | ||
738 | // return aDeferredContext['user'].encryptedDataUsingVersion(aDeferredContext['data']['version']); | ||
739 | return aDeferredContext['user'].serializedDataUsingVersion(MochiKit.Base.values(aDeferredContext['user'].records()), aDeferredContext['data']['version']); | ||
740 | }); | ||
741 | deferredResult.addCallback(function(aUserEncryptedData) { | ||
742 | deferredContext['encryptedData'] = aUserEncryptedData; | ||
743 | return deferredContext; | ||
744 | }); | ||
745 | deferredResult.addCallback(function(aDeferredContext) { | ||
746 | var connection; | ||
747 | |||
748 | connection = new Clipperz.PM.Connection.communicationProtocol.versions[aDeferredContext['data']['connectionVersion']]() | ||
749 | aDeferredContext['credentials'] = connection.serverSideUserCredentials(aDeferredContext['user'].username(),aDeferredContext['user'].passphrase()); | ||
750 | |||
751 | return aDeferredContext; | ||
752 | }); | ||
753 | |||
754 | // deferredResult.addCallback(function(aDeferredContext) { | ||
755 | //console.log("#-#-#-#-#", aDeferredContext); | ||
756 | // return aDeferredContext['user'].serializedDataUsingVersion(MochiKit.Base.values(aDeferredContext['user'].records()), aDeferredContext['data']['version']); | ||
757 | // }, deferredContext); | ||
758 | // deferredResult.addCallback(function(aUserSerializedData) { | ||
759 | //console.log("USER SERIALIZED DATA", aUserSerializedData); | ||
760 | // }); | ||
761 | // | ||
762 | // deferredResult.addCallback(MochiKit.Async.succeed, deferredContext); | ||
763 | deferredResult.callback(deferredContext); | ||
764 | |||
765 | return deferredResult; | ||
766 | }, | ||
767 | |||
768 | 'createUserUsingConfigurationData': function(someData) { | ||
769 | var result; | ||
770 | var user; | ||
771 | var recordLabel; | ||
772 | |||
773 | user = new Clipperz.PM.DataModel.User(); | ||
774 | user.initForTests(); | ||
775 | user.setUsername(someData['username']); | ||
776 | user.setPassphrase(someData['passphrase']); | ||
777 | |||
778 | for (recordLabel in someData['records']) { | ||
779 | var recordData; | ||
780 | var record; | ||
781 | var i, c; | ||
782 | |||
783 | recordData = someData['records'][recordLabel]; | ||
784 | record = new Clipperz.PM.DataModel.Record({user:user, label:recordLabel}); | ||
785 | record.setNotes(recordData['notes']); | ||
786 | |||
787 | c = recordData['fields'].length; | ||
788 | for (i=0; i<c; i++) { | ||
789 | var recordField; | ||
790 | |||
791 | recordField = new Clipperz.PM.DataModel.RecordField(); | ||
792 | recordField.setLabel(recordData['fields'][i]['name']); | ||
793 | recordField.setValue(recordData['fields'][i]['value']); | ||
794 | recordField.setType(recordData['fields'][i]['type']); | ||
795 | record.addField(recordField); | ||
796 | } | ||
797 | user.addRecord(record, true); | ||
798 | } | ||
799 | |||
800 | result = user; | ||
801 | |||
802 | return result; | ||
803 | }, | ||
804 | */ | ||
805 | //========================================================================= | ||
806 | __syntaxFix__: "syntax fix" | ||
807 | }); | ||
808 | |||
809 | Clipperz.PM.Proxy.Offline.DataStore['exception'] = { | ||
810 | 'ReadOnly': new MochiKit.Base.NamedError("Clipperz.PM.Proxy.Offline.DataStore.exception.ReadOnly") | ||
811 | }; \ No newline at end of file | ||
diff --git a/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.js b/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.js new file mode 100644 index 0000000..a15b223 --- a/dev/null +++ b/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.js | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer to http://www.clipperz.com | ||
12 | |||
13 | * Javascript Crypto Library is free software: you can redistribute | ||
14 | it and/or modify it under the terms of the GNU Affero General Public | ||
15 | License as published by the Free Software Foundation, either version | ||
16 | 3 of the License, or (at your option) any later version. | ||
17 | |||
18 | * Javascript Crypto Library is distributed in the hope that it will | ||
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
21 | See the GNU Affero General Public License for more details. | ||
22 | |||
23 | * You should have received a copy of the GNU Affero General Public | ||
24 | License along with Javascript Crypto Library. If not, see | ||
25 | <http://www.gnu.org/licenses/>. | ||
26 | |||
27 | */ | ||
28 | |||
29 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
30 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | ||
31 | |||
32 | //============================================================================= | ||
33 | |||
34 | Clipperz.PM.Proxy.Offline = function(args) { | ||
35 | args = args || {}; | ||
36 | |||
37 | Clipperz.PM.Proxy.Offline.superclass.constructor.call(this, args); | ||
38 | |||
39 | this._dataStore = args.dataStore || new Clipperz.PM.Proxy.Offline.DataStore(args); | ||
40 | |||
41 | return this; | ||
42 | } | ||
43 | |||
44 | Clipperz.Base.extend(Clipperz.PM.Proxy.Offline, Clipperz.PM.Proxy, { | ||
45 | |||
46 | 'toString': function () { | ||
47 | return "Clipperz.PM.Proxy.Offline"; | ||
48 | }, | ||
49 | |||
50 | //------------------------------------------------------------------------- | ||
51 | |||
52 | 'dataStore': function () { | ||
53 | return this._dataStore; | ||
54 | }, | ||
55 | |||
56 | //------------------------------------------------------------------------- | ||
57 | |||
58 | 'sendMessage': function(aFunctionName, someParameters) { | ||
59 | return this.dataStore().processMessage(aFunctionName, someParameters); | ||
60 | }, | ||
61 | |||
62 | //------------------------------------------------------------------------- | ||
63 | |||
64 | __syntaxFix__: "syntax fix" | ||
65 | |||
66 | }); | ||
67 | |||
diff --git a/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Test.js b/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Test.js new file mode 100644 index 0000000..be1c337 --- a/dev/null +++ b/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Test.js | |||
@@ -0,0 +1,167 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer to http://www.clipperz.com | ||
12 | |||
13 | * Javascript Crypto Library is free software: you can redistribute | ||
14 | it and/or modify it under the terms of the GNU Affero General Public | ||
15 | License as published by the Free Software Foundation, either version | ||
16 | 3 of the License, or (at your option) any later version. | ||
17 | |||
18 | * Javascript Crypto Library is distributed in the hope that it will | ||
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
21 | See the GNU Affero General Public License for more details. | ||
22 | |||
23 | * You should have received a copy of the GNU Affero General Public | ||
24 | License along with Javascript Crypto Library. If not, see | ||
25 | <http://www.gnu.org/licenses/>. | ||
26 | |||
27 | */ | ||
28 | |||
29 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
30 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | ||
31 | if (typeof(Clipperz.PM.Proxy) == 'undefined') { Clipperz.PM.Proxy = {}; } | ||
32 | |||
33 | //============================================================================= | ||
34 | |||
35 | Clipperz.PM.Proxy.Test = function(args) { | ||
36 | Clipperz.PM.Proxy.Test.superclass.constructor.call(this, args); | ||
37 | |||
38 | args = args || {}; | ||
39 | |||
40 | this._expectedRequests = (args.shouldCheckExpectedRequests === true) ? [] : null; | ||
41 | this._isExpectingRequests = true; | ||
42 | this._unexpectedRequests = []; | ||
43 | |||
44 | this.dataStore().resetData(); | ||
45 | |||
46 | return this; | ||
47 | } | ||
48 | |||
49 | Clipperz.Base.extend(Clipperz.PM.Proxy.Test, Clipperz.PM.Proxy.Offline, { | ||
50 | |||
51 | 'toString': function() { | ||
52 | return "Clipperz.PM.Proxy.Test"; | ||
53 | }, | ||
54 | |||
55 | //========================================================================= | ||
56 | |||
57 | 'expectedRequests': function () { | ||
58 | return this._expectedRequests; | ||
59 | }, | ||
60 | |||
61 | //------------------------------------------------------------------------- | ||
62 | |||
63 | 'shouldCheckExpectedRequests': function () { | ||
64 | return (this._expectedRequests != null); | ||
65 | }, | ||
66 | |||
67 | 'setShouldCheckExpectedRequests': function(aValue) { | ||
68 | if (aValue) { | ||
69 | this._expectedRequests = aValue; | ||
70 | } else { | ||
71 | this._expectedRequests = null; | ||
72 | } | ||
73 | }, | ||
74 | |||
75 | //------------------------------------------------------------------------- | ||
76 | |||
77 | 'shouldNotReceiveAnyFurtherRequest': function () { | ||
78 | this._isExpectingRequests = false; | ||
79 | }, | ||
80 | |||
81 | 'mayReceiveMoreRequests': function () { | ||
82 | this._isExpectingRequests = true; | ||
83 | this.resetUnexpectedRequests(); | ||
84 | }, | ||
85 | |||
86 | 'isExpectingRequests': function () { | ||
87 | return this._isExpectingRequests; | ||
88 | }, | ||
89 | |||
90 | //------------------------------------------------------------------------- | ||
91 | |||
92 | 'unexpectedRequests': function () { | ||
93 | return this._unexpectedRequests; | ||
94 | }, | ||
95 | |||
96 | 'resetUnexpectedRequests': function () { | ||
97 | this._unexpectedRequests = []; | ||
98 | }, | ||
99 | |||
100 | //------------------------------------------------------------------------- | ||
101 | |||
102 | 'testExpectedRequestParameters': function (aPath, anActualRequest, anExpectedRequest) { | ||
103 | var aKey; | ||
104 | //console.log(">>> Proxy.testExpectedRequestParameters [" + aPath + "]", anActualRequest, anExpectedRequest); | ||
105 | for (aKey in anExpectedRequest) { | ||
106 | if (typeof(anActualRequest[aKey]) == 'undefined') { | ||
107 | throw "the expected paramter [" + aKey + "] is missing from the actual request"; | ||
108 | } | ||
109 | if (typeof(anExpectedRequest[aKey]) == 'object') { | ||
110 | this.testExpectedRequestParameters(aPath + "." + aKey, anActualRequest[aKey], anExpectedRequest[aKey]) | ||
111 | } else { | ||
112 | if (! anExpectedRequest[aKey](anActualRequest[aKey])) { | ||
113 | throw "wrong value for paramter [" + aKey + "]; got '" + anActualRequest[aKey] + "'"; | ||
114 | } | ||
115 | } | ||
116 | } | ||
117 | //console.log("<<< Proxy.testExpectedRequestParameters"); | ||
118 | }, | ||
119 | |||
120 | //------------------------------------------------------------------------- | ||
121 | |||
122 | 'checkRequest': function(aFunctionName, someParameters) { | ||
123 | if (this.shouldCheckExpectedRequests()) { | ||
124 | var expectedRequest; | ||
125 | |||
126 | //console.log(">>> Proxy.Test.checkRequest - " + aFunctionName, someParameters); | ||
127 | expectedRequest = this.expectedRequests().pop(); | ||
128 | //console.log("--- Proxy.Test.checkRequest - expectedRequest", expectedRequest); | ||
129 | if (expectedRequest == null) { | ||
130 | throw "Proxy.Test.sentMessage: no expected result specified. Got request '" + aFunctionName + "': " + someParameters; | ||
131 | } | ||
132 | |||
133 | try { | ||
134 | if (aFunctionName != expectedRequest.functionName) { | ||
135 | throw "wrong function name. Got '" + aFunctionName + "', expected '" + expectedRequest.request.functionName + "'"; | ||
136 | } | ||
137 | |||
138 | this.testExpectedRequestParameters("parameters", someParameters, expectedRequest.parameters); | ||
139 | } catch(exception) { | ||
140 | //console.log("EXCEPTION: Proxy.Test.sentMessage[" + expectedRequest.name + "]", exception) | ||
141 | throw "Proxy.Test.sentMessage[" + expectedRequest.name + "]: " + exception; | ||
142 | } | ||
143 | } | ||
144 | //console.log("<<< Proxy.Test.checkRequest"); | ||
145 | }, | ||
146 | |||
147 | //========================================================================= | ||
148 | |||
149 | 'sendMessage': function(aFunctionName, someParameters) { | ||
150 | var result; | ||
151 | |||
152 | if (this.isExpectingRequests() == false) { | ||
153 | // throw Clipperz.PM.Connection.exception.UnexpectedRequest; | ||
154 | Clipperz.log("UNEXPECTED REQUEST " + aFunctionName /* + ": " + Clipperz.Base.serializeJSON(someParameters) */); | ||
155 | this.unexpectedRequests().push({'functionName':aFunctionName, 'someParameters': someParameters}); | ||
156 | }; | ||
157 | this.checkRequest(aFunctionName, someParameters); | ||
158 | result = Clipperz.PM.Proxy.Test.superclass.sendMessage.call(this, aFunctionName, someParameters); | ||
159 | |||
160 | return result; | ||
161 | }, | ||
162 | |||
163 | //========================================================================= | ||
164 | __syntaxFix__: "syntax fix" | ||
165 | |||
166 | }); | ||
167 | |||