]> WPIA git - gigi.git/blob - src/club/wpia/gigi/util/PasswordStrengthChecker.java
0df62480ef3d28d5e47b65e16b323f6db73a20cc
[gigi.git] / src / club / wpia / gigi / util / PasswordStrengthChecker.java
1 package club.wpia.gigi.util;
2
3 import java.util.Arrays;
4 import java.util.TreeSet;
5 import java.util.regex.Pattern;
6
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;
11
12 public class PasswordStrengthChecker {
13
14     private static Pattern digits = Pattern.compile("\\d");
15
16     private static Pattern lower = Pattern.compile("[a-z]");
17
18     private static Pattern upper = Pattern.compile("[A-Z]");
19
20     private static Pattern whitespace = Pattern.compile("\\s");
21
22     private static Pattern special = Pattern.compile("(?!\\s)\\W");
23
24     private PasswordStrengthChecker() {}
25
26     private static int checkpwlight(String pw) {
27         int points = 0;
28         if (pw.length() > 15) {
29             points++;
30         }
31         if (pw.length() > 20) {
32             points++;
33         }
34         if (pw.length() > 25) {
35             points++;
36         }
37         if (pw.length() > 30) {
38             points++;
39         }
40         if (digits.matcher(pw).find()) {
41             points++;
42         }
43         if (lower.matcher(pw).find()) {
44             points++;
45         }
46         if (upper.matcher(pw).find()) {
47             points++;
48         }
49         if (special.matcher(pw).find()) {
50             points++;
51         }
52         if (whitespace.matcher(pw).find()) {
53             points++;
54         }
55         return points;
56     }
57
58     public static int checkpw(String pw, String[] nameParts, String email) {
59         if (pw == null) {
60             return 0;
61         }
62         int light = checkpwlight(pw);
63         if (contained(pw, email)) {
64             light -= 2;
65         }
66         for (int i = 0; i < nameParts.length; i++) {
67             if (contained(pw, nameParts[i])) {
68                 light -= 2;
69             }
70         }
71         // TODO dictionary check
72         return light;
73     }
74
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());
80             }
81         }
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>'"))));
84         }
85     }
86
87     private static boolean contained(String pw, String check) {
88         if (check == null || check.equals("")) {
89             return false;
90         }
91         if (pw.contains(check)) {
92             return true;
93         }
94         if (check.contains(pw)) {
95             return true;
96         }
97         return false;
98     }
99
100 }