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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. } else if (getStrong() == true) {
  29. validatePassword(password1);
  30. }
  31. });
  32. $("#new_pass_1").focus(function () {
  33. password1 = $("#new_pass_1").val();
  34. password2 = $("#new_pass_2").val();
  35. if (password1.length > 0) {
  36. checkMatching(password1, password2);
  37. }
  38. });
  39. $("#new_pass_2").keyup(function () {
  40. password2 = $("#new_pass_2").val();
  41. checkMatching(password1, password2);
  42. });
  43. $("#new_pass_1").blur(function () {
  44. password1 = $("#new_pass_1").val();
  45. password2 = $("#new_pass_2").val();
  46. if (password1.length == 0 && password2.length == 0) {
  47. enableSubmit();
  48. }
  49. });
  50. });
  51. function validatePassword(password) {
  52. if (isUserPage()) {
  53. $.ajax({
  54. type: 'POST',
  55. dataType: 'text',
  56. url: 'ajax.php?action=password_validate',
  57. data: 'password=' + password,
  58. async: false,
  59. success: function (value) {
  60. if (value == 'false') {
  61. setStatus(COMMON);
  62. }
  63. }
  64. });
  65. }
  66. }
  67. function calculateComplexity(password) {
  68. var length = password.length;
  69. var username;
  70. if (isUserPage()) {
  71. username = $(".username").text();
  72. } else {
  73. username = $("#username").val() || '';
  74. }
  75. var irckey;
  76. if (isUserPage()) {
  77. irckey = $("#irckey").val();
  78. }
  79. if (length >= 15) {
  80. setStatus(WEAK);
  81. }
  82. if (length >= 15 && isStrongPassword(password)) {
  83. setStatus(STRONG);
  84. }
  85. if (length > 0 && length < 15) {
  86. setStatus(SHORT);
  87. }
  88. if (length == 0) {
  89. setStatus(CLEAR);
  90. }
  91. if (isUserPage()) {
  92. if (irckey.length > 0) {
  93. if (password.toLowerCase() == irckey.toLowerCase()) {
  94. setStatus(MATCH_IRCKEY);
  95. }
  96. }
  97. }
  98. if (username.length > 0) {
  99. if (password.toLowerCase() == username.toLowerCase()) {
  100. setStatus(MATCH_USERNAME);
  101. }
  102. }
  103. }
  104. function isStrongPassword(password) {
  105. return /(?=^.{6,}$).*$/.test(password);
  106. }
  107. function checkMatching(password1, password2) {
  108. if (password2.length > 0) {
  109. if (password1 == password2 && getStrong() == true) {
  110. $("#pass_match").text("Passwords match").css("color", "green");
  111. enableSubmit();
  112. } else if (getStrong() == true) {
  113. $("#pass_match").text("Passwords do not match").css("color", "red");
  114. disableSubmit();
  115. } else {
  116. $("#pass_match").text("Password isn't strong").css("color", "red");
  117. disableSubmit();
  118. }
  119. } else {
  120. $("#pass_match").text("");
  121. }
  122. }
  123. function getStrong() {
  124. return $("#pass_strength").text() == "Strong";
  125. }
  126. function setStatus(strength) {
  127. if (strength == WEAK) {
  128. disableSubmit();
  129. $("#pass_strength").text("Weak").css("color", "red");
  130. }
  131. if (strength == STRONG) {
  132. disableSubmit();
  133. $("#pass_strength").text("Strong").css("color", "green");
  134. }
  135. if (strength == SHORT) {
  136. disableSubmit();
  137. $("#pass_strength").text("Too Short").css("color", "red");
  138. }
  139. if (strength == MATCH_IRCKEY) {
  140. disableSubmit();
  141. $("#pass_strength").text("Password cannot match IRC Key").css("color", "red");
  142. }
  143. if (strength == MATCH_USERNAME) {
  144. disableSubmit();
  145. $("#pass_strength").text("Password cannot match Username").css("color", "red");
  146. }
  147. if (strength == COMMON) {
  148. disableSubmit();
  149. $("#pass_strength").text("Password is too common").css("color", "red");
  150. }
  151. if (strength == CLEAR) {
  152. $("#pass_strength").text("");
  153. }
  154. }
  155. function disableSubmit() {
  156. $('input[type="submit"]').attr('disabled', 'disabled');
  157. }
  158. function enableSubmit() {
  159. $('input[type="submit"]').removeAttr('disabled');
  160. }
  161. function isUserPage() {
  162. return window.location.pathname.indexOf(USER_PATH) != -1;
  163. }
  164. })();