*** empty log message ***
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / common / AttributeQueryHandle.java
index ef98665..579e9b1 100755 (executable)
@@ -1,6 +1,5 @@
 package edu.internet2.middleware.shibboleth.common;
 
-import java.net.URL;
 import java.util.StringTokenizer;
 
 import javax.crypto.Cipher;
@@ -30,7 +29,7 @@ public class AttributeQueryHandle {
         * 
         */
 
-       public AttributeQueryHandle(String handle, SecretKey key)
+       public AttributeQueryHandle(byte[] handle, SecretKey key)
                throws HandleException {
 
                try {
@@ -38,10 +37,13 @@ public class AttributeQueryHandle {
                        cipher.init(Cipher.DECRYPT_MODE, key);
                        StringTokenizer tokenizer =
                                new StringTokenizer(
-                                       new String(cipher.doFinal(Base64.decode(handle))),
+                                       new String(cipher.doFinal(Base64.decode(handle)), "UTF-8"),
                                        "||",
                                        false);
-                       principal = tokenizer.nextToken();
+                       principal =
+                               new String(
+                                       Base64.decode(tokenizer.nextToken().getBytes("ASCII")),
+                                       "UTF-8");
                        expirationTime = new Long(tokenizer.nextToken()).longValue();
                        handleID = tokenizer.nextToken();
                } catch (Exception e) {
@@ -53,7 +55,7 @@ public class AttributeQueryHandle {
        /**
         * Creates a new <code>AttributeQueryHandle</code>
         * @param principal <code>String</code> representation of user that the handle should reference
-        * @param ticketLength Time in milliseconds for which the handle should be valid
+        * @param validityPeriod Time in milliseconds for which the handle should be valid
         * @param hsLocation URL of the Handle Service used to generate the AQH
         * @param key Symmetric key used to encrypt the AQH upon serialization
         * 
@@ -62,26 +64,36 @@ public class AttributeQueryHandle {
        public AttributeQueryHandle(
                String principal,
                SecretKey key,
-               long ticketLength,
-               URL hsLocation)
+               long validityPeriod,
+               String hsLocation)
                throws HandleException {
 
                this.principal = principal;
                this.creationTime = System.currentTimeMillis();
-               this.expirationTime = creationTime + ticketLength;
+               this.expirationTime = creationTime + validityPeriod;
 
                try {
+                       //create a unique id based on the url of the HS and the current time
                        UUIDGenerator uuidGen = UUIDGenerator.getInstance();
                        UUID nameSpaceUUID = new UUID(UUID.NAMESPACE_URL);
                        handleID =
-                       uuidGen.generateNameBasedUUID(nameSpaceUUID, hsLocation.toString())+ ":" + uuidGen.generateTimeBasedUUID();
-                       
+                               uuidGen.generateNameBasedUUID(nameSpaceUUID, hsLocation)
+                                       + ":"
+                                       + uuidGen.generateTimeBasedUUID();
+
                        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
                        cipher.init(Cipher.ENCRYPT_MODE, key);
                        cipherTextHandle =
                                cipher.doFinal(
-                                       (principal + "||" + expirationTime + "||" + handleID)
-                                               .getBytes());
+                                       (
+                                               new String(
+                                                       Base64.encode(principal.getBytes("UTF-8")),
+                                                       "ASCII")
+                                                       + "||"
+                                                       + expirationTime
+                                                       + "||"
+                                                       + handleID).getBytes(
+                                               "UTF-8"));
 
                } catch (Exception e) {
                        throw new HandleException("Error creating handle: " + e);
@@ -99,12 +111,12 @@ public class AttributeQueryHandle {
        }
 
        /**
-        * Returns a <code>String</code> of ciphertext representing the <code>AttributeQueryHandle</code> instance.
+        * Returns bytes of ciphertext representing the <code>AttributeQueryHandle</code> instance.
         */
 
-       public String serialize() {
+       public byte[] serialize() {
 
-               return new String(Base64.encode(cipherTextHandle));
+               return Base64.encode(cipherTextHandle);
        }
 
        /**
@@ -114,7 +126,7 @@ public class AttributeQueryHandle {
 
        public boolean isExpired() {
 
-               if (System.currentTimeMillis() > expirationTime) {
+               if (System.currentTimeMillis() >= expirationTime) {
                        return true;
                } else {
                        return false;
@@ -125,7 +137,7 @@ public class AttributeQueryHandle {
        /**
         * Returns a <code>String</code> representation of the unique identifier for this handle.
         */
-       
+
        public String getHandleID() {
                return handleID;
        }