X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2FConfirmation.java;h=6f3428da0aaf4ee8568ecefd154dedc359794e57;hb=39707349ea7742cb8a883b307834b04f1aab9dd7;hp=dc1cf3ed5eb72d21ef3b0aae40d2d9b90bfc721a;hpb=266a951dc005261d529da58438a755eaa603adda;p=org.ibex.mail.git diff --git a/src/org/ibex/mail/Confirmation.java b/src/org/ibex/mail/Confirmation.java index dc1cf3e..6f3428d 100644 --- a/src/org/ibex/mail/Confirmation.java +++ b/src/org/ibex/mail/Confirmation.java @@ -1,6 +1,9 @@ +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + package org.ibex.mail; import java.lang.reflect.*; -import org.prevayler.*; import org.ibex.crypto.*; import org.ibex.util.*; import org.ibex.mail.protocol.*; @@ -19,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(); + + public static Confirmation get(int serial) { + return all.get(serial); + } - protected Confirmation(Address who, long expiration) { this.who = who; this.expiration = expiration; } + 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 { @@ -83,16 +100,21 @@ public abstract class Confirmation implements Externalizable { } } - public void signAndSend(long secret) throws IOException, Message.Malformed { - SMTP.Outgoing.accept(new Message(new Stream("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)), - new Message.Envelope(FROM, who, new Date()) - ) - ); + 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 { @@ -102,28 +124,25 @@ public abstract class Confirmation implements Externalizable { oos.flush(); oos.close(); byte[] b = os.toByteArray(); - StringBuffer sb = new StringBuffer(new String(Base64.encode(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); b = new byte[sha1.getDigestSize()]; sha1.doFinal(b, 0); - sb.append(new String(Base64.encode(b))); + sb.append(new String(Encode.toBase64(b))); return sb.toString(); } - public static Confirmation decode(String encoded, long secret) { + public static Confirmation decode(String encoded, long secret, Date now) { try { - // FIXME: not prevayler-safe! - String payload = encoded.substring(0, encoded.indexOf('.')); - ObjectInputStream ois = new ObjectInputStream(new InflaterInputStream(new Base64.InputStream(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 (System.currentTimeMillis() > cve.expiration) throw new Expired(); + 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();