]> WPIA git - gigi.git/blob - src/org/cacert/gigi/EmailAddress.java
Enforce POST requests to only contain POST data.
[gigi.git] / src / org / cacert / gigi / EmailAddress.java
1 package org.cacert.gigi;
2
3 import java.io.IOException;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7
8 import org.cacert.gigi.database.DatabaseConnection;
9 import org.cacert.gigi.email.EmailProvider;
10 import org.cacert.gigi.util.RandomToken;
11 import org.cacert.gigi.util.ServerConstants;
12
13 public class EmailAddress {
14         private String address;
15         private int id;
16         private User owner;
17         private String hash = null;
18
19         private EmailAddress(int id) throws SQLException {
20                 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
21                         "SELECT memid, email, hash FROM `email` WHERE id=? AND deleted=0");
22                 ps.setInt(1, id);
23
24                 ResultSet rs = ps.executeQuery();
25                 if (!rs.next()) {
26                         throw new IllegalArgumentException("Invalid email id " + id);
27                 }
28                 this.id = id;
29                 owner = User.getById(rs.getInt(1));
30                 address = rs.getString(2);
31                 hash = rs.getString(3);
32                 rs.close();
33         }
34
35         public EmailAddress(String address, User owner) {
36                 this.address = address;
37                 this.owner = owner;
38                 this.hash = RandomToken.generateToken(16);
39         }
40
41         public void insert(Language l) {
42                 if (id != 0) {
43                         throw new IllegalStateException("already inserted.");
44                 }
45                 try {
46                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
47                                 "INSERT INTO `email` SET memid=?, hash=?, email=?");
48                         ps.setInt(1, owner.getId());
49                         ps.setString(2, hash);
50                         ps.setString(3, address);
51                         ps.execute();
52                         id = DatabaseConnection.lastInsertId(ps);
53                         StringBuffer body = new StringBuffer();
54                         body.append(l
55                                 .getTranslation("Thanks for signing up with CAcert.org, below is the link you need to open to verify your account. Once your account is verified you will be able to start issuing certificates till your hearts' content!"));
56                         body.append("\n\nhttps://");
57                         body.append(ServerConstants.getWwwHostNamePort());
58                         body.append("/verify?type=email&id=");
59                         body.append(id);
60                         body.append("&hash=");
61                         body.append(hash);
62                         body.append("\n\n");
63                         body.append(l.getTranslation("Best regards"));
64                         body.append("\n");
65                         body.append(l.getTranslation("CAcert.org Support!"));
66                         EmailProvider.getInstance().sendmail(address, "[CAcert.org] " + l.getTranslation("Mail Probe"),
67                                 body.toString(), "support@cacert.org", null, null, null, null, false);
68                 } catch (SQLException e) {
69                         e.printStackTrace();
70                 } catch (IOException e) {
71                         e.printStackTrace();
72                 }
73         }
74
75         public int getId() {
76                 return id;
77         }
78
79         public String getAddress() {
80                 return address;
81         }
82
83         public synchronized void verify(String hash) throws GigiApiException {
84                 if (this.hash.equals(hash)) {
85
86                         try {
87                                 PreparedStatement ps = DatabaseConnection.getInstance()
88                                         .prepare("UPDATE `email` SET hash='' WHERE id=?");
89                                 ps.setInt(1, id);
90                                 ps.execute();
91                                 hash = "";
92
93                                 // Verify user with that primary email
94                                 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare(
95                                         "update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
96                                 ps2.setInt(1, owner.getId());
97                                 ps2.setString(2, address);
98                                 ps2.execute();
99                                 this.hash = "";
100                         } catch (SQLException e) {
101                                 throw new GigiApiException(e);
102                         }
103
104                 } else {
105                         throw new GigiApiException("Email verification hash is invalid.");
106                 }
107         }
108
109         public static EmailAddress getById(int id) throws IllegalArgumentException {
110                 // TODO cache
111                 try {
112                         EmailAddress e = new EmailAddress(id);
113                         return e;
114                 } catch (SQLException e) {
115                         throw new IllegalArgumentException(e);
116                 }
117         }
118
119         public boolean isVerified() {
120                 return hash.isEmpty();
121         }
122 }