Merge "add: implement to define a strong authenticated login"
[gigi.git] / util-testing / club / wpia / gigi / pages / Manager.java
1 package club.wpia.gigi.pages;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.lang.reflect.Field;
6 import java.security.GeneralSecurityException;
7 import java.security.KeyPair;
8 import java.security.KeyPairGenerator;
9 import java.security.Signature;
10 import java.text.SimpleDateFormat;
11 import java.util.Base64;
12 import java.util.Calendar;
13 import java.util.Date;
14 import java.util.GregorianCalendar;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.Locale;
20 import java.util.Map;
21 import java.util.Properties;
22 import java.util.Random;
23 import java.util.TreeSet;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import club.wpia.gigi.Gigi;
31 import club.wpia.gigi.GigiApiException;
32 import club.wpia.gigi.crypto.SPKAC;
33 import club.wpia.gigi.database.GigiPreparedStatement;
34 import club.wpia.gigi.dbObjects.CATS;
35 import club.wpia.gigi.dbObjects.CATS.CATSType;
36 import club.wpia.gigi.dbObjects.Certificate;
37 import club.wpia.gigi.dbObjects.Certificate.CertificateStatus;
38 import club.wpia.gigi.dbObjects.CertificateOwner;
39 import club.wpia.gigi.dbObjects.Country;
40 import club.wpia.gigi.dbObjects.Digest;
41 import club.wpia.gigi.dbObjects.Domain;
42 import club.wpia.gigi.dbObjects.DomainPingConfiguration;
43 import club.wpia.gigi.dbObjects.DomainPingExecution;
44 import club.wpia.gigi.dbObjects.DomainPingType;
45 import club.wpia.gigi.dbObjects.EmailAddress;
46 import club.wpia.gigi.dbObjects.Group;
47 import club.wpia.gigi.dbObjects.NamePart;
48 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
49 import club.wpia.gigi.dbObjects.User;
50 import club.wpia.gigi.dbObjects.Verification.VerificationType;
51 import club.wpia.gigi.email.DelegateMailProvider;
52 import club.wpia.gigi.email.EmailProvider;
53 import club.wpia.gigi.localisation.Language;
54 import club.wpia.gigi.output.template.IterableDataset;
55 import club.wpia.gigi.output.template.Template;
56 import club.wpia.gigi.pages.account.certs.CertificateRequest;
57 import club.wpia.gigi.ping.DomainPinger;
58 import club.wpia.gigi.ping.PingerDaemon;
59 import club.wpia.gigi.util.AuthorizationContext;
60 import club.wpia.gigi.util.DayDate;
61 import club.wpia.gigi.util.DomainAssessment;
62 import club.wpia.gigi.util.HTMLEncoder;
63 import club.wpia.gigi.util.Notary;
64 import club.wpia.gigi.util.TimeConditions;
65 import sun.security.x509.X509Key;
66
67 public class Manager extends Page {
68
69     public static String validVerificationDateString() {
70         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
71         Calendar c = Calendar.getInstance();
72         c.setTimeInMillis(System.currentTimeMillis());
73         c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION + 1);
74         return sdf.format(new Date(c.getTimeInMillis()));
75     }
76
77     public static Country getRandomCountry() {
78         List<Country> cc = Country.getCountries();
79         int rnd = new Random().nextInt(cc.size());
80         return cc.get(rnd);
81     }
82
83     public static final String PATH = "/manager";
84
85     private static HashMap<DomainPingType, DomainPinger> dps;
86
87     private Manager() {
88         super("Test Manager");
89
90         try {
91             Field gigiInstance = Gigi.class.getDeclaredField("instance");
92             gigiInstance.setAccessible(true);
93             Gigi g = (Gigi) gigiInstance.get(null);
94
95             Field gigiPinger = Gigi.class.getDeclaredField("pinger");
96             gigiPinger.setAccessible(true);
97             PingerDaemon pd = (PingerDaemon) gigiPinger.get(g);
98
99             Field f = PingerDaemon.class.getDeclaredField("pingers");
100             f.setAccessible(true);
101             dps = (HashMap<DomainPingType, DomainPinger>) f.get(pd);
102             HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
103             for (DomainPingType dpt : DomainPingType.values()) {
104                 pingers.put(dpt, new PingerFetcher(dpt));
105             }
106             f.set(pd, pingers);
107         } catch (ReflectiveOperationException e) {
108             e.printStackTrace();
109         }
110     }
111
112     public User getSupporter() {
113         if (supporter != null) {
114             return supporter;
115         }
116         try {
117             User u = createAgent( -1);
118             if ( !u.isInGroup(Group.SUPPORTER)) {
119                 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
120                     ps.setInt(1, u.getId());
121                     ps.setString(2, Group.SUPPORTER.getDBName());
122                     ps.setInt(3, u.getId());
123                     ps.execute();
124                 }
125                 u.refreshGroups();
126             }
127             supporter = u;
128         } catch (ReflectiveOperationException | GigiApiException e) {
129             e.printStackTrace();
130         }
131         return supporter;
132     }
133
134     public User getAgent(int i) {
135         if (agents[i] != null) {
136             return agents[i];
137         }
138         try {
139             User u = createAgent(i);
140             agents[i] = u;
141
142         } catch (ReflectiveOperationException | GigiApiException e) {
143             e.printStackTrace();
144         }
145         return agents[i];
146     }
147
148     private User createAgent(int i) throws GigiApiException, IllegalAccessException {
149         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `country`=?")) {
150             String mail = "test-agent" + i + "@example.com";
151             User u = User.getByEmail(mail);
152             if (u == null) {
153                 System.out.println("Creating RA-Agent");
154                 createUser(mail);
155                 u = User.getByEmail(mail);
156                 passCATS(u, CATSType.AGENT_CHALLENGE);
157                 ps.setInt(1, u.getId());
158                 ps.setInt(2, u.getPreferredName().getId());
159                 ps.setInt(3, 100);
160                 ps.setString(4, "Manager init code");
161                 ps.setString(5, "1990-01-01");
162                 ps.setString(6, getRandomCountry().getCode());
163                 ps.execute();
164             }
165             return u;
166         }
167     }
168
169     private void passCATS(User u, CATSType t) {
170         CATS.enterResult(u, t, new Date(System.currentTimeMillis()), "en_EN", "1");
171     }
172
173     private static Manager instance;
174
175     private static final Template t = new Template(Manager.class.getResource("ManagerMails.templ"));
176
177     HashMap<String, LinkedList<String>> emails = new HashMap<>();
178
179     private static TreeSet<String> pingExempt = new TreeSet<>();
180
181     public static Manager getInstance() {
182         if (instance == null) {
183             instance = new Manager();
184         }
185         return instance;
186     }
187
188     public static class MailFetcher extends DelegateMailProvider {
189
190         Pattern[] toForward;
191
192         public MailFetcher(Properties props) {
193             super(props, props.getProperty("emailProvider.manager.target"));
194             String str = props.getProperty("emailProvider.manager.filter");
195             if (str == null) {
196                 toForward = new Pattern[0];
197             } else {
198                 String[] parts = str.split(" ");
199                 toForward = new Pattern[parts.length];
200                 for (int i = 0; i < parts.length; i++) {
201                     toForward[i] = Pattern.compile(parts[i]);
202                 }
203             }
204         }
205
206         @Override
207         public String checkEmailServer(int forUid, String address) throws IOException {
208             return OK;
209         }
210
211         @Override
212         public synchronized void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
213             HashMap<String, LinkedList<String>> mails = Manager.getInstance().emails;
214             LinkedList<String> hismails = mails.get(to);
215             if (hismails == null) {
216                 mails.put(to, hismails = new LinkedList<>());
217             }
218             hismails.addFirst(subject + "\n" + message);
219             for (int i = 0; i < toForward.length; i++) {
220                 if (toForward[i].matcher(to).matches()) {
221                     super.sendMail(to, subject, message, replyto, toname, fromname, errorsto, extra);
222                     return;
223                 }
224             }
225         }
226
227     }
228
229     public class PingerFetcher extends DomainPinger {
230
231         private DomainPingType dpt;
232
233         public PingerFetcher(DomainPingType dpt) {
234             this.dpt = dpt;
235         }
236
237         @Override
238         public DomainPingExecution ping(Domain domain, String configuration, CertificateOwner target, DomainPingConfiguration conf) {
239             System.err.println("TestManager: " + domain.getSuffix());
240             if (pingExempt.contains(domain.getSuffix())) {
241                 return enterPingResult(conf, DomainPinger.PING_SUCCEDED, "Succeeded by TestManager pass-by", null);
242             } else {
243                 DomainPinger pinger = dps.get(dpt);
244                 System.err.println("Forward to old pinger: " + pinger);
245                 return pinger.ping(domain, configuration, target, conf);
246             }
247         }
248
249     }
250
251     public void batchCreateUsers(String mailPrefix, String domain, int amount, PrintWriter out) {
252
253         try {
254             if (amount > 100) {
255                 out.print("100 at most, please.");
256                 return;
257             }
258             for (int i = 0; i < amount; i++) {
259                 String email = mailPrefix + i + "@" + domain;
260                 createUser(email);
261             }
262         } catch (ReflectiveOperationException e) {
263             out.println("failed");
264             e.printStackTrace();
265         } catch (GigiApiException e) {
266             out.println("failed: " + e.getMessage());
267             e.printStackTrace();
268         }
269     }
270
271     private void createUser(String email) throws GigiApiException, IllegalAccessException {
272         Calendar gc = GregorianCalendar.getInstance();
273         gc.setTimeInMillis(0);
274         gc.set(1990, 0, 1);
275
276         Country country = getRandomCountry();
277
278         User u = new User(email, "xvXV12°§", new DayDate(gc.getTime().getTime()), Locale.ENGLISH, country, //
279                 new NamePart(NamePartType.FIRST_NAME, "Först"), new NamePart(NamePartType.FIRST_NAME, "Müddle"), //
280                 new NamePart(NamePartType.LAST_NAME, "Läst"), new NamePart(NamePartType.SUFFIX, "Süffix"));
281         EmailAddress ea = u.getEmails()[0];
282         verify(email, ea);
283     }
284
285     private void verify(String email, EmailAddress ea) throws GigiApiException {
286         LinkedList<String> i = emails.get(email);
287         while (i.size() > 0 && !ea.isVerified()) {
288             String lst = i.getLast();
289             Pattern p = Pattern.compile("hash=([a-zA-Z0-9]+)");
290             Matcher m = p.matcher(lst);
291             if (m.find()) {
292                 ea.verify(m.group(1));
293             }
294             i.removeLast();
295         }
296         // ea.verify(hash);
297     }
298
299     User[] agents = new User[25];
300
301     User supporter;
302
303     @Override
304     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
305         AuthorizationContext sessionAc = (AuthorizationContext) req.getSession().getAttribute(Gigi.AUTH_CONTEXT);
306         if (req.getParameter("create") != null) {
307             String prefix = req.getParameter("prefix");
308             String domain = req.getParameter("suffix");
309             try {
310                 if (null == prefix) {
311                     throw new GigiApiException("No prefix given.");
312                 }
313                 if (null == domain) {
314                     throw new GigiApiException("No domain given.");
315                 }
316
317                 DomainAssessment.checkCertifiableDomain(domain, false, true);
318
319                 if ( !EmailProvider.isValidMailAddress(prefix + "@" + domain)) {
320                     throw new GigiApiException("Invalid email address template.");
321                 }
322
323                 batchCreateUsers(prefix, domain, Integer.parseInt(req.getParameter("amount")), resp.getWriter());
324                 resp.getWriter().println("User batch created.");
325             } catch (GigiApiException e) {
326                 throw new Error(e);
327             }
328         } else if (req.getParameter("addpriv") != null || req.getParameter("delpriv") != null) {
329             User u = User.getByEmail(req.getParameter("email"));
330             if (u == null) {
331                 resp.getWriter().println("User not found.");
332                 return;
333             }
334             try {
335                 if (req.getParameter("addpriv") != null) {
336                     u.grantGroup(getSupporter(), Group.getByString(req.getParameter("priv")));
337                     resp.getWriter().println("Privilege granted");
338                 } else {
339                     u.revokeGroup(getSupporter(), Group.getByString(req.getParameter("priv")));
340                     resp.getWriter().println("Privilege revoked");
341                 }
342             } catch (GigiApiException e) {
343                 throw new Error(e);
344             }
345         } else if (req.getParameter("fetch") != null) {
346             String mail = req.getParameter("femail");
347             fetchMails(req, resp, mail);
348         } else if (req.getParameter("cats") != null) {
349             String mail = req.getParameter("catsEmail");
350             String testId = req.getParameter("catsType");
351             User byEmail = User.getByEmail(mail);
352             if (byEmail == null) {
353                 resp.getWriter().println("User not found.");
354                 return;
355             }
356             if (testId == null) {
357                 resp.getWriter().println("No test given.");
358                 return;
359             }
360             CATSType test = CATSType.values()[Integer.parseInt(testId)];
361             passCATS(byEmail, test);
362             resp.getWriter().println("Test '" + test.getDisplayName() + "' was added to user account.");
363         } else if (req.getParameter("verify") != null) {
364             String mail = req.getParameter("verifyEmail");
365             String verificationPoints = req.getParameter("verificationPoints");
366             User byEmail = User.getByEmail(mail);
367
368             if (byEmail == null) {
369                 resp.getWriter().println("User not found.");
370                 return;
371             }
372
373             int vp = 0;
374             int agentNumber = 0;
375
376             try {
377                 try {
378                     vp = Integer.parseInt(verificationPoints);
379                 } catch (NumberFormatException e) {
380                     throw new GigiApiException("No valid Verification Points entered.");
381                 }
382
383                 if (vp > 100) { // only allow max 100 Verification points
384                     vp = 100;
385                 }
386
387                 while (vp > 0) {
388                     int currentVP = 10;
389                     if (vp < 10) {
390                         currentVP = vp;
391                     }
392                     Notary.verify(getAgent(agentNumber), byEmail, byEmail.getPreferredName(), byEmail.getDoB(), currentVP, "Testmanager Verify up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
393                     agentNumber += 1;
394                     vp -= currentVP;
395                 }
396
397             } catch (GigiApiException e) {
398                 throw new Error(e);
399             }
400
401             resp.getWriter().println("User has been verified " + agentNumber + " times.");
402
403         } else if (req.getParameter("letverify") != null) {
404             String mail = req.getParameter("letverifyEmail");
405             User byEmail = User.getByEmail(mail);
406             if (byEmail == null || !byEmail.canVerify()) {
407                 resp.getWriter().println("User not found, or found user is not allowed to verify.");
408             } else {
409                 try {
410                     for (int i = 0; i < 25; i++) {
411                         User a = getAgent(i);
412                         Notary.verify(byEmail, a, a.getNames()[0], a.getDoB(), 10, "Testmanager exp up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
413                     }
414                     resp.getWriter().println("Successfully added experience points.");
415                 } catch (GigiApiException e) {
416                     throw new Error(e);
417                 }
418             }
419         } else if (req.getParameter("addEmail") != null) {
420             User u = User.getByEmail(req.getParameter("addEmailEmail"));
421             try {
422                 EmailAddress ea = new EmailAddress(u, req.getParameter("addEmailNew"), Locale.ENGLISH);
423                 verify(ea.getAddress(), ea);
424             } catch (IllegalArgumentException e) {
425                 e.printStackTrace();
426                 resp.getWriter().println("An internal error occured.");
427             } catch (GigiApiException e) {
428                 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH), getDefaultVars(req));
429             }
430         } else if (req.getParameter("addCert") != null) {
431             User u = User.getByEmail(req.getParameter("addCertEmail"));
432             try {
433                 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
434                 kpg.initialize(4096);
435                 KeyPair kp = kpg.generateKeyPair();
436                 SPKAC s = new SPKAC((X509Key) kp.getPublic(), "challenge");
437                 Signature sign = Signature.getInstance("SHA512withRSA");
438                 sign.initSign(kp.getPrivate());
439
440                 byte[] res = s.getEncoded(sign);
441
442                 CertificateRequest cr = new CertificateRequest(new AuthorizationContext(u, u, sessionAc.isStronglyAuthenticated()), Base64.getEncoder().encodeToString(res), "challenge");
443                 cr.update(CertificateRequest.DEFAULT_CN, Digest.SHA512.toString(), "client", null, "", "email:" + u.getEmail());
444                 Certificate draft = cr.draft();
445                 draft.issue(null, "2y", u).waitFor(10000);
446                 if (draft.getStatus() == CertificateStatus.ISSUED) {
447                     resp.getWriter().println("added certificate");
448                 } else {
449                     resp.getWriter().println("signer failed");
450                 }
451             } catch (GeneralSecurityException e1) {
452                 e1.printStackTrace();
453                 resp.getWriter().println("error");
454             } catch (GigiApiException e) {
455                 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH), getDefaultVars(req));
456             }
457
458         } else if (req.getParameter("addExDom") != null) {
459             String dom = req.getParameter("exemptDom");
460             pingExempt.add(dom);
461             resp.getWriter().println("Updated domains exempt from pings. Current set: <br/>");
462             resp.getWriter().println(HTMLEncoder.encodeHTML(pingExempt.toString()));
463         } else if (req.getParameter("delExDom") != null) {
464             String dom = req.getParameter("exemptDom");
465             pingExempt.remove(dom);
466             resp.getWriter().println("Updated domains exempt from pings. Current set: <br/>");
467             resp.getWriter().println(HTMLEncoder.encodeHTML(pingExempt.toString()));
468         }
469         resp.getWriter().println("<br/><a href='" + PATH + "'>Go back</a>");
470     }
471
472     private void fetchMails(HttpServletRequest req, HttpServletResponse resp, String mail) throws IOException {
473         final LinkedList<String> mails = emails.get(mail);
474         HashMap<String, Object> vars = new HashMap<>();
475         vars.put("mail", mail);
476         if (mails != null) {
477             vars.put("mails", new IterableDataset() {
478
479                 Iterator<String> s = mails.iterator();
480
481                 @Override
482                 public boolean next(Language l, Map<String, Object> vars) {
483                     if ( !s.hasNext()) {
484                         return false;
485                     }
486                     vars.put("body", s.next().replaceAll("(https?://\\S+)", "<a href=\"$1\">$1</a>"));
487                     return true;
488                 }
489             });
490         }
491         t.output(resp.getWriter(), getLanguage(req), vars);
492         if (mails == null) {
493             resp.getWriter().println("No mails");
494
495         }
496     }
497
498     private static final Template form = new Template(Manager.class.getResource("Manager.templ"));
499
500     @Override
501     public boolean needsLogin() {
502         return false;
503     }
504
505     @Override
506     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
507         String pi = req.getPathInfo().substring(PATH.length());
508         if (pi.length() > 1 && pi.startsWith("/fetch-")) {
509             String mail = pi.substring(pi.indexOf('-', 2) + 1);
510             fetchMails(req, resp, mail);
511             return;
512         }
513         HashMap<String, Object> vars = new HashMap<>();
514         vars.put("cats_types", new IterableDataset() {
515
516             CATSType[] type = CATSType.values();
517
518             int i = 0;
519
520             @Override
521             public boolean next(Language l, Map<String, Object> vars) {
522                 if (i >= type.length) {
523                     return false;
524                 }
525                 CATSType t = type[i++];
526                 vars.put("id", i - 1);
527                 vars.put("name", t.getDisplayName());
528                 return true;
529             }
530         });
531
532         vars.put("testValidMonths", TimeConditions.getInstance().getTestMonths());
533         vars.put("reverificationDays", TimeConditions.getInstance().getVerificationLimitDays());
534         vars.put("verificationFreshMonths", TimeConditions.getInstance().getVerificationMonths());
535         vars.put("verificationMaxAgeMonths", TimeConditions.getInstance().getVerificationMaxAgeMonths());
536         vars.put("emailPingMonths", TimeConditions.getInstance().getEmailPingMonths());
537
538         form.output(resp.getWriter(), getLanguage(req), vars);
539     }
540 }