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