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                 },
106                 cn: [
107                     {
108                     //id: 'PwdMeter',
109                     tag: 'div',
110                     cls: 'password-meter-tooweek',
111                     style: {
112                         //width: 0,
113                         width: this.meterWidth + 'px'                                                
114                         }
115                     },
116                     {                            
117                          cls: 'password-meter-text'                      
118                     }
119                 ]                
120             }
121         });
122         if (this.hideTrigger) {
123             this.trigger.setDisplayed(false);
124         }
125         this.setSize(this.width || '', this.height || '');
126     },
127     // private
128     onDestroy: function () {
129         if (this.trigger) {
130             this.trigger.removeAllListeners();
131             this.trigger.remove();
132         }
133         if (this.wrap) {
134             this.wrap.remove();
135         }
136         Roo.bootstrap.TriggerField.superclass.onDestroy.call(this);
137     },
138     // private
139     checkStrength: function () {
140         var pwd = this.inputEl().getValue();
141         if (pwd == this._lastPwd) {
142             return;
143         }
144
145         var strength;
146         if (this.ClientSideStrongPassword(pwd)) {
147             strength = 3;
148         } else if (this.ClientSideMediumPassword(pwd)) {
149             strength = 2;
150         } else if (this.ClientSideWeakPassword(pwd)) {
151             strength = 1;
152         } else {
153             strength = 0;
154         }
155         
156         console.log('strength1: ' + strength);
157         
158         //var pm = this.trigger.child('div/div/div').dom;
159         var pm = this.trigger.child('div/div/div');
160         //pm.removeClass(this.meterClass);
161         //pm.addClass(this.meterClass[strength]);
162         
163         
164         //pm.style.width = (this.meterWidth / 3) * strength + 'px';
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         var pm = this.trigger.child('div/div/div');
180         //pm.removeClass(this.meterClass);
181         //pm.addClass(this.meterClass[0]);        
182         
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         
248         //var pm = this.trigger.child('div/div/div').dom;
249         
250         var pm = this.trigger.child('div/div/div');
251         //pm.removeClass(this.meterClass);
252         //pm.addClass(this.meterClass[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         
258         this.errorMsg = ''; 
259         return true;
260     },
261     // private
262     CharacterSetChecks: function (type) {
263         this.type = type;
264         this.fResult = false;
265     },
266     // private
267     isctype: function (character, type) {
268         switch (type) { //why needed break after return in js ? very odd bug
269             case this.kCapitalLetter:
270                 if (character >= 'A' && character <= 'Z') {
271                     return true;
272                 }
273                 break;
274             case this.kSmallLetter:
275                 if (character >= 'a' && character <= 'z') {
276                     return true;
277                 }
278                 break;
279             case this.kDigit:
280                 if (character >= '0' && character <= '9') {
281                     return true;
282                 }
283                 break;
284             case this.kPunctuation:
285                 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
286                     return true;
287                 }
288                 break;
289             default:
290                 return false;
291         }
292
293     },
294     // private
295     IsLongEnough: function (pwd, size) {
296         return !(pwd == null || isNaN(size) || pwd.length < size);
297     },
298     // private
299     SpansEnoughCharacterSets: function (word, nb) {
300         if (!this.IsLongEnough(word, nb))
301         {
302             return false;
303         }
304
305         var characterSetChecks = new Array(
306                 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
307                 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
308         for (var index = 0; index < word.length; ++index) {
309             for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
310                 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
311                     characterSetChecks[nCharSet].fResult = true;
312                     break;
313                 }
314             }
315         }
316
317         var nCharSets = 0;
318         for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
319             if (characterSetChecks[nCharSet].fResult) {
320                 ++nCharSets;
321             }
322         }
323
324         if (nCharSets < nb) {
325             return false;
326         }
327         return true;
328     },
329     // private
330     ClientSideStrongPassword: function (pwd) {
331         return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
332     },
333     // private
334     ClientSideMediumPassword: function (pwd) {
335         return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
336     },
337     // private
338     ClientSideWeakPassword: function (pwd) {
339         return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
340     }
341           
342 })