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 {
15 private String address;
21 private String hash = null;
23 private EmailAddress(int id) throws SQLException {
24 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, email, hash FROM `email` WHERE id=? AND deleted=0");
27 ResultSet rs = ps.executeQuery();
29 throw new IllegalArgumentException("Invalid email id " + id);
32 owner = User.getById(rs.getInt(1));
33 address = rs.getString(2);
34 hash = rs.getString(3);
38 public EmailAddress(String address, User owner) {
39 if ( !EmailProvider.MAIL.matcher(address).matches()) {
40 throw new IllegalArgumentException("Invalid email.");
42 this.address = address;
44 this.hash = RandomToken.generateToken(16);
47 public void insert(Language l) {
49 throw new IllegalStateException("already inserted.");
52 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `email` SET memid=?, hash=?, email=?");
53 ps.setInt(1, owner.getId());
54 ps.setString(2, hash);
55 ps.setString(3, address);
57 id = DatabaseConnection.lastInsertId(ps);
58 StringBuffer body = new StringBuffer();
59 body.append(l.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!"));
60 body.append("\n\nhttps://");
61 body.append(ServerConstants.getWwwHostNamePort());
62 body.append("/verify?type=email&id=");
64 body.append("&hash=");
67 body.append(l.getTranslation("Best regards"));
69 body.append(l.getTranslation("CAcert.org Support!"));
70 EmailProvider.getInstance().sendmail(address, "[CAcert.org] " + l.getTranslation("Mail Probe"), 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().prepare("UPDATE `email` SET hash='' WHERE id=?");
95 // Verify user with that primary email
96 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
97 ps2.setInt(1, owner.getId());
98 ps2.setString(2, address);
101 } catch (SQLException e) {
102 throw new GigiApiException(e);
106 throw new GigiApiException("Email verification hash is invalid.");
110 public static EmailAddress getById(int id) throws IllegalArgumentException {
113 EmailAddress e = new EmailAddress(id);
115 } catch (SQLException e) {
116 throw new IllegalArgumentException(e);
120 public boolean isVerified() {
121 return hash.isEmpty();