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             $('input[type="password"]').on('keydown',function(event){
75                 if(event.which == 8){ // backspace
76                     event.preventDefault();
77                     $(this).val('');
78                 }
79             });
80             Ext.form.SecurePass.superclass.initEvents.call(this);
81             this.el.on('keyup', this.checkStrength, this, {buffer:50});
82         },
83
84         // private
85         onRender : function(ct, position){
86                 Ext.form.SecurePass.superclass.onRender.call(this, ct, position);
87                 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
88                 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter '+this.triggerClass});
89                  
90                 this.trigger.createChild({
91             tag: 'div',  
92             style: {
93                 'margin-bottom': '10px',
94                 width: this.width + 'px'
95             },
96             cn: {
97                 tag: 'div', 
98                 style: {
99                     width: this.width + 'px',
100                     height: '9px',
101                     'background-image' : 'url(\''+this.imageRoot+'/password_meter_grey.gif\')',
102                     'background-position' : 'center center',
103                     'background-repeat': 'no-repeat'
104                 },
105                 cn : {
106                     //id: 'PwdMeter',
107                     tag: 'div',
108                     style:  {
109                         width: 0,
110                         height: '9px',
111                         'background-image': 'url(\''+this.imageRoot+'/password_meter.gif\')',
112                         'background-position': 'center center',
113                         'background-repeat': 'no-repeat',
114                         'font-size': '9px'
115                     }
116                 }
117             }
118         });
119                 if(this.hideTrigger){
120                         this.trigger.setDisplayed(false);
121                 }
122                 this.setSize(this.width||'', this.height||'');
123         },
124
125         // private
126         onDestroy : function(){
127                 if(this.trigger){
128                         this.trigger.removeAllListeners();
129                         this.trigger.remove();
130                 }
131                 if(this.wrap){
132                         this.wrap.remove();
133                 }
134                 Ext.form.TriggerField.superclass.onDestroy.call(this);
135         },
136     
137         // private
138         checkStrength : function(){
139                 var pwd = this.el.getValue();
140                 if (pwd == this._lastPwd) {
141                         return;
142                 }
143
144                 var strength;
145                 if (this.ClientSideStrongPassword(pwd)) {
146                         strength = 3;
147                 } else if(this.ClientSideMediumPassword(pwd)) {
148                         strength = 2;
149                 } else if(this.ClientSideWeakPassword(pwd)) {
150                         strength = 1;
151                 } else {
152                         strength = 0;
153                 }
154         var pm = this.trigger.child('div/div/div').dom;
155         
156                 pm.style.width = (this.width/3) * strength +'px';
157                 //if(this.pwdStrengths != null && strength > 0) {
158         pm.innerHTML = this.meterLabel + '&nbsp;'+ this.pwdStrengths[strength];
159                 //} else {
160                 //      pm.innerHTML = '';
161                 //}
162
163                 this._lastPwd = pwd;
164         },
165     reset : function(){
166         Ext.form.SecurePass.superclass.reset.call(this);
167         this._lastPwd = '';
168         var pm = this.trigger.child('div/div/div').dom;
169         pm.style.width = 0;
170         pm.innerHTML = '';
171     },
172     // private
173         validateValue : function(value){
174                 if (!Ext.form.TextField.superclass.validateValue.call(this, value)){
175             return false;
176         }
177                 if (value.length == 0) {
178             if (this.allowBlank) {
179                 this.clearInvalid();
180                 return true;
181             }
182             
183                         this.markInvalid(this.errors.PwdEmpty);
184             return false;
185                 }
186                 if ('[\x21-\x7e]*'.match(value)) {
187                         this.markInvalid(this.errors.PwdBadChar);
188             return false;
189                 }
190                 if (value.length < 6) {
191                         this.markInvalid(this.errors.PwdShort);
192             return false;
193                 }
194                 if (value.length > 16) {
195                         this.markInvalid(this.errors.PwdLong);
196             return false;
197                 }
198         var strength;
199                 if (this.ClientSideStrongPassword(value)) {
200                         strength = 3;
201                 } else if(this.ClientSideMediumPassword(value)) {
202                         strength = 2;
203                 } else if(this.ClientSideWeakPassword(value)) {
204                         strength = 1;
205                 } else {
206                         strength = 0;
207                 }
208         if (strength < 2) {
209                         this.markInvalid(this.errors.TooWeak);
210             return false;
211                 }
212         /*
213                 for (var index = 0; index < this.fieldsFilter.length; ++index) {
214                         filter = document.getElementById(this.fieldsFilter[index][0]).value;
215                         if (filter != '')
216                         {
217                                 re = new RegExp(filter);
218                                 if (re.test(value)) {
219                                         this.markInvalid(eval('this.errors.'+ this.fieldsFilter[index][1]));
220                                         return false;
221                                 }
222                         }
223                 }
224         */
225                 return true;
226         },
227
228     // private
229         CharacterSetChecks : function(type){
230                 this.type = type;
231                 this.fResult = false;
232         },
233
234     // private
235         isctype : function(character, type){
236                 switch (type) { //why needed break after return in js ? very odd bug
237                         case this.kCapitalLetter: if (character >= 'A' && character <= 'Z') { return true; } break;
238                         case this.kSmallLetter: if (character >= 'a' && character <= 'z') { return true; } break;
239                         case this.kDigit: if (character >= '0' && character <= '9') { return true; } break;
240                         case this.kPunctuation: if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) { return true; } break;
241                         default: return false;
242                 }
243         
244         },
245
246     // private
247         IsLongEnough : function(pwd, size){
248                 return !(pwd == null || isNaN(size) || pwd.length < size);
249         },
250
251     // private
252         SpansEnoughCharacterSets : function(word, nb){
253                 if (!this.IsLongEnough(word, nb))
254                 {
255                         return false;
256                 }
257
258                 var characterSetChecks = new Array(
259                         new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
260                         new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
261                 for (var index = 0; index < word.length; ++index) {
262                         for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
263                                 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
264                                         characterSetChecks[nCharSet].fResult = true;
265                                         break;
266                                 }
267                         }
268                 }
269
270                 var nCharSets = 0;
271                 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
272                         if (characterSetChecks[nCharSet].fResult) {
273                                 ++nCharSets;
274                         }
275                 }
276
277                 if (nCharSets < nb) {
278                         return false;
279                 }
280                 return true;
281         },
282
283     // private
284         ClientSideStrongPassword : function(pwd){
285                 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
286         },
287
288     // private
289         ClientSideMediumPassword : function(pwd){
290                 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
291         },
292
293     // private
294         ClientSideWeakPassword : function(pwd){
295                 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
296         }
297 })