]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/main/Signup.java
chg: move PasswordChecker object to Gigi class
[gigi.git] / src / club / wpia / gigi / pages / main / Signup.java
1 package club.wpia.gigi.pages.main;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10
11 import club.wpia.gigi.Gigi;
12 import club.wpia.gigi.GigiApiException;
13 import club.wpia.gigi.database.GigiPreparedStatement;
14 import club.wpia.gigi.database.GigiResultSet;
15 import club.wpia.gigi.dbObjects.User;
16 import club.wpia.gigi.email.EmailProvider;
17 import club.wpia.gigi.localisation.Language;
18 import club.wpia.gigi.output.CountrySelector;
19 import club.wpia.gigi.output.DateSelector;
20 import club.wpia.gigi.output.NameInput;
21 import club.wpia.gigi.output.template.Form;
22 import club.wpia.gigi.output.template.PlainOutputable;
23 import club.wpia.gigi.output.template.SprintfCommand;
24 import club.wpia.gigi.output.template.Template;
25 import club.wpia.gigi.output.template.TranslateCommand;
26 import club.wpia.gigi.pages.Page;
27 import club.wpia.gigi.passwords.PasswordStrengthChecker;
28 import club.wpia.gigi.util.CalendarUtil;
29 import club.wpia.gigi.util.HTMLEncoder;
30 import club.wpia.gigi.util.Notary;
31 import club.wpia.gigi.util.RateLimit.RateLimitException;
32
33 public class Signup extends Form {
34
35     private NameInput ni;
36
37     private String email = "";
38
39     private static final Template t = new Template(Signup.class.getResource("Signup.templ"));
40
41     private CountrySelector cs;
42
43     public Signup(HttpServletRequest hsr) {
44         super(hsr);
45         ni = new NameInput();
46         cs = new CountrySelector("residenceCountry", true);
47     }
48
49     private DateSelector myDoB = new DateSelector("day", "month", "year");
50
51     @Override
52     public void outputContent(PrintWriter out, Language l, Map<String, Object> outerVars) {
53         HashMap<String, Object> vars = new HashMap<String, Object>(outerVars);
54         vars.put("name", ni);
55         vars.put("dob", myDoB);
56         vars.put("email", HTMLEncoder.encodeHTML(email));
57         vars.put("helpOnNames", new SprintfCommand("Help on Names {0}in the knowledge base{1}", Arrays.asList("!(/kb/names", "!'</a>")));
58         vars.put("csrf", getCSRFToken());
59         vars.put("dobmin", User.MINIMUM_AGE + "");
60         vars.put("countryCode", cs);
61         t.output(out, l, vars);
62     }
63
64     private void update(HttpServletRequest r) throws GigiApiException {
65         if (r.getParameter("email") != null) {
66             email = r.getParameter("email");
67         }
68         GigiApiException problems = new GigiApiException();
69         try {
70             ni.update(r);
71         } catch (GigiApiException e) {
72             problems.mergeInto(e);
73         }
74         try {
75             myDoB.update(r);
76         } catch (GigiApiException e) {
77             problems.mergeInto(e);
78         }
79
80         cs.update(r);
81
82         if ( !problems.isEmpty()) {
83             throw problems;
84         }
85
86     }
87
88     @Override
89     public synchronized SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
90         if (RegisterPage.RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
91             throw new RateLimitException();
92         }
93
94         GigiApiException ga = new GigiApiException();
95         try {
96             update(req);
97         } catch (GigiApiException e) {
98             ga.mergeInto(e);
99         }
100         try {
101             ni.getNameParts();
102         } catch (GigiApiException e) {
103             ga.mergeInto(e);
104         }
105
106         if ( !myDoB.isValid()) {
107             ga.mergeInto(new GigiApiException("Invalid date of birth"));
108         }
109
110         if ( !CalendarUtil.isOfAge(myDoB.getDate(), User.MINIMUM_AGE)) {
111             ga.mergeInto(new GigiApiException("Entered date of birth is below the restricted age requirements."));
112         }
113
114         if (CalendarUtil.isYearsInFuture(myDoB.getDate().end(), User.MAXIMUM_PLAUSIBLE_AGE)) {
115             ga.mergeInto(new GigiApiException("Entered date of birth exceeds the maximum age set in our policies. Please check your DoB is correct and contact support if the issue persists."));
116         }
117
118         if ( !"1".equals(req.getParameter("tos_agree"))) {
119             ga.mergeInto(new GigiApiException("Acceptance of the ToS is required to continue."));
120         }
121         if (email.equals("")) {
122             ga.mergeInto(new GigiApiException("Email Address was blank"));
123         }
124         String pw1 = req.getParameter("pword1");
125         String pw2 = req.getParameter("pword2");
126         if (pw1 == null || pw1.equals("")) {
127             ga.mergeInto(new GigiApiException("Passwords were blank"));
128         } else if ( !pw1.equals(pw2)) {
129             ga.mergeInto(new GigiApiException("Passwords don't match"));
130         }
131         if ( !ga.isEmpty()) {
132             throw ga;
133         }
134         GigiApiException gaPassword = Gigi.getPasswordChecker().checkPassword(pw1, ni.getNamePartsPlain(), email);
135         if (gaPassword != null) {
136             throw gaPassword;
137         }
138         GigiApiException ga2 = new GigiApiException();
139         try (GigiPreparedStatement q1 = new GigiPreparedStatement("SELECT * FROM `emails` WHERE `email`=? AND `deleted` IS NULL"); GigiPreparedStatement q2 = new GigiPreparedStatement("SELECT * FROM `certOwners` INNER JOIN `users` ON `users`.`id`=`certOwners`.`id` WHERE `email`=? AND `deleted` IS NULL")) {
140             q1.setString(1, email);
141             q2.setString(1, email);
142             GigiResultSet r1 = q1.executeQuery();
143             GigiResultSet r2 = q2.executeQuery();
144             if (r1.next() || r2.next()) {
145                 ga2.mergeInto(new GigiApiException("This email address is currently valid in the system."));
146             }
147         }
148         try (GigiPreparedStatement q3 = new GigiPreparedStatement("SELECT `domain` FROM `baddomains` WHERE `domain`=RIGHT(?, LENGTH(`domain`))")) {
149             q3.setString(1, email);
150
151             GigiResultSet r3 = q3.executeQuery();
152             if (r3.next()) {
153                 String domain = r3.getString(1);
154                 ga2.mergeInto(new GigiApiException(SprintfCommand.createSimple("We don't allow signups from people using email addresses from {0}.", domain)));
155             }
156         }
157         String mailResult = EmailProvider.FAIL;
158         try {
159             mailResult = HTMLEncoder.encodeHTML(EmailProvider.getInstance().checkEmailServer(0, email));
160         } catch (IOException e) {
161         }
162         if ( !mailResult.equals(EmailProvider.OK)) {
163             if (mailResult.startsWith("4")) {
164                 ga2.mergeInto(new GigiApiException("The mail server responsible for your domain indicated" + " a temporary failure. This may be due to anti-SPAM measures, such" + " as greylisting. Please try again in a few minutes."));
165             } else {
166                 ga2.mergeInto(new GigiApiException("Email Address given was invalid, or a test connection" + " couldn't be made to your server, or the server" + " rejected the email address as invalid"));
167             }
168             if (mailResult.equals(EmailProvider.FAIL)) {
169                 ga2.mergeInto(new GigiApiException("Failed to make a connection to the mail server"));
170             } else {
171                 ga2.mergeInto(new GigiApiException(new PlainOutputable(mailResult)));
172             }
173         }
174
175         if ( !ga2.isEmpty()) {
176             throw ga2;
177         }
178         run(req, pw1);
179         return new SuccessMessageResult(new TranslateCommand("Your information has been submitted" + " into our system. You will now be sent an email with a web link," + " you need to open that link in your web browser within 24 hours" + " or your information will be removed from our system!"));
180     }
181
182     private void run(HttpServletRequest req, String password) throws GigiApiException {
183         User u = new User(email, password, myDoB.getDate(), Page.getLanguage(req).getLocale(), cs.getCountry(), ni.getNameParts());
184         Notary.writeUserAgreement(u, "ToS", "account creation", "", true, 0);
185     }
186
187 }