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