49c4427fafecdc229643c9a339e9e7127d99d4e9
[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             cls: 'password-meter-container col-xs-18',
97             style: {               
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 col-xs-12',
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         //pm.style.width = (this.meterWidth / 3) * strength + 'px';
164                
165         
166         var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;        
167                 
168         pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
169         
170         this._lastPwd = pwd;
171     },
172     reset: function () {
173         Roo.bootstrap.SecurePass.superclass.reset.call(this);
174         
175         this._lastPwd = '';
176         //var pm = this.trigger.child('div/div/div').dom;
177         //pm.style.width = 0;
178         var pm = this.trigger.child('div/div/div');
179         pm.removeClass(this.meterClass);
180         pm.addClass(this.meterClass[0]);        
181         
182         
183         var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;        
184                 
185         //pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
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=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 })