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-tooweek',
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         var pm = this.trigger.child('div/div/div').dom;
159         this.trigger.child('div/div/div').removeClass(this.meterClass);
160         this.trigger.child('div/div/div').addClass(this.meterClass[strength]);
161         console.log('strength1: ' + strength);
162         
163         pm.style.width = (this.meterWidth / 3) * strength + 'px';
164         
165         
166         
167         var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;        
168                 
169         pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
170         
171         this._lastPwd = pwd;
172     },
173     reset: function () {
174         Roo.bootstrap.SecurePass.superclass.reset.call(this);
175         
176         this._lastPwd = '';
177         var pm = this.trigger.child('div/div/div').dom;
178         pm.style.width = 0;
179         
180         var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;        
181                 
182         //pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
183         
184         pt.innerHTML = '';
185         this.inputEl().dom.type='password';
186     },
187     // private
188     validateValue: function (value) {
189         
190         if (!Roo.bootstrap.SecurePass.superclass.validateValue.call(this, value)) {
191             return false;
192         }
193         if (value.length == 0) {
194             if (this.allowBlank) {
195                 this.clearInvalid();
196                 return true;
197             }
198
199             this.markInvalid(this.errors.PwdEmpty);
200             this.errorMsg = this.errors.PwdEmpty;
201             return false;
202         }
203         
204         if(this.insecure){
205             return true;
206         }
207         
208         if ('[\x21-\x7e]*'.match(value)) {
209             this.markInvalid(this.errors.PwdBadChar);
210             this.errorMsg = this.errors.PwdBadChar;
211             return false;
212         }
213         if (value.length < 6) {
214             this.markInvalid(this.errors.PwdShort);
215             this.errorMsg = this.errors.PwdShort;
216             return false;
217         }
218         if (value.length > 16) {
219             this.markInvalid(this.errors.PwdLong);
220             this.errorMsg = this.errors.PwdLong;
221             return false;
222         }
223         var strength;
224         if (this.ClientSideStrongPassword(value)) {
225             strength = 3;
226         } else if (this.ClientSideMediumPassword(value)) {
227             strength = 2;
228         } else if (this.ClientSideWeakPassword(value)) {
229             strength = 1;
230         } else {
231             strength = 0;
232         }
233
234         /*
235         if (strength < 2) {
236             this.markInvalid(this.errors.TooWeak);
237             this.errorMsg = this.errors.TooWeak;
238             return false;
239         }
240         */
241         
242         console.log('strength2: ' + strength);
243         var pm = this.trigger.child('div/div/div').dom;
244                  
245         pm.style.width = (this.meterWidth / 3) * strength + 'px';
246         pm.style.class = 'password-meter-' + this.meterClass[strength];
247                       
248         //pm.innerHTML = this.meterLabel + '&nbsp;' + this.pwdStrengths[strength];
249
250         var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;        
251                 
252         pt.innerHTML = this.meterLabel + '&nbsp;' + this.pwdStrengths[strength];
253         this.errorMsg = ''; 
254         return true;
255     },
256     // private
257     CharacterSetChecks: function (type) {
258         this.type = type;
259         this.fResult = false;
260     },
261     // private
262     isctype: function (character, type) {
263         switch (type) { //why needed break after return in js ? very odd bug
264             case this.kCapitalLetter:
265                 if (character >= 'A' && character <= 'Z') {
266                     return true;
267                 }
268                 break;
269             case this.kSmallLetter:
270                 if (character >= 'a' && character <= 'z') {
271                     return true;
272                 }
273                 break;
274             case this.kDigit:
275                 if (character >= '0' && character <= '9') {
276                     return true;
277                 }
278                 break;
279             case this.kPunctuation:
280                 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
281                     return true;
282                 }
283                 break;
284             default:
285                 return false;
286         }
287
288     },
289     // private
290     IsLongEnough: function (pwd, size) {
291         return !(pwd == null || isNaN(size) || pwd.length < size);
292     },
293     // private
294     SpansEnoughCharacterSets: function (word, nb) {
295         if (!this.IsLongEnough(word, nb))
296         {
297             return false;
298         }
299
300         var characterSetChecks = new Array(
301                 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
302                 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
303         for (var index = 0; index < word.length; ++index) {
304             for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
305                 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
306                     characterSetChecks[nCharSet].fResult = true;
307                     break;
308                 }
309             }
310         }
311
312         var nCharSets = 0;
313         for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
314             if (characterSetChecks[nCharSet].fResult) {
315                 ++nCharSets;
316             }
317         }
318
319         if (nCharSets < nb) {
320             return false;
321         }
322         return true;
323     },
324     // private
325     ClientSideStrongPassword: function (pwd) {
326         return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
327     },
328     // private
329     ClientSideMediumPassword: function (pwd) {
330         return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
331     },
332     // private
333     ClientSideWeakPassword: function (pwd) {
334         return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
335     }
336           
337 })