1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
6 import java.lang.reflect.*;
7 //import org.prevayler.*;
8 import org.ibex.crypto.*;
9 import org.ibex.util.*;
10 import org.ibex.mail.protocol.*;
13 import java.util.zip.*;
18 * A convenient way to verify that the agent requesting an action
19 * owns a particular email address. Extend this class; the
20 * Transaction.executeOn() method will be invoked when the user
23 public abstract class Confirmation implements Externalizable {
25 public static final long serialVersionUID = 0x981879f18a11ffeeL;
26 public static final Address FROM = Address.parse("adam@megacz.com"); // FIXME
28 public transient Address who = null;
29 public long expiration;
30 public abstract String getDescription();
32 protected Confirmation(Address who, long expiration) { this.who = who; this.expiration = expiration; }
34 public void readExternal(ObjectInput s) throws IOException {
36 int numfields = s.readInt();
37 Class c = this.getClass();
38 for(int i=0; i<numfields; i++) {
39 String name = s.readUTF();
40 Field f = c.getField(name);
41 Class c2 = f.getType();
42 if (c2 == Boolean.TYPE) f.setBoolean(this, s.readBoolean());
43 else if (c2 == Byte.TYPE) f.setByte(this, s.readByte());
44 else if (c2 == Long.TYPE) f.setLong(this, s.readLong());
45 else if (c2 == Integer.TYPE) f.setInt(this, s.readInt());
46 else if (c2 == Character.TYPE) f.setChar(this, s.readChar());
47 else if (c2 == Short.TYPE) f.setShort(this, s.readShort());
48 else if (c2 == Float.TYPE) f.setFloat(this, s.readFloat());
49 else if (c2 == Double.TYPE) f.setDouble(this, s.readDouble());
50 else if (c2 == String.class) f.set(this, s.readObject());
51 else f.set(this, s.readObject());
53 } catch (Exception e) {
58 public void writeExternal(ObjectOutput s) throws IOException {
60 Class c = this.getClass();
61 Field[] fields = c.getFields();
63 for(int i=0; i<fields.length; i++) {
65 if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0) continue;
68 s.writeInt(numfields);
69 for(int i=0; i<fields.length; i++) {
71 if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0) continue;
72 s.writeUTF(fields[i].getName());
73 Class c2 = f.getType();
74 if (c2 == Boolean.TYPE) s.writeBoolean(f.getBoolean(this));
75 else if (c2 == Byte.TYPE) s.writeByte(f.getByte(this));
76 else if (c2 == Long.TYPE) s.writeLong(f.getLong(this));
77 else if (c2 == Integer.TYPE) s.writeInt(f.getInt(this));
78 else if (c2 == Character.TYPE) s.writeChar(f.getChar(this));
79 else if (c2 == Short.TYPE) s.writeShort(f.getShort(this));
80 else if (c2 == Float.TYPE) s.writeFloat(f.getFloat(this));
81 else if (c2 == Double.TYPE) s.writeDouble(f.getDouble(this));
82 else if (c2 == String.class) s.writeUTF((String)f.get(this));
83 else s.writeObject(f.get(this));
85 } catch (Exception e) {
90 public void signAndSend(long secret, Date now) throws IOException, Message.Malformed {
91 SMTP.Outgoing.accept(Message.newMessage(new Stream("From: " + FROM + "\r\n" +
92 "To: " + who.toString(true) + "\r\n" +
93 "Subject: confirm " + getDescription() + "\r\n" +
95 "Please click the link below to " + getDescription() + "\r\n" +
101 public String sign(long secret) throws IOException {
102 ByteArrayOutputStream os = new ByteArrayOutputStream();
103 ObjectOutputStream oos = new ObjectOutputStream(new DeflaterOutputStream(os));
104 oos.writeObject(this);
107 byte[] b = os.toByteArray();
108 StringBuffer sb = new StringBuffer(new String(Base64.encode(b)));
110 SHA1 sha1 = new SHA1();
111 sha1.update(b, 0, b.length);
112 b = new byte[sha1.getDigestSize()];
114 sb.append(new String(Base64.encode(b)));
115 return sb.toString();
118 public static Confirmation decode(String encoded, long secret, Date now) {
120 String payload = encoded.substring(0, encoded.indexOf('.'));
121 ObjectInputStream ois = new ObjectInputStream(new InflaterInputStream(new Base64.InputStream(payload)));
122 Confirmation cve = (Confirmation)ois.readObject();
123 if (!cve.sign(secret).equals(encoded)) throw new InvalidSignature();
124 if (now.getTime() > cve.expiration) throw new Expired();
126 } catch (ClassNotFoundException e) {
127 Log.error(Confirmation.class, e);
128 throw new InvalidSignature();
129 } catch (IOException e) {
130 Log.error(Confirmation.class, e);
131 throw new InvalidSignature();
135 public static class Exn extends RuntimeException { }
136 public static class Expired extends Exn { }
137 public static class InvalidSignature extends Exn { }