1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
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;
13 public class EmailAddress {
14 private String address;
17 private String hash = null;
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");
24 ResultSet rs = ps.executeQuery();
26 throw new IllegalArgumentException("Invalid email id " + id);
29 owner = User.getById(rs.getInt(1));
30 address = rs.getString(2);
31 hash = rs.getString(3);
35 public EmailAddress(String address, User owner) {
36 if (!EmailProvider.MAIL.matcher(address).matches()) {
37 throw new IllegalArgumentException("Invalid email.");
39 this.address = address;
41 this.hash = RandomToken.generateToken(16);
44 public void insert(Language l) {
46 throw new IllegalStateException("already inserted.");
49 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
50 "INSERT INTO `email` SET memid=?, hash=?, email=?");
51 ps.setInt(1, owner.getId());
52 ps.setString(2, hash);
53 ps.setString(3, address);
55 id = DatabaseConnection.lastInsertId(ps);
56 StringBuffer body = new StringBuffer();
58 .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!"));
59 body.append("\n\nhttps://");
60 body.append(ServerConstants.getWwwHostNamePort());
61 body.append("/verify?type=email&id=");
63 body.append("&hash=");
66 body.append(l.getTranslation("Best regards"));
68 body.append(l.getTranslation("CAcert.org Support!"));
69 EmailProvider.getInstance().sendmail(address, "[CAcert.org] " + l.getTranslation("Mail Probe"),
70 body.toString(), "support@cacert.org", null, null, null, null, false);
71 } catch (SQLException e) {
73 } catch (IOException e) {
82 public String getAddress() {
86 public synchronized void verify(String hash) throws GigiApiException {
87 if (this.hash.equals(hash)) {
90 PreparedStatement ps = DatabaseConnection.getInstance()
91 .prepare("UPDATE `email` SET hash='' WHERE id=?");
96 // Verify user with that primary email
97 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare(
98 "update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
99 ps2.setInt(1, owner.getId());
100 ps2.setString(2, address);
103 } catch (SQLException e) {
104 throw new GigiApiException(e);
108 throw new GigiApiException("Email verification hash is invalid.");
112 public static EmailAddress getById(int id) throws IllegalArgumentException {
115 EmailAddress e = new EmailAddress(id);
117 } catch (SQLException e) {
118 throw new IllegalArgumentException(e);
122 public boolean isVerified() {
123 return hash.isEmpty();