Diffstat (limited to 'frontend/beta/js/Clipperz/PM/Components/PasswordGenerator.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/PM/Components/PasswordGenerator.js | 285 |
1 files changed, 285 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/PM/Components/PasswordGenerator.js b/frontend/beta/js/Clipperz/PM/Components/PasswordGenerator.js new file mode 100644 index 0000000..8195f2e --- a/dev/null +++ b/frontend/beta/js/Clipperz/PM/Components/PasswordGenerator.js | |||
@@ -0,0 +1,285 @@ | |||
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.Components) == 'undefined') { Clipperz.PM.Components = {}; } | ||
32 | |||
33 | Clipperz.PM.Components.PasswordGenerator = function(anElement, aFieldValueComponent, args) { | ||
34 | args = args || {}; | ||
35 | |||
36 | //MochiKit.Logging.logDebug(">>> new TextFormField"); | ||
37 | Clipperz.PM.Components.PasswordGenerator.superclass.constructor.call(this, anElement, args); | ||
38 | |||
39 | this._fieldValueComponent = aFieldValueComponent; | ||
40 | this._panelButton = null; | ||
41 | this.render(); | ||
42 | //MochiKit.Logging.logDebug("<<< new TextFormField"); | ||
43 | |||
44 | return this; | ||
45 | }; | ||
46 | |||
47 | YAHOO.extendX(Clipperz.PM.Components.PasswordGenerator, Clipperz.PM.Components.BaseComponent, { | ||
48 | |||
49 | 'toString': function() { | ||
50 | return "Clipperz.PM.Components.PasswordGenerator"; | ||
51 | }, | ||
52 | |||
53 | //----------------------------------------------------- | ||
54 | |||
55 | 'fieldValueComponent': function() { | ||
56 | return this._fieldValueComponent; | ||
57 | }, | ||
58 | |||
59 | //----------------------------------------------------- | ||
60 | |||
61 | 'render': function() { | ||
62 | MochiKit.Signal.disconnectAllTo(this); | ||
63 | |||
64 | // this._panelButton = new YAHOO.ext.Button(this.element().dom, {text:Clipperz.PM.Strings['passwordGeneratorButtonLabel'], handler:this.openPasswordPanel, scope:this}); | ||
65 | MochiKit.Signal.connect(this.element().dom, 'onmouseenter', this, 'onMouseEnter'); | ||
66 | MochiKit.Signal.connect(this.element().dom, 'onmouseleave', this, 'onMouseLeave'); | ||
67 | MochiKit.Signal.connect(this.element().dom, 'onclick', this, 'openPasswordPanel'); | ||
68 | }, | ||
69 | |||
70 | //----------------------------------------------------- | ||
71 | |||
72 | 'onMouseEnter': function() { | ||
73 | this.element().addClass('hover'); | ||
74 | }, | ||
75 | |||
76 | 'onMouseLeave': function() { | ||
77 | this.element().removeClass('hover'); | ||
78 | }, | ||
79 | |||
80 | //----------------------------------------------------- | ||
81 | |||
82 | 'panelButton': function() { | ||
83 | return this._panelButton; | ||
84 | }, | ||
85 | |||
86 | //----------------------------------------------------- | ||
87 | |||
88 | 'openPasswordPanel': function() { | ||
89 | var passwordGeneratorElement; | ||
90 | var passwordGeneratorDialog; | ||
91 | var cancelButton; | ||
92 | var okButton; | ||
93 | var cancelFunction; | ||
94 | var okFunction; | ||
95 | |||
96 | //MochiKit.Logging.logDebug(">>> PasswordGenerator.openPasswordPanel"); | ||
97 | passwordGeneratorElement = Clipperz.YUI.DomHelper.append(document.body, {tag:'div', id:'passwordGenerator', children:[ | ||
98 | {tag:'div', cls:'ydlg-hd', htmlString:Clipperz.PM.Strings['passwordGeneratorPanelTitle']}, | ||
99 | {tag:'div', cls:'ydlg-bd', children:[ | ||
100 | {tag:'form', id:this.getId('passwordGeneratorForm'), cls:'passwordGenerator', children:[ | ||
101 | {tag:'input', type:'text', cls:'clipperz_passwordGenerator_password', id:this.getId('passwordField')}, | ||
102 | {tag:'table', children:[ | ||
103 | {tag:'tbody', children:[ | ||
104 | {tag:'tr', children:[ | ||
105 | {tag:'td', width:'20%', children:[ | ||
106 | {tag:'input', type:'checkbox', name:'lowercase', id:this.getId('lowercase'), checked:true}, | ||
107 | {tag:'span', htmlString:Clipperz.PM.Strings['passwordGeneratorLowercaseLabel']} | ||
108 | ]}, | ||
109 | {tag:'td', width:'20%', children:[ | ||
110 | {tag:'input', type:'checkbox', name:'uppercase', id:this.getId('uppercase'), checked:true}, | ||
111 | {tag:'span', htmlString:Clipperz.PM.Strings['passwordGeneratorUppercaseLabel']} | ||
112 | ]}, | ||
113 | {tag:'td', width:'20%', children:[ | ||
114 | {tag:'input', type:'checkbox', name:'numbers', id:this.getId('numbers'), checked:true}, | ||
115 | {tag:'span', htmlString:Clipperz.PM.Strings['passwordGeneratorNumberLabel']} | ||
116 | ]}, | ||
117 | {tag:'td', width:'20%', children:[ | ||
118 | {tag:'input', type:'checkbox', name:'symbols', id:this.getId('symbols'), checked:true}, | ||
119 | {tag:'span', htmlString:Clipperz.PM.Strings['passwordGeneratorSymbolLabel']} | ||
120 | ]}, | ||
121 | {tag:'td', width:'20%', children:[ | ||
122 | {tag:'span', cls:'passwordGeneratorLength', children:[ | ||
123 | {tag:'span', htmlString:Clipperz.PM.Strings['passwordGeneratorLengthLabel']}, | ||
124 | {tag:'span', id:this.getId('passwordLength'), cls:'passwordGeneratorLengthValue', html:'0'} | ||
125 | ]} | ||
126 | ]} | ||
127 | ]} | ||
128 | ]} | ||
129 | ]} | ||
130 | ]} | ||
131 | ]}, | ||
132 | {tag:'div', cls:'ydlg-ft'} | ||
133 | ]}, true); | ||
134 | |||
135 | new Clipperz.PM.Components.PasswordEntropyDisplay(this.getElement('passwordField')); | ||
136 | |||
137 | MochiKit.Signal.connect(this.getId('lowercase'), 'onclick', this, 'updatePasswordValue'); | ||
138 | MochiKit.Signal.connect(this.getId('uppercase'), 'onclick', this, 'updatePasswordValue'); | ||
139 | MochiKit.Signal.connect(this.getId('numbers'), 'onclick', this, 'updatePasswordValue'); | ||
140 | MochiKit.Signal.connect(this.getId('symbols'), 'onclick', this, 'updatePasswordValue'); | ||
141 | |||
142 | MochiKit.Signal.connect(this.getDom('passwordField'), 'onkeyup', this, 'updatePasswordLengthLabel'); | ||
143 | MochiKit.Signal.connect(this.getDom('passwordField'), 'onchange', this, 'updatePasswordLengthLabel'); | ||
144 | MochiKit.Signal.connect(this.getDom('passwordField'), 'onblur', this, 'updatePasswordLengthLabel'); | ||
145 | |||
146 | this.updatePasswordValue(); | ||
147 | |||
148 | passwordGeneratorDialog = new YAHOO.ext.BasicDialog( | ||
149 | passwordGeneratorElement, { | ||
150 | autoCreate:false, | ||
151 | closable:false, | ||
152 | modal:true, | ||
153 | autoTabs:false, | ||
154 | resizable:false, | ||
155 | fixedcenter:true, | ||
156 | constraintoviewport:false, | ||
157 | width:320, | ||
158 | height:130, | ||
159 | shadow:true, | ||
160 | minWidth:200, | ||
161 | minHeight:100 | ||
162 | } | ||
163 | ); | ||
164 | |||
165 | cancelFunction = MochiKit.Base.partial(MochiKit.Base.bind(this.cancelPasswordPanel, this), passwordGeneratorDialog); | ||
166 | passwordGeneratorDialog.addKeyListener(27, cancelFunction); | ||
167 | cancelButton = passwordGeneratorDialog.addButton(Clipperz.PM.Strings['passwordGeneratorPanelCancelLabel'], cancelFunction, this); | ||
168 | |||
169 | okFunction = MochiKit.Base.partial(MochiKit.Base.bind(this.okPasswordPanel, this), passwordGeneratorDialog); | ||
170 | passwordGeneratorDialog.addKeyListener([10, 13], okFunction); | ||
171 | okButton = passwordGeneratorDialog.addButton(Clipperz.PM.Strings['passwordGeneratorPanelOkLabel'], okFunction, this); | ||
172 | |||
173 | MochiKit.Signal.connect(this.getId('passwordGeneratorForm'), 'onsubmit', okFunction); | ||
174 | |||
175 | passwordGeneratorDialog.setDefaultButton(okButton); | ||
176 | |||
177 | this.fieldValueComponent().mainComponent().mainPanel().exitModalView(); | ||
178 | this.fieldValueComponent().mainComponent().scrollToTop(); | ||
179 | |||
180 | // passwordGeneratorDialog.show(this.panelButton().getEl()); | ||
181 | passwordGeneratorDialog.show(this.element()); | ||
182 | this.onMouseLeave(); | ||
183 | }, | ||
184 | |||
185 | //----------------------------------------------------- | ||
186 | |||
187 | 'cancelPasswordPanel': function(aPasswordGeneratorPanel) { | ||
188 | this.fieldValueComponent().mainComponent().mainPanel().enterModalView(); | ||
189 | aPasswordGeneratorPanel.hide(MochiKit.Base.bind(function() { | ||
190 | aPasswordGeneratorPanel.destroy(true); | ||
191 | MochiKit.Signal.disconnectAllTo(this); | ||
192 | }, this)); | ||
193 | }, | ||
194 | |||
195 | //----------------------------------------------------- | ||
196 | |||
197 | 'updatePasswordLengthLabel': function() { | ||
198 | this.getElement('passwordLength').update(this.getDom('passwordField').value.length); | ||
199 | }, | ||
200 | |||
201 | //----------------------------------------------------- | ||
202 | |||
203 | 'okPasswordPanel': function(aPasswordGeneratorPanel, anEvent) { | ||
204 | //MochiKit.Logging.logDebug(">>> PasswordGenerator.okPasswordPanel"); | ||
205 | |||
206 | if (anEvent.stop) { | ||
207 | anEvent.stop(); | ||
208 | } | ||
209 | |||
210 | this.fieldValueComponent().inputElement().dom.focus(); | ||
211 | this.fieldValueComponent().inputElement().dom.value = this.getElement('passwordField').dom.value; | ||
212 | this.getElement('passwordField').dom.focus(); | ||
213 | this.cancelPasswordPanel(aPasswordGeneratorPanel); | ||
214 | |||
215 | return false; | ||
216 | }, | ||
217 | |||
218 | //----------------------------------------------------- | ||
219 | |||
220 | 'updatePasswordValue': function(anEvent) { | ||
221 | varrandomBytes; | ||
222 | varrandomValue; | ||
223 | var charset; | ||
224 | var charsetBitSize; | ||
225 | var stringValue; | ||
226 | varblockIndex; | ||
227 | |||
228 | //MochiKit.Logging.logDebug(">>> updatePasswordValue"); | ||
229 | randomBytes = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(50); | ||
230 | stringValue = ""; | ||
231 | blockIndex = 0; | ||
232 | |||
233 | charset = ""; | ||
234 | if (this.getDom('lowercase').checked) { | ||
235 | charset += Clipperz.PM.Strings['passwordGeneratorLowercaseCharset']; | ||
236 | } | ||
237 | if (this.getDom('uppercase').checked) { | ||
238 | charset += Clipperz.PM.Strings['passwordGeneratorUppercaseCharset']; | ||
239 | } | ||
240 | if (this.getDom('numbers').checked) { | ||
241 | charset += Clipperz.PM.Strings['passwordGeneratorNumberCharset']; | ||
242 | } | ||
243 | if (this.getDom('symbols').checked) { | ||
244 | charset += Clipperz.PM.Strings['passwordGeneratorSymbolCharset']; | ||
245 | } | ||
246 | |||
247 | charsetBitSize = 0; | ||
248 | while (Math.pow(2, charsetBitSize) < charset.length) { | ||
249 | charsetBitSize ++; | ||
250 | } | ||
251 | |||
252 | if (charsetBitSize > 0) { | ||
253 | while (Clipperz.PM.Crypto.passwordEntropy(stringValue) < 128) { | ||
254 | if (((blockIndex + 1)*charsetBitSize) > (randomBytes.length() * 8)) { | ||
255 | randomBytes = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(50); | ||
256 | blockIndex = 0; | ||
257 | } | ||
258 | randomValue = randomBytes.bitBlockAtIndexWithSize(blockIndex*charsetBitSize, charsetBitSize); | ||
259 | if (randomValue < charset.length) { | ||
260 | stringValue += charset.charAt(randomValue); | ||
261 | } | ||
262 | |||
263 | blockIndex ++; | ||
264 | } | ||
265 | } else { | ||
266 | stringValue = ""; | ||
267 | } | ||
268 | |||
269 | this.getElement('passwordField').dom.focus() | ||
270 | this.getElement('passwordField').dom.value = stringValue; | ||
271 | |||
272 | |||
273 | if (anEvent) { | ||
274 | anEvent.src().focus(); | ||
275 | } else { | ||
276 | this.element().focus(); | ||
277 | } | ||
278 | |||
279 | return false; | ||
280 | //MochiKit.Logging.logDebug("<<< updatePasswordValue"); | ||
281 | }, | ||
282 | |||
283 | //----------------------------------------------------- | ||
284 | __syntaxFix__: '__syntaxFix__' | ||
285 | }); | ||