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     meterWidth: 300,
51     errors: {},
52     imageRoot: '/',
53     /**
54      * @cfg {String/Object} Label for the strength meter (defaults to
55      * 'Password strength:')
56      */
57     // private
58     meterLabel: '',
59     /**
60      * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
61      * ['Weak', 'Medium', 'Strong'])
62      */
63     // private
64     pwdStrengths: [],    
65     // private
66     strength: 0,
67     // private
68     _lastPwd: null,
69     // private
70     kCapitalLetter: 0,
71     kSmallLetter: 1,
72     kDigit: 2,
73     kPunctuation: 3,
74     
75     insecure: false,
76     // private
77     initEvents: function () {
78         Roo.bootstrap.SecurePass.superclass.initEvents.call(this);
79
80         if (this.el.is('input[type=password]') && Roo.isSafari) {
81             this.el.on('keydown', this.SafariOnKeyDown, this);
82         }
83
84         this.el.on('keyup', this.checkStrength, this, {buffer: 50});
85     },
86     // private
87     onRender: function (ct, position) {
88         Roo.bootstrap.SecurePass.superclass.onRender.call(this, ct, position);
89         this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
90         this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
91
92         this.trigger.createChild({
93             tag: 'div',
94             style: {
95                 'margin-bottom': '20px',
96                 width: this.meterWidth + 'px'
97             },
98             cn: {
99                 tag: 'div',
100                 cls: 'password-meter-grey',
101                 style: {
102                     width: this.meterWidth + 'px',
103                     height: '10px'                    
104                 },
105                 cn: [
106                     {
107                     //id: 'PwdMeter',
108                     tag: 'div',
109                     cls: 'password-meter',
110                     style: {
111                         //width: 0,
112                         height: '10px'                        
113                         }
114                     },
115                     {                            
116                          cls: 'password-meter-text',
117                          height: '10px'
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         var pm = this.trigger.child('div/div/div').dom;
156         console.log('strength1: ' + strength);
157         
158         pm.style.width = (this.meterWidth / 3) * strength + 'px';
159         
160         var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;        
161                 
162         pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
163         
164         this._lastPwd = pwd;
165     },
166     reset: function () {
167         Roo.bootstrap.SecurePass.superclass.reset.call(this);
168         
169         this._lastPwd = '';
170         var pm = this.trigger.child('div/div/div').dom;
171         pm.style.width = 0;
172         
173         var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;        
174                 
175         //pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
176         
177         pt.innerHTML = '';
178         this.inputEl().dom.type='password';
179     },
180     // private
181     validateValue: function (value) {
182         
183         if (!Roo.bootstrap.SecurePass.superclass.validateValue.call(this, value)) {
184             return false;
185         }
186         if (value.length == 0) {
187             if (this.allowBlank) {
188                 this.clearInvalid();
189                 return true;
190             }
191
192             this.markInvalid(this.errors.PwdEmpty);
193             return false;
194         }
195         
196         if(this.insecure){
197             return true;
198         }
199         
200         if ('[\x21-\x7e]*'.match(value)) {
201             this.markInvalid(this.errors.PwdBadChar);
202             return false;
203         }
204         if (value.length < 6) {
205             this.markInvalid(this.errors.PwdShort);
206             return false;
207         }
208         if (value.length > 16) {
209             this.markInvalid(this.errors.PwdLong);
210             return false;
211         }
212         var strength;
213         if (this.ClientSideStrongPassword(value)) {
214             strength = 3;
215         } else if (this.ClientSideMediumPassword(value)) {
216             strength = 2;
217         } else if (this.ClientSideWeakPassword(value)) {
218             strength = 1;
219         } else {
220             strength = 0;
221         }
222
223         // allow weak password
224         //if (strength < 2) {
225         //    this.markInvalid(this.errors.TooWeak);
226         //    return false;
227         //}
228         
229         console.log('strength2: ' + strength);
230         var pm = this.trigger.child('div/div/div').dom;
231                  
232         pm.style.width = (this.meterWidth / 3) * strength + 'px';
233                       
234         //pm.innerHTML = this.meterLabel + '&nbsp;' + this.pwdStrengths[strength];
235
236         var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;        
237                 
238         pt.innerHTML = this.meterLabel + '&nbsp;' + this.pwdStrengths[strength];
239                         
240         return true;
241     },
242     // private
243     CharacterSetChecks: function (type) {
244         this.type = type;
245         this.fResult = false;
246     },
247     // private
248     isctype: function (character, type) {
249         switch (type) { //why needed break after return in js ? very odd bug
250             case this.kCapitalLetter:
251                 if (character >= 'A' && character <= 'Z') {
252                     return true;
253                 }
254                 break;
255             case this.kSmallLetter:
256                 if (character >= 'a' && character <= 'z') {
257                     return true;
258                 }
259                 break;
260             case this.kDigit:
261                 if (character >= '0' && character <= '9') {
262                     return true;
263                 }
264                 break;
265             case this.kPunctuation:
266                 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
267                     return true;
268                 }
269                 break;
270             default:
271                 return false;
272         }
273
274     },
275     // private
276     IsLongEnough: function (pwd, size) {
277         return !(pwd == null || isNaN(size) || pwd.length < size);
278     },
279     // private
280     SpansEnoughCharacterSets: function (word, nb) {
281         if (!this.IsLongEnough(word, nb))
282         {
283             return false;
284         }
285
286         var characterSetChecks = new Array(
287                 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
288                 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
289         for (var index = 0; index < word.length; ++index) {
290             for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
291                 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
292                     characterSetChecks[nCharSet].fResult = true;
293                     break;
294                 }
295             }
296         }
297
298         var nCharSets = 0;
299         for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
300             if (characterSetChecks[nCharSet].fResult) {
301                 ++nCharSets;
302             }
303         }
304
305         if (nCharSets < nb) {
306             return false;
307         }
308         return true;
309     },
310     // private
311     ClientSideStrongPassword: function (pwd) {
312         return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
313     },
314     // private
315     ClientSideMediumPassword: function (pwd) {
316         return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
317     },
318     // private
319     ClientSideWeakPassword: function (pwd) {
320         return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
321     }
322           
323 })