PageRenderTime 67ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/Appstart/assets/plugins/forms/password/jquery-passy.js

https://gitlab.com/kaouech/theme
JavaScript | 203 lines | 169 code | 32 blank | 2 comment | 24 complexity | 85e6c2c509951af0d8135f61b0669215 MD5 | raw file
  1. (function($) {
  2. var passy = {
  3. character: { DIGIT: 1, LOWERCASE: 2, UPPERCASE: 4, PUNCTUATION: 8 },
  4. strength: { LOW: 0, MEDIUM: 1, HIGH: 2, EXTREME: 3 },
  5. dictionary: [],
  6. patterns: [
  7. '0123456789',
  8. 'abcdefghijklmnopqrstuvwxyz',
  9. 'qwertyuiopasdfghjklzxcvbnm',
  10. 'azertyuiopqsdfghjklmwxcvbn',
  11. '!#$*+-.:?@^'
  12. ],
  13. threshold: {
  14. medium: 16,
  15. high: 22,
  16. extreme: 36
  17. }
  18. };
  19. passy.requirements = {
  20. characters: passy.character.DIGIT | passy.character.LOWERCASE | passy.character.UPPERCASE,
  21. length: {
  22. min: 6,
  23. max: Infinity
  24. }
  25. };
  26. if(Object.seal) {
  27. Object.seal(passy.character);
  28. Object.seal(passy.strength);
  29. }
  30. if(Object.freeze) {
  31. Object.freeze(passy.character);
  32. Object.freeze(passy.strength);
  33. }
  34. passy.analize = function(password) {
  35. var score = Math.floor(password.length * 2);
  36. var i = password.length;
  37. score += $.passy.analizePatterns(password);
  38. score += $.passy.analizeDictionary(password);
  39. while(i--) score += $.passy.analizeCharacter(password.charAt(i));
  40. return $.passy.analizeScore(score);
  41. };
  42. passy.analizeCharacter = function(char) {
  43. var code = char.charCodeAt(0);
  44. if(code >= 97 && code <= 122) return 1; // lower case
  45. if(code >= 48 && code <= 57) return 2; // numeric
  46. if(code >= 65 && code <= 90) return 3; // capital
  47. if(code <= 126) return 4; // punctuation
  48. return 5; // foreign characters etc
  49. };
  50. passy.analizePattern = function(password, pattern) {
  51. var lastmatch = -1;
  52. var score = -2;
  53. for(var i = 0; i < password.length; i++) {
  54. var match = pattern.indexOf(password.charAt(i));
  55. if(lastmatch === match - 1) {
  56. lastmatch = match;
  57. score++;
  58. }
  59. }
  60. return Math.max(0, score);
  61. };
  62. passy.analizePatterns = function(password) {
  63. var chars = password.toLowerCase();
  64. var score = 0;
  65. for(var i in $.passy.patterns) {
  66. var pattern = $.passy.patterns[i].toLowerCase();
  67. score += $.passy.analizePattern(chars, pattern);
  68. }
  69. // patterns are bad man!
  70. return score * -5;
  71. };
  72. passy.analizeDictionary = function(password) {
  73. var chars = password.toLowerCase();
  74. var score = 0;
  75. for(var i in $.passy.dictionary) {
  76. var word = $.passy.dictionary[i].toLowerCase();
  77. if(password.indexOf(word) >= 0) score++;
  78. }
  79. // using words are bad too!
  80. return score * -5;
  81. };
  82. passy.analizeScore = function(score) {
  83. if(score >= $.passy.threshold.extreme) return $.passy.strength.EXTREME;
  84. if(score >= $.passy.threshold.high) return $.passy.strength.HIGH;
  85. if(score >= $.passy.threshold.medium) return $.passy.strength.MEDIUM;
  86. return $.passy.strength.LOW;
  87. };
  88. passy.generate = function(len) {
  89. var chars = [
  90. '0123456789',
  91. 'abcdefghijklmnopqrstuvwxyz',
  92. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  93. '!#$&()*+<=>@[]^'
  94. ];
  95. var password = [];
  96. var type, index;
  97. len = Math.max(len, $.passy.requirements.length.min);
  98. len = Math.min(len, $.passy.requirements.length.max);
  99. while(len--) {
  100. type = len % chars.length;
  101. index = Math.floor(Math.random() * chars[type].length);
  102. password.push(chars[type].charAt(index));
  103. }
  104. password.sort(function() {
  105. return Math.random() * 2 - 1;
  106. });
  107. return password.join('');
  108. };
  109. passy.contains = function(str, char) {
  110. if(char === $.passy.character.DIGIT) {
  111. return /\d/.test(str);
  112. } else if(char === $.passy.character.LOWERCASE) {
  113. return /[a-z]/.test(str);
  114. } else if(char === $.passy.character.UPPERCASE) {
  115. return /[A-Z]/.test(str);
  116. } else if(char === $.passy.character.PUNCTUATION) {
  117. return /[!"#$%&'()*+,\-./:;<=>?@[\\]\^_`{\|}~]/.test(str);
  118. }
  119. };
  120. passy.valid = function(str) {
  121. var valid = true;
  122. if(!$.passy.requirements) return true;
  123. if(str.length < $.passy.requirements.length.min) return false;
  124. if(str.length > $.passy.requirements.length.max) return false;
  125. for(var i in $.passy.character) {
  126. if($.passy.requirements.characters & $.passy.character[i]) {
  127. valid = $.passy.contains(str, $.passy.character[i]) && valid;
  128. }
  129. }
  130. return valid;
  131. };
  132. var methods = {
  133. init: function(callback) {
  134. var $this = $(this);
  135. $this.on('change keyup', function() {
  136. if(typeof callback !== 'function') return;
  137. var value = $this.val();
  138. callback.call($this, $.passy.analize(value), methods.valid.call($this));
  139. });
  140. },
  141. generate: function(len) {
  142. this.val($.passy.generate(len));
  143. this.change();
  144. },
  145. valid: function() {
  146. return $.passy.valid(this.val());
  147. }
  148. };
  149. $.fn.passy = function(opt) {
  150. if(methods[opt]) {
  151. return methods[opt].apply(this, Array.prototype.slice.call(arguments, 1));
  152. } else if (typeof opt === 'function' || !opt) {
  153. return methods.init.apply(this, arguments);
  154. }
  155. return this;
  156. };
  157. $.extend({ passy: passy });
  158. })(jQuery);