1 package club.wpia.gigi.util;
3 import java.util.Arrays;
4 import java.util.TreeSet;
5 import java.util.regex.Pattern;
7 import club.wpia.gigi.GigiApiException;
8 import club.wpia.gigi.dbObjects.Name;
9 import club.wpia.gigi.dbObjects.NamePart;
10 import club.wpia.gigi.output.template.SprintfCommand;
12 public class PasswordStrengthChecker {
14 private static Pattern digits = Pattern.compile("\\d");
16 private static Pattern lower = Pattern.compile("[a-z]");
18 private static Pattern upper = Pattern.compile("[A-Z]");
20 private static Pattern whitespace = Pattern.compile("\\s");
22 private static Pattern special = Pattern.compile("(?!\\s)\\W");
24 private PasswordStrengthChecker() {}
26 private static int checkpwlight(String pw) {
28 if (pw.length() > 15) {
31 if (pw.length() > 20) {
34 if (pw.length() > 25) {
37 if (pw.length() > 30) {
40 if (digits.matcher(pw).find()) {
43 if (lower.matcher(pw).find()) {
46 if (upper.matcher(pw).find()) {
49 if (special.matcher(pw).find()) {
52 if (whitespace.matcher(pw).find()) {
58 public static int checkpw(String pw, String[] nameParts, String email) {
62 int light = checkpwlight(pw);
63 if (contained(pw, email)) {
66 for (int i = 0; i < nameParts.length; i++) {
67 if (contained(pw, nameParts[i])) {
71 // TODO dictionary check
75 public static void assertStrongPassword(String pw, Name[] names, String email) throws GigiApiException {
76 TreeSet<String> parts = new TreeSet<>();
77 for (int i = 0; i < names.length; i++) {
78 for (NamePart string : names[i].getParts()) {
79 parts.add(string.getValue());
82 if (checkpw(pw, parts.toArray(new String[parts.size()]), email) < 3) {
83 throw (new GigiApiException(new SprintfCommand("The Pass Phrase you submitted failed to contain enough differing characters and/or contained words from your name and/or email address. For the current requirements and to learn more, visit our {0}FAQ{1}.", Arrays.asList("!(/wiki/goodPassword", "!'</a>'"))));
87 private static boolean contained(String pw, String check) {
88 if (check == null || check.equals("")) {
91 if (pw.contains(check)) {
94 if (check.contains(pw)) {