Created
February 2, 2017 23:05
-
-
Save victorcarrico/5786e0fb326860b04253f3887edea4f0 to your computer and use it in GitHub Desktop.
Django password validators in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import difflib from 'difflib'; | |
// Password Validation | |
export function userAttributeSimilarityValidator(password, attributes) { | |
const DEFAULT_USER_ATTRIBUTES = ['first_name', 'last_name', 'email']; | |
const max_similarity = 0.7; | |
for (let user_attr of DEFAULT_USER_ATTRIBUTES) { | |
let value = attributes[user_attr]; | |
let value_parts = value.split(/(\W+)/).concat(value); | |
for (let value_part of value_parts){ | |
let seqMatcher = new difflib.SequenceMatcher(null, password.toLowerCase(), value_part.toLowerCase()); | |
if (seqMatcher.quickRatio() > max_similarity){ | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
export function minimumLengthValidator(password) { | |
const min_length = 8; | |
if (password.length < min_length){ | |
return false; | |
} else { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment