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