summaryrefslogtreecommitdiff
path: root/frontend/beta/js/Clipperz/PM/DataModel/DirectLogin.js
Unidiff
Diffstat (limited to 'frontend/beta/js/Clipperz/PM/DataModel/DirectLogin.js') (more/less context) (show whitespace changes)
-rw-r--r--frontend/beta/js/Clipperz/PM/DataModel/DirectLogin.js536
1 files changed, 536 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/PM/DataModel/DirectLogin.js b/frontend/beta/js/Clipperz/PM/DataModel/DirectLogin.js
new file mode 100644
index 0000000..3ebc208
--- a/dev/null
+++ b/frontend/beta/js/Clipperz/PM/DataModel/DirectLogin.js
@@ -0,0 +1,536 @@
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
29if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
30if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; }
31if (typeof(Clipperz.PM.DataModel) == 'undefined') { Clipperz.PM.DataModel = {}; }
32
33
34//#############################################################################
35
36Clipperz.PM.DataModel.DirectLogin = function(args) {
37//MochiKit.Logging.logDebug(">>> new Clipperz.PM.DataModel.DirectLogin");
38//console.log(">>> new Clipperz.PM.DataModel.DirectLogin - args: %o", args);
39//console.log("--- formData: %s", Clipperz.Base.serializeJSON(args.formData));
40 args = args || {};
41
42//MochiKit.Logging.logDebug("--- new Clipperz.PM.DataModel.DirectLogin - args: " + Clipperz.Base.serializeJSON(MochiKit.Base.keys(args)));
43 this._record = args.record || null;
44 this._label = args.label || "unnamed record"
45 this._reference = args.reference || Clipperz.PM.Crypto.randomKey();
46 this._favicon = args.favicon || null;
47 this._bookmarkletVersion = args.bookmarkletVersion || "0.1";
48
49 this._directLoginInputs = null;
50
51 this._formValues = args.formValues || {};
52 this.setFormData(args.formData || null);
53//console.log("=== formData: %o", this.formData());
54
55 if (args.legacyBindingData == null) {
56 this.setBindingData(args.bindingData || null);
57 } else {
58 this.setLegacyBindingData(args.legacyBindingData);
59 }
60
61 this._fixedFavicon = null;
62
63 //this._formValues = args.formValues || (this.hasValuesToSet() ? {} : null);
64//MochiKit.Logging.logDebug("<<< new Clipperz.PM.DataModel.DirectLogin");
65
66 return this;
67}
68
69Clipperz.PM.DataModel.DirectLogin.prototype = MochiKit.Base.update(null, {
70
71 'remove': function() {
72 this.record().removeDirectLogin(this);
73 },
74
75 //-------------------------------------------------------------------------
76
77 'record': function() {
78 return this._record;
79 },
80
81 //-------------------------------------------------------------------------
82
83 'user': function() {
84 return this.record().user();
85 },
86
87 //-------------------------------------------------------------------------
88
89 'reference': function() {
90 return this._reference;
91 },
92
93 //-------------------------------------------------------------------------
94
95 'label': function() {
96 return this._label;
97 },
98
99 'setLabel': function(aValue) {
100 this._label = aValue;
101 },
102
103 //-------------------------------------------------------------------------
104
105 'favicon': function() {
106 if (this._favicon == null) {
107 varactionUrl;
108 var hostname;
109
110 actionUrl = this.formData()['attributes']['action'];
111 hostname = actionUrl.replace(/^https?:\/\/([^\/]*)\/.*/, '$1');
112 this._favicon = "http://" + hostname + "/favicon.ico";
113 }
114
115 return this._favicon;
116 },
117
118 //-------------------------------------------------------------------------
119
120 'fixedFavicon': function() {
121 var result;
122
123 if (this._fixedFavicon == null) {
124 result = this.favicon();
125
126 if (Clipperz_IEisBroken) {
127 if (this.user().preferences().disableUnsecureFaviconLoadingForIE()) {
128 if (result.indexOf('https://') != 0) {
129 result = Clipperz.PM.Strings['defaultFaviconUrl_IE'];
130 this.setFixedFavicon(result);
131 }
132 }
133 }
134 } else {
135 result = this._fixedFavicon;
136 }
137
138 return result;
139 },
140
141 'setFixedFavicon': function(aValue) {
142 this._fixedFavicon = aValue;
143 },
144
145 //-------------------------------------------------------------------------
146
147 'bookmarkletVersion': function() {
148 return this._bookmarkletVersion;
149 },
150
151 'setBookmarkletVersion': function(aValue) {
152 this._bookmarkletVersion = aValue;
153 },
154
155 //-------------------------------------------------------------------------
156
157 'formData': function() {
158 return this._formData;
159 },
160
161 'setFormData': function(aValue) {
162 var formData;
163
164//MochiKit.Logging.logDebug(">>> DirectLogin.setFormData - " + Clipperz.Base.serializeJSON(aValue));
165 switch (this.bookmarkletVersion()) {
166 case "0.2":
167 formData = aValue;
168 break;
169 case "0.1":
170//MochiKit.Logging.logDebug("--- DirectLogin.setFormData - fixing form data from bookmarklet version 0.1");
171 formData = this.fixFormDataFromBookmarkletVersion_0_1(aValue);
172 break;
173 }
174
175 this._formData = aValue;
176 this.setBookmarkletVersion("0.2");
177
178//MochiKit.Logging.logDebug("--- DirectLogin.setFormData - formData: " + Clipperz.Base.serializeJSON(formData));
179 if (formData != null) {
180 var i,c;
181
182 this._directLoginInputs = [];
183 c = formData['inputs'].length;
184 for (i=0; i<c; i++) {
185 var directLoginInput;
186
187 directLoginInput = new Clipperz.PM.DataModel.DirectLoginInput(this, formData['inputs'][i]);
188 this._directLoginInputs.push(directLoginInput);
189 }
190 }
191//MochiKit.Logging.logDebug("<<< DirectLogin.setFormData");
192 },
193
194 'fixFormDataFromBookmarkletVersion_0_1': function(aValue) {
195//{"type":"radio", "name":"action", "value":"new-user", "checked":false }, { "type":"radio", "name":"action", "value":"sign-in", "checked":true }
196 // ||
197 // \ /
198 // \/
199//{"name":"dominio", "type":"radio", "options":[{"value":"@alice.it", "checked":true}, {"value":"@tin.it", "checked":false}, {"value":"@virgilio.it", "checked":false}, {"value":"@tim.it", "checked":false}]}
200 var result;
201 var inputs;
202 var updatedInputs;
203 var radios;
204
205//MochiKit.Logging.logDebug(">>> DirectLogin.fixFormDataFromBookmarkletVersion_0_1");
206 result = aValue;
207 inputs = aValue['inputs'];
208
209 updatedInputs = MochiKit.Base.filter(function(anInput) {
210 varresult;
211 var type;
212
213 type = anInput['type'] || 'text';
214 result = type.toLowerCase() != 'radio';
215
216 return result;
217 }, inputs);
218 radios = MochiKit.Base.filter(function(anInput) {
219 varresult;
220 var type;
221
222 type = anInput['type'] || 'text';
223 result = type.toLowerCase() == 'radio';
224
225 return result;
226 }, inputs);
227
228 if (radios.length > 0) {
229 var updatedRadios;
230
231 updatedRadios = {};
232 MochiKit.Iter.forEach(radios, MochiKit.Base.bind(function(aRadio) {
233 varradioConfiguration;
234
235 radioConfiguration = updatedRadios[aRadio['name']];
236 if (radioConfiguration == null) {
237 radioConfiguration = {type:'radio', name:aRadio['name'], options:[]};
238 updatedRadios[aRadio['name']] = radioConfiguration;
239 }
240
241 //TODO: remove the value: field and replace it with element.dom.value = <some value>
242 radioConfiguration.options.push({value:aRadio['value'], checked:aRadio['checked']});
243
244 if ((aRadio['checked'] == true) && (this.formValues()[aRadio['name']] == null)) {
245//MochiKit.Logging.logDebug("+++ setting value '" + aRadio['value'] + "' for key: '" + aRadio['name'] + "'");
246 this.formValues()[aRadio['name']] = aRadio['value'];
247 }
248 }, this))
249
250 updatedInputs = MochiKit.Base.concat(updatedInputs, MochiKit.Base.values(updatedRadios));
251 }
252
253 delete result.inputs;
254 result.inputs = updatedInputs;
255//MochiKit.Logging.logDebug("<<< DirectLogin.fixFormDataFromBookmarkletVersion_0_1");
256
257 return result;
258 },
259
260 //.........................................................................
261
262 'directLoginInputs': function() {
263 return this._directLoginInputs;
264 },
265
266 //-------------------------------------------------------------------------
267
268 'formValues': function() {
269 return this._formValues;
270 },
271
272 'hasValuesToSet': function() {
273 var result;
274
275//MochiKit.Logging.logDebug(">>> DirectLogin.hasValuesToSet");
276 if (this.directLoginInputs() != null) {
277 result = MochiKit.Iter.some(this.directLoginInputs(), MochiKit.Base.methodcaller('shouldSetValue'));
278 } else {
279 result = false;
280 }
281//MochiKit.Logging.logDebug("<<< DirectLogin.hasValuesToSet");
282
283 return result;
284 },
285
286 //'additionalValues': function() {
287 'inputsRequiringAdditionalValues': function() {
288 varresult;
289 var inputs;
290
291//MochiKit.Logging.logDebug(">>> DirectLogin.additionalValues");
292 result = {};
293 if (this.directLoginInputs() != null) {
294 inputs = MochiKit.Base.filter(MochiKit.Base.methodcaller('shouldSetValue'), this.directLoginInputs());
295 MochiKit.Iter.forEach(inputs, function(anInput) {
296 result[anInput.name()] = anInput;
297 })
298 }
299//MochiKit.Logging.logDebug("<<< DirectLogin.additionalValues");
300
301 return result;
302 },
303
304 //-------------------------------------------------------------------------
305
306 'bindingData': function() {
307 return this._bindingData;
308 },
309
310 'setBindingData': function(aValue) {
311//MochiKit.Logging.logDebug(">>> DirectLogin.setBindingData");
312 if (aValue != null) {
313 var bindingKey;
314
315 this._bindingData = aValue;
316 this._bindings = {};
317
318 for (bindingKey in aValue) {
319 var directLoginBinding;
320
321 directLoginBinding = new Clipperz.PM.DataModel.DirectLoginBinding(this, bindingKey, {fieldKey:aValue[bindingKey]});
322 this._bindings[bindingKey] = directLoginBinding;
323 }
324 } else {
325 var editableFields;
326 var bindings;
327
328 bindings = {};
329
330 editableFields = MochiKit.Base.filter(function(aField) {
331 var result;
332 var type;
333
334 type = aField['type'].toLowerCase();
335 result = ((type != 'hidden') && (type != 'submit') && (type != 'checkbox') && (type != 'radio') && (type != 'select'));
336
337 return result;
338 }, this.formData().inputs);
339
340 MochiKit.Iter.forEach(editableFields, function(anEditableField) {
341 bindings[anEditableField['name']] = new Clipperz.PM.DataModel.DirectLoginBinding(this, anEditableField['name']);
342 }, this);
343
344 this._bindings = bindings;
345 }
346//MochiKit.Logging.logDebug("<<< DirectLogin.setBindingData");
347 },
348
349 'setLegacyBindingData': function(aValue) {
350//MochiKit.Logging.logDebug(">>> DirectLogin.setLegacyBindingData");
351 var bindingKey;
352
353 this._bindingData = aValue;
354 this._bindings = {};
355
356 for (bindingKey in aValue) {
357 var directLoginBinding;
358
359 directLoginBinding = new Clipperz.PM.DataModel.DirectLoginBinding(this, bindingKey, {fieldName:aValue[bindingKey]});
360 this._bindings[bindingKey] = directLoginBinding;
361 }
362//MochiKit.Logging.logDebug("<<< DirectLogin.setLegacyBindingData");
363 },
364
365 //.........................................................................
366
367 'bindings': function() {
368 return this._bindings;
369 },
370
371 //-------------------------------------------------------------------------
372
373 'serializedData': function() {
374 var result;
375 varbindingKey;
376
377 result = {};
378 // result.reference = this.reference();
379 result.label = this.label();
380 result.favicon = this.favicon() || "";
381 result.bookmarkletVersion = this.bookmarkletVersion();
382 result.formData = this.formData();
383 if (this.hasValuesToSet) {
384 result.formValues = this.formValues();
385 }
386 result.bindingData = {};
387
388 for (bindingKey in this.bindings()) {
389 result.bindingData[bindingKey] = this.bindings()[bindingKey].serializedData();
390 }
391
392 return result;
393 },
394
395 //-------------------------------------------------------------------------
396
397 'handleMissingFaviconImage': function(anEvent) {
398 anEvent.stop();
399 MochiKit.Signal.disconnectAll(anEvent.src());
400 this.setFixedFavicon(Clipperz.PM.Strings['defaultFaviconUrl']);
401 anEvent.src().src = this.fixedFavicon();
402 },
403
404 //=========================================================================
405
406 'runHttpAuthDirectLogin': function(aWindow) {
407 MochiKit.DOM.withWindow(aWindow, MochiKit.Base.bind(function() {
408 var completeUrl;
409 var url;
410
411 url = this.bindings()['url'].field().value();
412
413 if (/^https?\:\/\//.test(url) == false) {
414 url = 'http://' + url;
415 }
416
417 if (Clipperz_IEisBroken === true) {
418 completeUrl = url;
419 } else {
420 var username;
421 var password;
422
423 username = this.bindings()['username'].field().value();
424 password = this.bindings()['password'].field().value();
425
426 /(^https?\:\/\/)?(.*)/.test(url);
427
428 completeUrl = RegExp.$1 + username + ':' + password + '@' + RegExp.$2;
429 }
430
431 MochiKit.DOM.currentWindow().location.href = completeUrl;
432 }, this));
433 },
434
435 //-------------------------------------------------------------------------
436
437 'runSubmitFormDirectLogin': function(aWindow) {
438 MochiKit.DOM.withWindow(aWindow, MochiKit.Base.bind(function() {
439 var formElement;
440 varformSubmitFunction;
441 var submitButtons;
442
443//MochiKit.Logging.logDebug("### runDirectLogin - 3");
444 // MochiKit.DOM.currentDocument().write('<html><head><title>' + this.label() + '</title><META http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body></body></html>')
445//MochiKit.Logging.logDebug("### runDirectLogin - 3.1");
446 MochiKit.DOM.appendChildNodes(MochiKit.DOM.currentDocument().body, MochiKit.DOM.H3(null, "Loading " + this.label() + " ..."));
447//MochiKit.Logging.logDebug("### runDirectLogin - 4");
448//console.log(this.formData()['attributes']);
449 formElement = MochiKit.DOM.FORM(MochiKit.Base.update({id:'directLoginForm'}, {'method':this.formData()['attributes']['method'],
450 'action':this.formData()['attributes']['action']}));
451//MochiKit.Logging.logDebug("### runDirectLogin - 5");
452 formSubmitFunction = MochiKit.Base.method(formElement, 'submit');
453//MochiKit.Logging.logDebug("### runDirectLogin - 6");
454
455 MochiKit.DOM.appendChildNodes(MochiKit.DOM.currentDocument().body,
456 MochiKit.DOM.DIV({style:'display:none; visibility:hidden;'}, formElement)
457 );
458//MochiKit.Logging.logDebug("### runDirectLogin - 7");
459 MochiKit.DOM.appendChildNodes(formElement, MochiKit.Base.map(MochiKit.Base.methodcaller("formConfiguration"),
460 this.directLoginInputs()));
461//MochiKit.Logging.logDebug("### runDirectLogin - 8");
462
463 submitButtons = MochiKit.Base.filter(function(anInputElement) {
464//MochiKit.Logging.logDebug("### runDirectLogin - 8.1 - " + anInputElement);
465//MochiKit.Logging.logDebug("### runDirectLogin - 8.2 - " + anInputElement.tagName);
466//MochiKit.Logging.logDebug("### runDirectLogin - 8.3 - " + anInputElement.getAttribute('type'));
467 return ((anInputElement.tagName.toLowerCase() == 'input') && (anInputElement.getAttribute('type').toLowerCase() == 'submit'));
468 }, formElement.elements)
469//MochiKit.Logging.logDebug("### runDirectLogin - 9");
470
471 if (submitButtons.length == 0) {
472//MochiKit.Logging.logDebug("### OLD submit")
473 if (Clipperz_IEisBroken == true) {
474//MochiKit.Logging.logDebug("### runDirectLogin - 10");
475 formElement.submit();
476 } else {
477//MochiKit.Logging.logDebug("### runDirectLogin - 11");
478 formSubmitFunction();
479 }
480 } else {
481//MochiKit.Logging.logDebug("### NEW submit")
482 submitButtons[0].click();
483 }
484
485 }, this));
486 },
487
488 //-------------------------------------------------------------------------
489
490 'runDirectLogin': function(aNewWindow) {
491 varnewWindow;
492
493//console.log("formData.attributes", this.formData()['attributes']);
494 // if (/^javascript/.test(this.formData()['attributes']['action'])) {
495 if ((/^(https?|webdav|ftp)\:/.test(this.formData()['attributes']['action']) == false) &&
496 (this.formData()['attributes']['type'] != 'http_auth'))
497 {
498 var messageBoxConfiguration;
499
500 if (typeof(aNewWindow) != 'undefined') {
501 aNewWindow.close();
502 }
503
504 messageBoxConfiguration = {};
505 messageBoxConfiguration.title = Clipperz.PM.Strings['VulnerabilityWarning_Panel_title'];
506 messageBoxConfiguration.msg = Clipperz.PM.Strings['VulnerabilityWarning_Panel_message'];
507 messageBoxConfiguration.animEl = YAHOO.ext.Element.get("mainDiv");
508 messageBoxConfiguration.progress = false;
509 messageBoxConfiguration.closable = false;
510 messageBoxConfiguration.buttons = {'cancel': Clipperz.PM.Strings['VulnerabilityWarning_Panel_buttonLabel']};
511
512 Clipperz.YUI.MessageBox.show(messageBoxConfiguration);
513
514 throw Clipperz.Base.exception.VulnerabilityIssue;
515 }
516
517//MochiKit.Logging.logDebug("### runDirectLogin - 1 : " + Clipperz.Base.serializeJSON(this.serializedData()));
518 if (typeof(aNewWindow) == 'undefined') {
519 newWindow = window.open(Clipperz.PM.Strings['directLoginJumpPageUrl'], "");
520 } else {
521 newWindow = aNewWindow;
522 }
523//MochiKit.Logging.logDebug("### runDirectLogin - 2");
524
525 if (this.formData()['attributes']['type'] == 'http_auth') {
526 this.runHttpAuthDirectLogin(newWindow);
527 } else {
528 this.runSubmitFormDirectLogin(newWindow)
529 }
530 },
531
532 //-------------------------------------------------------------------------
533 __syntaxFix__: "syntax fix"
534
535});
536