BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

password_validate.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Validates passwords to make sure they are powerful
  2. (function () {
  3. var CLEAR = 0;
  4. var WEAK = 1;
  5. var STRONG = 3;
  6. var SHORT = 4;
  7. var MATCH_IRCKEY = 5;
  8. var MATCH_USERNAME = 6;
  9. var COMMON = 7;
  10. var USER_PATH = "/user.php";
  11. $(document).ready(function () {
  12. var old = $("#new_pass_1").val().length;
  13. var password1;
  14. var password2;
  15. $("#new_pass_1").keyup(function () {
  16. password1 = $("#new_pass_1").val();
  17. if (password1.length != old) {
  18. disableSubmit();
  19. calculateComplexity(password1);
  20. old = password1.length;
  21. }
  22. });
  23. $("#new_pass_1").change(function () {
  24. password1 = $("#new_pass_1").val();
  25. password2 = $("#new_pass_2").val();
  26. if (password1.length == 0 && password2.length == 0) {
  27. enableSubmit();
  28. }
  29. });
  30. $("#new_pass_1").focus(function () {
  31. password1 = $("#new_pass_1").val();
  32. password2 = $("#new_pass_2").val();
  33. if (password1.length > 0) {
  34. checkMatching(password1, password2);
  35. }
  36. });
  37. $("#new_pass_2").keyup(function () {
  38. password2 = $("#new_pass_2").val();
  39. checkMatching(password1, password2);
  40. });
  41. $("#new_pass_1").blur(function () {
  42. password1 = $("#new_pass_1").val();
  43. password2 = $("#new_pass_2").val();
  44. if (password1.length == 0 && password2.length == 0) {
  45. enableSubmit();
  46. }
  47. });
  48. });
  49. /**
  50. * calculateComplexity
  51. */
  52. function calculateComplexity(password) {
  53. var length = password.length;
  54. var username;
  55. if (isUserPage()) {
  56. username = $(".username").text();
  57. } else {
  58. username = $("#username").val() || '';
  59. }
  60. var irckey;
  61. if (isUserPage()) {
  62. irckey = $("#irckey").val();
  63. }
  64. if (length >= 15) {
  65. setStatus(WEAK);
  66. }
  67. if (length >= 15 && isStrongPassword(password)) {
  68. setStatus(STRONG);
  69. }
  70. if (length > 0 && length < 15) {
  71. setStatus(SHORT);
  72. }
  73. if (length == 0) {
  74. setStatus(CLEAR);
  75. }
  76. if (isUserPage()) {
  77. if (irckey.length > 0) {
  78. if (password.toLowerCase() == irckey.toLowerCase()) {
  79. setStatus(MATCH_IRCKEY);
  80. }
  81. }
  82. }
  83. if (username.length > 0) {
  84. if (password.toLowerCase() == username.toLowerCase()) {
  85. setStatus(MATCH_USERNAME);
  86. }
  87. }
  88. }
  89. /**
  90. * isStrongPassword
  91. *
  92. * $ENV-PW_MIN is still harcoded here.
  93. */
  94. function isStrongPassword(password) {
  95. return /(?=^.{15,}$).*$/.test(password);
  96. }
  97. /**
  98. * checkMatching
  99. */
  100. function checkMatching(password1, password2) {
  101. if (password2.length > 0) {
  102. if (password1 == password2 && getStrong() == true) {
  103. $("#pass_match").text("Passwords match").css("color", "green");
  104. enableSubmit();
  105. } else if (getStrong() == true) {
  106. $("#pass_match").text("Passwords do not match").css("color", "red");
  107. disableSubmit();
  108. } else {
  109. $("#pass_match").text("Password isn't strong").css("color", "red");
  110. disableSubmit();
  111. }
  112. } else {
  113. $("#pass_match").text("");
  114. }
  115. }
  116. /**
  117. * getStrong
  118. */
  119. function getStrong() {
  120. return $("#pass_strength").text() == "Strong";
  121. }
  122. /**
  123. * setStatus
  124. */
  125. function setStatus(strength) {
  126. if (strength == WEAK) {
  127. disableSubmit();
  128. $("#pass_strength").text("Weak").css("color", "red");
  129. }
  130. if (strength == STRONG) {
  131. disableSubmit();
  132. $("#pass_strength").text("Strong").css("color", "green");
  133. }
  134. if (strength == SHORT) {
  135. disableSubmit();
  136. $("#pass_strength").text("Too Short").css("color", "red");
  137. }
  138. if (strength == MATCH_IRCKEY) {
  139. disableSubmit();
  140. $("#pass_strength").text("Password cannot match IRC Key").css("color", "red");
  141. }
  142. if (strength == MATCH_USERNAME) {
  143. disableSubmit();
  144. $("#pass_strength").text("Password cannot match Username").css("color", "red");
  145. }
  146. if (strength == COMMON) {
  147. disableSubmit();
  148. $("#pass_strength").text("Password is too common").css("color", "red");
  149. }
  150. if (strength == CLEAR) {
  151. $("#pass_strength").text("");
  152. }
  153. }
  154. /**
  155. * disableSubmit
  156. */
  157. function disableSubmit() {
  158. $('input[type="submit"]').attr('disabled', 'disabled');
  159. }
  160. /**
  161. * enableSubmit
  162. */
  163. function enableSubmit() {
  164. $('input[type="submit"]').removeAttr('disabled');
  165. }
  166. /**
  167. * isUserPage
  168. */
  169. function isUserPage() {
  170. return window.location.pathname.indexOf(USER_PATH) != -1;
  171. }
  172. })();