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