Gazelle/static/functions/password_validate.js

197 lines
4.0 KiB
JavaScript
Raw Normal View History

2012-06-17 08:00:18 +00:00
/**
*
* Validates passwords to make sure they are powerful
**/
(function($) {
var CLEAR = 0;
var WEAK = 1;
var STRONG = 3;
var SHORT = 4;
var MATCH_IRCKEY = 5;
var MATCH_USERNAME = 6;
var COMMON = 7;
var USER_PATH = "/user.php";
$(document).ready(function() {
var old = $("#new_pass_1").val().length;
var password1;
var password2;
$("#new_pass_1").keyup(function() {
password1 = $("#new_pass_1").val();
2013-04-18 08:00:54 +00:00
if (password1.length != old) {
2012-06-17 08:00:18 +00:00
disableSubmit();
calculateComplexity(password1);
old = password1.length;
}
});
$("#new_pass_1").change(function() {
password1 = $("#new_pass_1").val();
2013-04-18 08:00:54 +00:00
password2 = $("#new_pass_2").val();
2012-06-17 08:00:18 +00:00
2013-04-18 08:00:54 +00:00
if (password1.length == 0 && password2.length == 0) {
2012-06-17 08:00:18 +00:00
enableSubmit();
2013-04-18 08:00:54 +00:00
} else if (getStrong() == true) {
2012-06-17 08:00:18 +00:00
validatePassword(password1);
2013-04-18 08:00:54 +00:00
}
2012-06-17 08:00:18 +00:00
});
$("#new_pass_1").focus(function() {
password1 = $("#new_pass_1").val();
password2 = $("#new_pass_2").val();
2013-04-18 08:00:54 +00:00
if (password1.length > 0) {
2012-06-17 08:00:18 +00:00
checkMatching(password1, password2);
}
});
$("#new_pass_2").keyup(function() {
2013-04-18 08:00:54 +00:00
password2 = $("#new_pass_2").val();
2012-06-17 08:00:18 +00:00
checkMatching(password1, password2);
});
$("#new_pass_1").blur(function() {
2013-04-18 08:00:54 +00:00
password1 = $("#new_pass_1").val();
password2 = $("#new_pass_2").val();
if (password1.length == 0 && password2.length == 0) {
enableSubmit();
}
2012-06-17 08:00:18 +00:00
});
});
function validatePassword(password) {
2013-04-18 08:00:54 +00:00
if (isUserPage()) {
2012-06-17 08:00:18 +00:00
$.ajax({
2013-04-18 08:00:54 +00:00
type: 'POST',
dataType: 'text',
2012-06-17 08:00:18 +00:00
url : 'ajax.php?action=password_validate',
data: 'password=' + password,
async: false,
2013-04-18 08:00:54 +00:00
success: function(value) {
if (value == 'false') {
2012-06-17 08:00:18 +00:00
setStatus(COMMON);
}
}
});
}
}
function calculateComplexity(password) {
var length = password.length;
var username;
2013-04-18 08:00:54 +00:00
if (isUserPage()) {
2012-06-17 08:00:18 +00:00
username = $(".username").text();
}
else {
2012-06-18 08:00:14 +00:00
username = $("#username").val() || '';
2012-06-17 08:00:18 +00:00
}
var irckey;
2013-04-18 08:00:54 +00:00
if (isUserPage()) {
2012-06-17 08:00:18 +00:00
irckey = $("#irckey").val();
}
2013-04-18 08:00:54 +00:00
if (length >= 8) {
2012-06-17 08:00:18 +00:00
setStatus(WEAK);
}
2013-04-18 08:00:54 +00:00
if (length >= 8 && isStrongPassword(password)) {
2012-06-17 08:00:18 +00:00
setStatus(STRONG);
}
2013-04-18 08:00:54 +00:00
if (length > 0 && length < 8) {
2012-06-17 08:00:18 +00:00
setStatus(SHORT);
}
2013-04-18 08:00:54 +00:00
if (length == 0) {
2012-06-17 08:00:18 +00:00
setStatus(CLEAR);
}
2013-04-18 08:00:54 +00:00
if (isUserPage()) {
if (irckey.length > 0){
if (password.toLowerCase() == irckey.toLowerCase()) {
2012-06-17 08:00:18 +00:00
setStatus(MATCH_IRCKEY);
2013-04-18 08:00:54 +00:00
}
2012-06-17 08:00:18 +00:00
}
}
2013-04-18 08:00:54 +00:00
if (username.length > 0) {
if (password.toLowerCase() == username.toLowerCase()) {
2012-06-17 08:00:18 +00:00
setStatus(MATCH_USERNAME);
2013-04-18 08:00:54 +00:00
}
2012-06-17 08:00:18 +00:00
}
}
function isStrongPassword(password) {
2013-03-16 08:00:25 +00:00
return /(?=^.{8,}$)(?=.*[^a-zA-Z])(?=.*[A-Z])(?=.*[a-z]).*$/.test(password);
2012-06-17 08:00:18 +00:00
}
function checkMatching(password1, password2) {
2013-04-18 08:00:54 +00:00
if (password2.length > 0) {
if (password1 == password2 && getStrong() == true) {
$("#pass_match").text("Passwords match").css("color", "green");
enableSubmit();
} else if (getStrong() == true) {
$("#pass_match").text("Passwords do not match").css("color", "red");
disableSubmit();
} else {
$("#pass_match").text("Password isn't strong").css("color", "red");
disableSubmit();
}
} else {
2012-06-17 08:00:18 +00:00
$("#pass_match").text("");
}
}
function getStrong() {
return $("#pass_strength").text() == "Strong";
}
function setStatus(strength) {
2013-04-18 08:00:54 +00:00
if (strength == WEAK) {
2012-06-17 08:00:18 +00:00
disableSubmit();
$("#pass_strength").text("Weak").css("color", "red");
}
2013-04-18 08:00:54 +00:00
if (strength == STRONG) {
2012-06-17 08:00:18 +00:00
disableSubmit();
$("#pass_strength").text("Strong").css("color", "green");
}
2013-04-18 08:00:54 +00:00
if (strength == SHORT) {
2012-06-17 08:00:18 +00:00
disableSubmit();
$("#pass_strength").text("Too Short").css("color", "red");
}
2013-04-18 08:00:54 +00:00
if (strength == MATCH_IRCKEY) {
2012-06-17 08:00:18 +00:00
disableSubmit();
$("#pass_strength").text("Password cannot match IRC Key").css("color", "red");
}
2013-04-18 08:00:54 +00:00
if (strength == MATCH_USERNAME) {
2012-06-17 08:00:18 +00:00
disableSubmit();
$("#pass_strength").text("Password cannot match Username").css("color", "red");
}
2013-04-18 08:00:54 +00:00
if (strength == COMMON) {
2012-06-17 08:00:18 +00:00
disableSubmit();
$("#pass_strength").text("Password is too common").css("color", "red");
}
2013-04-18 08:00:54 +00:00
if (strength == CLEAR) {
2012-06-17 08:00:18 +00:00
$("#pass_strength").text("");
}
}
function disableSubmit() {
2013-04-18 08:00:54 +00:00
$('input[type="submit"]').attr('disabled','disabled');
2012-06-17 08:00:18 +00:00
}
function enableSubmit() {
2013-04-18 08:00:54 +00:00
$('input[type="submit"]').removeAttr('disabled');
2012-06-17 08:00:18 +00:00
}
function isUserPage() {
return window.location.pathname.indexOf(USER_PATH) != -1;
}
} ) ( jQuery );