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