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     this.meterClass = ["roo-password-meter-tooweak", 
34                        "roo-password-meter-weak", 
35                        "roo-password-meter-medium", 
36                        "roo-password-meter-strong", 
37                        "roo-password-meter-grey"],    
38     Roo.bootstrap.SecurePass.superclass.constructor.call(this, config);
39 }
40
41 Roo.extend(Roo.bootstrap.SecurePass, Roo.bootstrap.Input, {
42     /**
43      * @cfg {String/Object} errors A Error spec, or true for a default spec (defaults to
44      * {
45      *  PwdEmpty: "Please type a password, and then retype it to confirm.",
46      *  PwdShort: "Your password must be at least 6 characters long. Please type a different password.",
47      *  PwdLong: "Your password can't contain more than 16 characters. Please type a different password.",
48      *  PwdBadChar: "The password contains characters that aren't allowed. Please type a different password.",
49      *  IDInPwd: "Your password can't include the part of your ID. Please type a different password.",
50      *  FNInPwd: "Your password can't contain your first name. Please type a different password.",
51      *  LNInPwd: "Your password can't contain your last name. Please type a different password."
52      * })
53      */
54     // private
55     
56     meterWidth: 300,
57     errorMsg :'',    
58     errors: {},
59     imageRoot: '/',
60     /**
61      * @cfg {String/Object} Label for the strength meter (defaults to
62      * 'Password strength:')
63      */
64     // private
65     meterLabel: '',
66     /**
67      * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
68      * ['Weak', 'Medium', 'Strong'])
69      */
70     // private    
71     pwdStrengths: [],    
72     // private
73     strength: 0,
74     // private
75     _lastPwd: null,
76     // private
77     kCapitalLetter: 0,
78     kSmallLetter: 1,
79     kDigit: 2,
80     kPunctuation: 3,
81     
82     insecure: false,
83     // private
84     initEvents: function () {
85         Roo.bootstrap.SecurePass.superclass.initEvents.call(this);
86
87         if (this.el.is('input[type=password]') && Roo.isSafari) {
88             this.el.on('keydown', this.SafariOnKeyDown, this);
89         }
90
91         this.el.on('keyup', this.checkStrength, this, {buffer: 50});
92     },
93     // private
94     onRender: function (ct, position) {
95         Roo.bootstrap.SecurePass.superclass.onRender.call(this, ct, position);
96         this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
97         this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
98
99         this.trigger.createChild({
100             tag: 'div',
101             cls: 'roo-password-meter-container col-xs-12',
102             style: {               
103                 //width: this.meterWidth + 'px'
104             },
105             cn: {
106                 tag: 'div',
107                 cls: 'roo-password-meter-grey',
108                 style: {
109                     //width: this.meterWidth + 'px'                                        
110                 },
111                 cn: [
112                     {
113                     //id: 'PwdMeter',
114                     tag: 'div',
115                     cls: 'roo-password-meter-grey col-xs-12',
116                     style: {
117                         //width: 0,
118                         //width: this.meterWidth + 'px'                                                
119                         }
120                     },
121                     {                            
122                          cls: 'roo-password-meter-text'                          
123                     }
124                 ]                
125             }
126         });
127         if (this.hideTrigger) {
128             this.trigger.setDisplayed(false);
129         }
130         this.setSize(this.width || '', this.height || '');
131     },
132     // private
133     onDestroy: function () {
134         if (this.trigger) {
135             this.trigger.removeAllListeners();
136             this.trigger.remove();
137         }
138         if (this.wrap) {
139             this.wrap.remove();
140         }
141         Roo.bootstrap.TriggerField.superclass.onDestroy.call(this);
142     },
143     // private
144     checkStrength: function () {
145         var pwd = this.inputEl().getValue();
146         if (pwd == this._lastPwd) {
147             return;
148         }
149
150         var strength;
151         if (this.ClientSideStrongPassword(pwd)) {
152             strength = 3;
153         } else if (this.ClientSideMediumPassword(pwd)) {
154             strength = 2;
155         } else if (this.ClientSideWeakPassword(pwd)) {
156             strength = 1;
157         } else {
158             strength = 0;
159         }
160         
161         console.log('strength1: ' + strength);
162         
163         //var pm = this.trigger.child('div/div/div').dom;
164         var pm = this.trigger.child('div/div/div');
165         pm.removeClass(this.meterClass);
166         pm.addClass(this.meterClass[strength]);
167                 
168         
169         var pt = this.trigger.child('/div/div/').child('>*[class=roo-password-meter-text]').dom;        
170                 
171         pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
172         
173         this._lastPwd = pwd;
174     },
175     reset: function () {
176         Roo.bootstrap.SecurePass.superclass.reset.call(this);
177         
178         this._lastPwd = '';
179         
180         var pm = this.trigger.child('div/div/div');
181         pm.removeClass(this.meterClass);
182         pm.addClass('roo-password-meter-grey');        
183         
184         
185         var pt = this.trigger.child('/div/div/').child('>*[class=roo-password-meter-text]').dom;        
186         
187         pt.innerHTML = '';
188         this.inputEl().dom.type='password';
189     },
190     // private
191     validateValue: function (value) {
192         
193         if (!Roo.bootstrap.SecurePass.superclass.validateValue.call(this, value)) {
194             return false;
195         }
196         if (value.length == 0) {
197             if (this.allowBlank) {
198                 this.clearInvalid();
199                 return true;
200             }
201
202             this.markInvalid(this.errors.PwdEmpty);
203             this.errorMsg = this.errors.PwdEmpty;
204             return false;
205         }
206         
207         if(this.insecure){
208             return true;
209         }
210         
211         if ('[\x21-\x7e]*'.match(value)) {
212             this.markInvalid(this.errors.PwdBadChar);
213             this.errorMsg = this.errors.PwdBadChar;
214             return false;
215         }
216         if (value.length < 6) {
217             this.markInvalid(this.errors.PwdShort);
218             this.errorMsg = this.errors.PwdShort;
219             return false;
220         }
221         if (value.length > 16) {
222             this.markInvalid(this.errors.PwdLong);
223             this.errorMsg = this.errors.PwdLong;
224             return false;
225         }
226         var strength;
227         if (this.ClientSideStrongPassword(value)) {
228             strength = 3;
229         } else if (this.ClientSideMediumPassword(value)) {
230             strength = 2;
231         } else if (this.ClientSideWeakPassword(value)) {
232             strength = 1;
233         } else {
234             strength = 0;
235         }
236
237         
238         if (strength < 2) {
239             //this.markInvalid(this.errors.TooWeak);
240             this.errorMsg = this.errors.TooWeak;
241             //return false;
242         }
243         
244         
245         console.log('strength2: ' + strength);
246         
247         //var pm = this.trigger.child('div/div/div').dom;
248         
249         var pm = this.trigger.child('div/div/div');
250         pm.removeClass(this.meterClass);
251         pm.addClass(this.meterClass[strength]);
252                 
253         var pt = this.trigger.child('/div/div/').child('>*[class=roo-password-meter-text]').dom;        
254                 
255         pt.innerHTML = this.meterLabel + '&nbsp;' + this.pwdStrengths[strength];
256         
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 })