sync
[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: 200,
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': '10px',
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                     //id: 'PwdMeter',
107                     tag: 'div',
108                     cls: 'password-meter',
109                     style: {
110                         //width: 0,
111                         height: '10px'                        
112                     }
113                 }
114             }
115         });
116         if (this.hideTrigger) {
117             this.trigger.setDisplayed(false);
118         }
119         this.setSize(this.width || '', this.height || '');
120     },
121     // private
122     onDestroy: function () {
123         if (this.trigger) {
124             this.trigger.removeAllListeners();
125             this.trigger.remove();
126         }
127         if (this.wrap) {
128             this.wrap.remove();
129         }
130         Roo.bootstrap.TriggerField.superclass.onDestroy.call(this);
131     },
132     // private
133     checkStrength: function () {
134         var pwd = this.el.getValue();
135         if (pwd == this._lastPwd) {
136             return;
137         }
138
139         var strength;
140         if (this.ClientSideStrongPassword(pwd)) {
141             strength = 3;
142         } else if (this.ClientSideMediumPassword(pwd)) {
143             strength = 2;
144         } else if (this.ClientSideWeakPassword(pwd)) {
145             strength = 1;
146         } else {
147             strength = 0;
148         }
149         var pm = this.trigger.child('div/div/div').dom;
150         console.log('strength1: ' + strength);
151         
152         pm.style.width = (this.meterWidth / 3) * strength + 'px';
153         //if(this.pwdStrengths != null && strength > 0) {
154         pm.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
155         //} else {
156         //      pm.innerHTML = '';
157         //}
158         
159         this._lastPwd = pwd;
160     },
161     reset: function () {
162         Roo.bootstrap.SecurePass.superclass.reset.call(this);
163         this._lastPwd = '';
164         var pm = this.trigger.child('div/div/div').dom;
165         pm.style.width = 0;
166         pm.innerHTML = '';
167     },
168     // private
169     validateValue: function (value) {
170         
171         if (!Roo.bootstrap.SecurePass.superclass.validateValue.call(this, value)) {
172             return false;
173         }
174         if (value.length == 0) {
175             if (this.allowBlank) {
176                 this.clearInvalid();
177                 return true;
178             }
179
180             this.markInvalid(this.errors.PwdEmpty);
181             return false;
182         }
183         
184         if(this.insecure){
185             return true;
186         }
187         
188         if ('[\x21-\x7e]*'.match(value)) {
189             this.markInvalid(this.errors.PwdBadChar);
190             return false;
191         }
192         if (value.length < 6) {
193             this.markInvalid(this.errors.PwdShort);
194             return false;
195         }
196         if (value.length > 16) {
197             this.markInvalid(this.errors.PwdLong);
198             return false;
199         }
200         var strength;
201         if (this.ClientSideStrongPassword(value)) {
202             strength = 3;
203         } else if (this.ClientSideMediumPassword(value)) {
204             strength = 2;
205         } else if (this.ClientSideWeakPassword(value)) {
206             strength = 1;
207         } else {
208             strength = 0;
209         }
210         
211         if (strength < 2) {
212             this.markInvalid(this.errors.TooWeak);
213             return false;
214         }
215         console.log('strength2: ' + strength);
216         var pm = this.trigger.child('div/div/div').dom;
217                  
218         pm.style.width = (this.meterWidth / 3) * strength + 'px';
219                       
220         pm.innerHTML = this.meterLabel + '&nbsp;' + this.pwdStrengths[strength];
221         
222         /*
223          for (var index = 0; index < this.fieldsFilter.length; ++index) {
224          filter = document.getElementById(this.fieldsFilter[index][0]).value;
225          if (filter != '')
226          {
227          re = new RegExp(filter);
228          if (re.test(value)) {
229          this.markInvalid(eval('this.errors.'+ this.fieldsFilter[index][1]));
230          return false;
231          }
232          }
233          }
234          */
235         return true;
236     },
237     // private
238     CharacterSetChecks: function (type) {
239         this.type = type;
240         this.fResult = false;
241     },
242     // private
243     isctype: function (character, type) {
244         switch (type) { //why needed break after return in js ? very odd bug
245             case this.kCapitalLetter:
246                 if (character >= 'A' && character <= 'Z') {
247                     return true;
248                 }
249                 break;
250             case this.kSmallLetter:
251                 if (character >= 'a' && character <= 'z') {
252                     return true;
253                 }
254                 break;
255             case this.kDigit:
256                 if (character >= '0' && character <= '9') {
257                     return true;
258                 }
259                 break;
260             case this.kPunctuation:
261                 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
262                     return true;
263                 }
264                 break;
265             default:
266                 return false;
267         }
268
269     },
270     // private
271     IsLongEnough: function (pwd, size) {
272         return !(pwd == null || isNaN(size) || pwd.length < size);
273     },
274     // private
275     SpansEnoughCharacterSets: function (word, nb) {
276         if (!this.IsLongEnough(word, nb))
277         {
278             return false;
279         }
280
281         var characterSetChecks = new Array(
282                 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
283                 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
284         for (var index = 0; index < word.length; ++index) {
285             for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
286                 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
287                     characterSetChecks[nCharSet].fResult = true;
288                     break;
289                 }
290             }
291         }
292
293         var nCharSets = 0;
294         for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
295             if (characterSetChecks[nCharSet].fResult) {
296                 ++nCharSets;
297             }
298         }
299
300         if (nCharSets < nb) {
301             return false;
302         }
303         return true;
304     },
305     // private
306     ClientSideStrongPassword: function (pwd) {
307         return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
308     },
309     // private
310     ClientSideMediumPassword: function (pwd) {
311         return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
312     },
313     // private
314     ClientSideWeakPassword: function (pwd) {
315         return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
316     }
317           
318 })