]> WPIA git - gigi.git/blob - src/club/wpia/gigi/util/PasswordStrengthChecker.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / club / wpia / gigi / util / PasswordStrengthChecker.java
1 package club.wpia.gigi.util;
2
3 import java.util.TreeSet;
4 import java.util.regex.Pattern;
5
6 import club.wpia.gigi.GigiApiException;
7 import club.wpia.gigi.dbObjects.Name;
8 import club.wpia.gigi.dbObjects.NamePart;
9
10 public class PasswordStrengthChecker {
11
12     private static Pattern digits = Pattern.compile("\\d");
13
14     private static Pattern lower = Pattern.compile("[a-z]");
15
16     private static Pattern upper = Pattern.compile("[A-Z]");
17
18     private static Pattern whitespace = Pattern.compile("\\s");
19
20     private static Pattern special = Pattern.compile("(?!\\s)\\W");
21
22     private PasswordStrengthChecker() {}
23
24     private static int checkpwlight(String pw) {
25         int points = 0;
26         if (pw.length() > 15) {
27             points++;
28         }
29         if (pw.length() > 20) {
30             points++;
31         }
32         if (pw.length() > 25) {
33             points++;
34         }
35         if (pw.length() > 30) {
36             points++;
37         }
38         if (digits.matcher(pw).find()) {
39             points++;
40         }
41         if (lower.matcher(pw).find()) {
42             points++;
43         }
44         if (upper.matcher(pw).find()) {
45             points++;
46         }
47         if (special.matcher(pw).find()) {
48             points++;
49         }
50         if (whitespace.matcher(pw).find()) {
51             points++;
52         }
53         return points;
54     }
55
56     public static int checkpw(String pw, String[] nameParts, String email) {
57         if (pw == null) {
58             return 0;
59         }
60         int light = checkpwlight(pw);
61         if (contained(pw, email)) {
62             light -= 2;
63         }
64         for (int i = 0; i < nameParts.length; i++) {
65             if (contained(pw, nameParts[i])) {
66                 light -= 2;
67             }
68         }
69         // TODO dictionary check
70         return light;
71     }
72
73     public static void assertStrongPassword(String pw, Name[] names, String email) throws GigiApiException {
74         TreeSet<String> parts = new TreeSet<>();
75         for (int i = 0; i < names.length; i++) {
76             for (NamePart string : names[i].getParts()) {
77                 parts.add(string.getValue());
78             }
79         }
80         if (checkpw(pw, parts.toArray(new String[parts.size()]), email) < 3) {
81             throw new GigiApiException("The Pass Phrase you submitted failed to contain enough" + " differing characters and/or contained words from" + " your name and/or email address.");
82         }
83     }
84
85     private static boolean contained(String pw, String check) {
86         if (check == null || check.equals("")) {
87             return false;
88         }
89         if (pw.contains(check)) {
90             return true;
91         }
92         if (check.contains(pw)) {
93             return true;
94         }
95         return false;
96     }
97
98 }