summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.DataStore.js
Unidiff
Diffstat (limited to 'frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.DataStore.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/gamma/js/Clipperz/PM/Proxy/Proxy.Offline.DataStore.js811
1 files changed, 811 insertions, 0 deletions
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
3Copyright 2008-2011 Clipperz Srl
4
5This file is part of Clipperz's Javascript Crypto Library.
6Javascript Crypto Library provides web developers with an extensive
7and efficient set of cryptographic functions. The library aims to
8obtain maximum execution speed while preserving modularity and
9reusability.
10For further information about its features and functionalities please
11refer 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
29try { 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
35Clipperz.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
48Clipperz.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
809Clipperz.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