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     bypassValidate: true,
52     errors: {},
53     imageRoot: '/',
54     /**
55      * @cfg {String/Object} Label for the strength meter (defaults to
56      * 'Password strength:')
57      */
58     // private
59     meterLabel: '',
60     /**
61      * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
62      * ['Weak', 'Medium', 'Strong'])
63      */
64     // private
65     pwdStrengths: [],    
66     // private
67     strength: 0,
68     // private
69     _lastPwd: null,
70     // private
71     kCapitalLetter: 0,
72     kSmallLetter: 1,
73     kDigit: 2,
74     kPunctuation: 3,
75     
76     insecure: false,
77     // private
78     initEvents: function () {
79         Roo.bootstrap.SecurePass.superclass.initEvents.call(this);
80
81         if (this.el.is('input[type=password]') && Roo.isSafari) {
82             this.el.on('keydown', this.SafariOnKeyDown, this);
83         }
84
85         this.el.on('keyup', this.checkStrength, this, {buffer: 50});
86     },
87     // private
88     onRender: function (ct, position) {
89         Roo.bootstrap.SecurePass.superclass.onRender.call(this, ct, position);
90         this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
91         this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
92
93         this.trigger.createChild({
94             tag: 'div',
95             style: {
96                 'margin-bottom': '20px',
97                 width: this.meterWidth + 'px'
98             },
99             cn: {
100                 tag: 'div',
101                 cls: 'password-meter-grey',
102                 style: {
103                     width: this.meterWidth + 'px',
104                     height: '10px'                    
105                 },
106                 cn: [
107                     {
108                     //id: 'PwdMeter',
109                     tag: 'div',
110                     cls: 'password-meter',
111                     style: {
112                         //width: 0,
113                         height: '10px'                        
114                         }
115                     },
116                     {                            
117                          cls: 'password-meter-text',
118                          height: '10px'
119                     }
120                 ]                
121             }
122         });
123         if (this.hideTrigger) {
124             this.trigger.setDisplayed(false);
125         }
126         this.setSize(this.width || '', this.height || '');
127     },
128     // private
129     onDestroy: function () {
130         if (this.trigger) {
131             this.trigger.removeAllListeners();
132             this.trigger.remove();
133         }
134         if (this.wrap) {
135             this.wrap.remove();
136         }
137         Roo.bootstrap.TriggerField.superclass.onDestroy.call(this);
138     },
139     // private
140     checkStrength: function () {
141         var pwd = this.inputEl().getValue();
142         if (pwd == this._lastPwd) {
143             return;
144         }
145
146         var strength;
147         if (this.ClientSideStrongPassword(pwd)) {
148             strength = 3;
149         } else if (this.ClientSideMediumPassword(pwd)) {
150             strength = 2;
151         } else if (this.ClientSideWeakPassword(pwd)) {
152             strength = 1;
153         } else {
154             strength = 0;
155         }
156         var pm = this.trigger.child('div/div/div').dom;
157         console.log('strength1: ' + strength);
158         
159         pm.style.width = (this.meterWidth / 3) * strength + 'px';
160         
161         var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;        
162                 
163         pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
164         
165         this._lastPwd = pwd;
166     },
167     reset: function () {
168         Roo.bootstrap.SecurePass.superclass.reset.call(this);
169         
170         this._lastPwd = '';
171         var pm = this.trigger.child('div/div/div').dom;
172         pm.style.width = 0;
173         
174         var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;        
175                 
176         //pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
177         
178         pt.innerHTML = '';
179         this.inputEl().dom.type='password';
180     },
181     // private
182     validateValue: function (value) {
183         
184         if (!Roo.bootstrap.SecurePass.superclass.validateValue.call(this, value)) {
185             return false;
186         }
187         if (value.length == 0) {
188             if (this.allowBlank) {
189                 this.clearInvalid();
190                 return true;
191             }
192
193             this.markInvalid(this.errors.PwdEmpty);
194             return false;
195         }
196         
197         if(this.insecure){
198             return true;
199         }
200         
201         if ('[\x21-\x7e]*'.match(value)) {
202             this.markInvalid(this.errors.PwdBadChar);
203             return false;
204         }
205         if (value.length < 6) {
206             this.markInvalid(this.errors.PwdShort);
207             return false;
208         }
209         if (value.length > 16) {
210             this.markInvalid(this.errors.PwdLong);
211             return false;
212         }
213         var strength;
214         if (this.ClientSideStrongPassword(value)) {
215             strength = 3;
216         } else if (this.ClientSideMediumPassword(value)) {
217             strength = 2;
218         } else if (this.ClientSideWeakPassword(value)) {
219             strength = 1;
220         } else {
221             strength = 0;
222         }
223
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 })