1 package edu.internet2.middleware.shibboleth.common;
3 import java.util.StringTokenizer;
5 import javax.crypto.Cipher;
6 import javax.crypto.SecretKey;
7 import org.doomdark.uuid.UUID;
8 import org.doomdark.uuid.UUIDGenerator;
11 * A Shibboleth Attribute Query Handle.
13 * @author Walter Hoehn wassa@columbia.edu
17 public class AttributeQueryHandle {
19 private String principal;
20 private long creationTime;
21 private long expirationTime;
22 private byte[] cipherTextHandle;
23 private String handleID;
26 * Unmarshalls an <code>AttributeQueryHandle</code> based on the results of the serialize() method
27 * of an existing <code>AttributeQueryHandle</code>. Requires a key identical to the one used
28 * in the creation of the original <code>AttributeQueryHandle</code>.
32 public AttributeQueryHandle(byte[] handle, SecretKey key)
33 throws HandleException {
36 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
37 cipher.init(Cipher.DECRYPT_MODE, key);
38 StringTokenizer tokenizer =
40 new String(cipher.doFinal(Base64.decode(handle)), "UTF-8"),
45 Base64.decode(tokenizer.nextToken().getBytes("ASCII")),
47 expirationTime = new Long(tokenizer.nextToken()).longValue();
48 handleID = tokenizer.nextToken();
49 } catch (Exception e) {
50 throw new HandleException("Error unmarshalling handle: " + e);
56 * Creates a new <code>AttributeQueryHandle</code>
57 * @param principal <code>String</code> representation of user that the handle should reference
58 * @param validityPeriod Time in milliseconds for which the handle should be valid
59 * @param hsLocation URL of the Handle Service used to generate the AQH
60 * @param key Symmetric key used to encrypt the AQH upon serialization
64 public AttributeQueryHandle(
69 throws HandleException {
71 this.principal = principal;
72 this.creationTime = System.currentTimeMillis();
73 this.expirationTime = creationTime + validityPeriod;
76 //create a unique id based on the url of the HS and the current time
77 UUIDGenerator uuidGen = UUIDGenerator.getInstance();
78 UUID nameSpaceUUID = new UUID(UUID.NAMESPACE_URL);
80 uuidGen.generateNameBasedUUID(nameSpaceUUID, hsLocation)
82 + uuidGen.generateTimeBasedUUID();
84 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
85 cipher.init(Cipher.ENCRYPT_MODE, key);
90 Base64.encode(principal.getBytes("UTF-8")),
98 } catch (Exception e) {
99 throw new HandleException("Error creating handle: " + e);
106 * Returns a <code>String</code> representation of the user that the handle references.
109 public String getPrincipal() {
114 * Returns bytes of ciphertext representing the <code>AttributeQueryHandle</code> instance.
117 public byte[] serialize() {
119 return Base64.encode(cipherTextHandle);
123 * Boolean result indicates whether the validity of this <code>AttributeQueryHandle</code>
127 public boolean isExpired() {
129 if (System.currentTimeMillis() >= expirationTime) {
138 * Returns a <code>String</code> representation of the unique identifier for this handle.
141 public String getHandleID() {