]> WPIA git - gigi.git/blob - src/club/wpia/gigi/api/APIPoint.java
Merge "upd: remove 'browser install'"
[gigi.git] / src / club / wpia / gigi / api / APIPoint.java
1 package club.wpia.gigi.api;
2
3 import java.io.IOException;
4 import java.math.BigInteger;
5 import java.security.cert.X509Certificate;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import club.wpia.gigi.dbObjects.Certificate;
11 import club.wpia.gigi.dbObjects.CertificateOwner;
12 import club.wpia.gigi.dbObjects.User;
13 import club.wpia.gigi.pages.LoginPage;
14
15 public abstract class APIPoint {
16
17     public void process(HttpServletRequest req, HttpServletResponse resp) throws IOException {
18         X509Certificate cert = LoginPage.getCertificateFromRequest(req);
19         if (cert == null) {
20             resp.sendError(403, "Error, cert authing required. No cert found.");
21             return;
22         }
23         BigInteger serial = LoginPage.extractSerialFormCert(cert);
24         Certificate clientCert = Certificate.getBySerial(serial);
25         CertificateOwner u = CertificateOwner.getByEnabledSerial(serial);
26         if (u == null || clientCert == null) {
27             resp.sendError(403, "Error, cert authing required. Serial not found: " + serial);
28             return;
29         }
30         if (req.getMethod().equals("GET")) {
31             if (u instanceof User) {
32                 processGet(req, resp, (User) u);
33                 return;
34             } else {
35                 resp.sendError(500, "Error, requires a User certificate.");
36                 return;
37             }
38         }
39
40         if ( !req.getMethod().equals("POST")) {
41             resp.sendError(500, "Error, POST required.");
42             return;
43         }
44         if (req.getQueryString() != null) {
45             resp.sendError(500, "Error, no query String allowed.");
46             return;
47         }
48         process(req, resp, u, clientCert);
49     }
50
51     protected void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u, Certificate clientCert) throws IOException {
52         process(req, resp, u);
53     }
54
55     protected void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u) throws IOException {
56         if (u instanceof User) {
57             process(req, resp, (User) u);
58         } else {
59             resp.sendError(500, "Error, requires a User certificate.");
60             return;
61         }
62     }
63
64     protected void process(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
65         resp.sendError(500, "Error, Post not allowed.");
66     }
67
68     protected void processGet(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
69         resp.sendError(500, "Error, Get not allowed.");
70     }
71 }