widgets/SecurePass.js
[Pman.Core] / widgets / SecurePass.js
1
2 //<script type="text/Javascript">
3
4
5 Ext.form.SecurePass = function(config) {
6         // these go here, so the translation tool can replace them..
7     this.errors = {
8                 PwdEmpty: "Please type a password, and then retype it to confirm.",
9                 PwdShort: "Your password must be at least 6 characters long. Please type a different password.",
10                 PwdLong: "Your password can't contain more than 16 characters. Please type a different password.",
11                 PwdBadChar: "The password contains characters that aren't allowed. Please type a different password.",
12                 IDInPwd: "Your password can't include the part of your ID. Please type a different password.",
13                 FNInPwd: "Your password can't contain your first name. Please type a different password.",
14                 LNInPwd: "Your password can't contain your last name. Please type a different password.",
15         TooWeak: "Your password is Too Weak."
16         },
17     this.meterLabel = "Password strength:";
18     this.pwdStrengths = ["Too Weak" , "Weak", "Medium", "Strong"];
19     Ext.form.SecurePass.superclass.constructor.call(this, config);
20 }
21
22 Ext.extend(Ext.form.SecurePass, Ext.form.TextField, {
23         /**
24          * @cfg {String/Object} errors A Error spec, or true for a default spec (defaults to
25          * {
26          *  PwdEmpty: "Please type a password, and then retype it to confirm.",
27          *  PwdShort: "Your password must be at least 6 characters long. Please type a different password.",
28          *  PwdLong: "Your password can't contain more than 16 characters. Please type a different password.",
29          *  PwdBadChar: "The password contains characters that aren't allowed. Please type a different password.",
30          *  IDInPwd: "Your password can't include the part of your ID. Please type a different password.",
31          *  FNInPwd: "Your password can't contain your first name. Please type a different password.",
32          *  LNInPwd: "Your password can't contain your last name. Please type a different password."
33          * })
34          */
35         // private
36         errors : {},
37     
38     imageRoot: '/',
39         
40         /**
41          * @cfg {String/Object} Label for the strength meter (defaults to
42          * 'Password strength:')
43          */
44         // private
45         meterLabel : '',
46
47         /**
48          * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
49          * ['Weak', 'Medium', 'Strong'])
50          */
51         // private
52         pwdStrengths : [],
53
54         /**
55          * @cfg {String/Object} fieldsFilter A fieldsFilter spec, as [['field_name', 'error_id'], ...]
56          */
57         // private
58         fieldsFilter : [],
59
60         // private
61         strength : 0,
62
63         // private
64         _lastPwd : null,
65     
66         // private
67         kCapitalLetter : 0,
68         kSmallLetter : 1,
69         kDigit : 2,
70         kPunctuation : 3,
71
72     // private
73     initEvents : function(){
74             if(this.inputType == 'password'){
75                 this.el.on('keydown',function(event){
76                     Roo.log(this.getValue().length);
77                     if((event.getKey() == 8 || event.getKey() == 46)){ // backspace and delete key
78                         event.preventDefault();
79                         this.setValue('');
80                     };
81                 },this);
82             }
83             Ext.form.SecurePass.superclass.initEvents.call(this);
84             this.el.on('keyup', this.checkStrength, this, {buffer:50});
85         },
86
87         // private
88         onRender : function(ct, position){
89                 Ext.form.SecurePass.superclass.onRender.call(this, ct, position);
90                 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
91                 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter '+this.triggerClass});
92                  
93                 this.trigger.createChild({
94             tag: 'div',  
95             style: {
96                 'margin-bottom': '10px',
97                 width: this.width + 'px'
98             },
99             cn: {
100                 tag: 'div', 
101                 style: {
102                     width: this.width + 'px',
103                     height: '9px',
104                     'background-image' : 'url(\''+this.imageRoot+'/password_meter_grey.gif\')',
105                     'background-position' : 'center center',
106                     'background-repeat': 'no-repeat'
107                 },
108                 cn : {
109                     //id: 'PwdMeter',
110                     tag: 'div',
111                     style:  {
112                         width: 0,
113                         height: '9px',
114                         'background-image': 'url(\''+this.imageRoot+'/password_meter.gif\')',
115                         'background-position': 'center center',
116                         'background-repeat': 'no-repeat',
117                         'font-size': '9px'
118                     }
119                 }
120             }
121         });
122                 if(this.hideTrigger){
123                         this.trigger.setDisplayed(false);
124                 }
125                 this.setSize(this.width||'', this.height||'');
126         },
127
128         // private
129         onDestroy : function(){
130                 if(this.trigger){
131                         this.trigger.removeAllListeners();
132                         this.trigger.remove();
133                 }
134                 if(this.wrap){
135                         this.wrap.remove();
136                 }
137                 Ext.form.TriggerField.superclass.onDestroy.call(this);
138         },
139     
140         // private
141         checkStrength : function(){
142                 var pwd = this.el.getValue();
143                 if (pwd == this._lastPwd) {
144                         return;
145                 }
146
147                 var strength;
148                 if (this.ClientSideStrongPassword(pwd)) {
149                         strength = 3;
150                 } else if(this.ClientSideMediumPassword(pwd)) {
151                         strength = 2;
152                 } else if(this.ClientSideWeakPassword(pwd)) {
153                         strength = 1;
154                 } else {
155                         strength = 0;
156                 }
157         var pm = this.trigger.child('div/div/div').dom;
158         
159                 pm.style.width = (this.width/3) * strength +'px';
160                 //if(this.pwdStrengths != null && strength > 0) {
161         pm.innerHTML = this.meterLabel + '&nbsp;'+ this.pwdStrengths[strength];
162                 //} else {
163                 //      pm.innerHTML = '';
164                 //}
165
166                 this._lastPwd = pwd;
167         },
168     reset : function(){
169         Ext.form.SecurePass.superclass.reset.call(this);
170         this._lastPwd = '';
171         var pm = this.trigger.child('div/div/div').dom;
172         pm.style.width = 0;
173         pm.innerHTML = '';
174     },
175     // private
176         validateValue : function(value){
177                 if (!Ext.form.TextField.superclass.validateValue.call(this, value)){
178             return false;
179         }
180                 if (value.length == 0) {
181             if (this.allowBlank) {
182                 this.clearInvalid();
183                 return true;
184             }
185             
186                         this.markInvalid(this.errors.PwdEmpty);
187             return false;
188                 }
189                 if ('[\x21-\x7e]*'.match(value)) {
190                         this.markInvalid(this.errors.PwdBadChar);
191             return false;
192                 }
193                 if (value.length < 6) {
194                         this.markInvalid(this.errors.PwdShort);
195             return false;
196                 }
197                 if (value.length > 16) {
198                         this.markInvalid(this.errors.PwdLong);
199             return false;
200                 }
201         var strength;
202                 if (this.ClientSideStrongPassword(value)) {
203                         strength = 3;
204                 } else if(this.ClientSideMediumPassword(value)) {
205                         strength = 2;
206                 } else if(this.ClientSideWeakPassword(value)) {
207                         strength = 1;
208                 } else {
209                         strength = 0;
210                 }
211         if (strength < 2) {
212                         this.markInvalid(this.errors.TooWeak);
213             return false;
214                 }
215         /*
216                 for (var index = 0; index < this.fieldsFilter.length; ++index) {
217                         filter = document.getElementById(this.fieldsFilter[index][0]).value;
218                         if (filter != '')
219                         {
220                                 re = new RegExp(filter);
221                                 if (re.test(value)) {
222                                         this.markInvalid(eval('this.errors.'+ this.fieldsFilter[index][1]));
223                                         return false;
224                                 }
225                         }
226                 }
227         */
228                 return true;
229         },
230
231     // private
232         CharacterSetChecks : function(type){
233                 this.type = type;
234                 this.fResult = false;
235         },
236
237     // private
238         isctype : function(character, type){
239                 switch (type) { //why needed break after return in js ? very odd bug
240                         case this.kCapitalLetter: if (character >= 'A' && character <= 'Z') { return true; } break;
241                         case this.kSmallLetter: if (character >= 'a' && character <= 'z') { return true; } break;
242                         case this.kDigit: if (character >= '0' && character <= '9') { return true; } break;
243                         case this.kPunctuation: if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) { return true; } break;
244                         default: return false;
245                 }
246         
247         },
248
249     // private
250         IsLongEnough : function(pwd, size){
251                 return !(pwd == null || isNaN(size) || pwd.length < size);
252         },
253
254     // private
255         SpansEnoughCharacterSets : function(word, nb){
256                 if (!this.IsLongEnough(word, nb))
257                 {
258                         return false;
259                 }
260
261                 var characterSetChecks = new Array(
262                         new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
263                         new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
264                 for (var index = 0; index < word.length; ++index) {
265                         for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
266                                 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
267                                         characterSetChecks[nCharSet].fResult = true;
268                                         break;
269                                 }
270                         }
271                 }
272
273                 var nCharSets = 0;
274                 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
275                         if (characterSetChecks[nCharSet].fResult) {
276                                 ++nCharSets;
277                         }
278                 }
279
280                 if (nCharSets < nb) {
281                         return false;
282                 }
283                 return true;
284         },
285
286     // private
287         ClientSideStrongPassword : function(pwd){
288                 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
289         },
290
291     // private
292         ClientSideMediumPassword : function(pwd){
293                 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
294         },
295
296     // private
297         ClientSideWeakPassword : function(pwd){
298                 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
299         }
300 })