X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2FConfirmation.java;h=6f3428da0aaf4ee8568ecefd154dedc359794e57;hb=39707349ea7742cb8a883b307834b04f1aab9dd7;hp=ba15fb8cf8e4e082b6bb5582adac1831863ef017;hpb=fab67c8262b062c00d179a5f057d4e489e7c4366;p=org.ibex.mail.git diff --git a/src/org/ibex/mail/Confirmation.java b/src/org/ibex/mail/Confirmation.java index ba15fb8..6f3428d 100644 --- a/src/org/ibex/mail/Confirmation.java +++ b/src/org/ibex/mail/Confirmation.java @@ -4,7 +4,6 @@ package org.ibex.mail; import java.lang.reflect.*; -//import org.prevayler.*; import org.ibex.crypto.*; import org.ibex.util.*; import org.ibex.mail.protocol.*; @@ -23,13 +22,27 @@ import java.io.*; public abstract class Confirmation implements Externalizable { public static final long serialVersionUID = 0x981879f18a11ffeeL; - public static final Address FROM = Address.parse("adam@megacz.com"); // FIXME + + public static int master_serial = 0; public transient Address who = null; public long expiration; public abstract String getDescription(); + public abstract String getURL(String tail); + public int serial; + + private static HashMap all = new HashMap(); - protected Confirmation(Address who, long expiration) { this.who = who; this.expiration = expiration; } + public static Confirmation get(int serial) { + return all.get(serial); + } + + protected Confirmation(Address who, long expiration) { + this.who = who; + this.expiration = expiration; + this.serial = master_serial++; + all.put(serial, this); + } public void readExternal(ObjectInput s) throws IOException { try { @@ -87,14 +100,21 @@ public abstract class Confirmation implements Externalizable { } } - public void signAndSend(long secret, Date now) throws IOException, Message.Malformed { - SMTP.Outgoing.accept(Message.newMessage(new Fountain.StringFountain("From: " + FROM + "\r\n" + - "To: " + who.toString(true) + "\r\n" + - "Subject: confirm " + getDescription() + "\r\n" + - "\r\n" + - "Please click the link below to " + getDescription() + "\r\n" + - sign(secret))) - ); + public void signAndSend(Address sender, long secret, Date now) throws IOException, Message.Malformed { + + Headers h = new Headers(new String[] { + "From", sender.toString(true), + "To", who.toString(true), + "Message-Id", Message.generateFreshMessageId(), + "Date", new Date()+"" /*FIXME!!!*/, + "Subject", "confirm " + getDescription() + }); + + Fountain fountain = Fountain.Util.create("Please click the link below to " + + getDescription() + "\r\n" + + getURL(sign(secret))); + Message m = Message.newMessageFromHeadersAndBody(h, fountain, sender, who); + SMTP.Outgoing.enqueue(m); } public String sign(long secret) throws IOException { @@ -104,7 +124,9 @@ public abstract class Confirmation implements Externalizable { oos.flush(); oos.close(); byte[] b = os.toByteArray(); - StringBuffer sb = new StringBuffer(new String(Encode.toBase64(b))); + StringBuffer sb = new StringBuffer(); + sb.append(serial); + //sb.append(new String(Encode.toBase64(b))); sb.append('.'); SHA1 sha1 = new SHA1(); sha1.update(b, 0, b.length); @@ -116,15 +138,11 @@ public abstract class Confirmation implements Externalizable { public static Confirmation decode(String encoded, long secret, Date now) { try { - String payload = encoded.substring(0, encoded.indexOf('.')); - ObjectInputStream ois = new ObjectInputStream(new InflaterInputStream(new Encode.Base64InputStream(payload))); - Confirmation cve = (Confirmation)ois.readObject(); + String serial = encoded.substring(0, encoded.indexOf('.')); + Confirmation cve = get(Integer.parseInt(serial)); if (!cve.sign(secret).equals(encoded)) throw new InvalidSignature(); if (now.getTime() > cve.expiration) throw new Expired(); return cve; - } catch (ClassNotFoundException e) { - Log.error(Confirmation.class, e); - throw new InvalidSignature(); } catch (IOException e) { Log.error(Confirmation.class, e); throw new InvalidSignature();