diff --git a/src/main/java/org/lsc/utils/directory/AD.java b/src/main/java/org/lsc/utils/directory/AD.java index 51686c2c..468d00a9 100644 --- a/src/main/java/org/lsc/utils/directory/AD.java +++ b/src/main/java/org/lsc/utils/directory/AD.java @@ -52,6 +52,8 @@ import java.util.HashMap; import java.util.Map; +import org.apache.commons.codec.binary.Hex; + /** * Utility class to manage specific entries for a Microsoft ActiveDirectory * @@ -243,6 +245,22 @@ public static long getAccountExpires(String expireDate) throws ParseException { return getAccountExpires(expireDate, "yyyy-MM-dd"); } + /** + *

Decode the binary value of a GUID and convert into a readble string representation of this UUID.

+ *

Call this method with binary value of objectGUID attribute:
+ * AD.binaryGuidToReadableUUID(srcBean.getDatasetFirstBinaryValueById("objectGUID"));

+ *

Attribute objectGUID needs to be declared as binary in your AD connexion for this method to work.

+ * @param GUID the binary GUID as sent by Microsoft AD + * @return the UUID/string representation of this binary GUID + */ + public static String binaryGuidToReadableUUID(byte[] GUID) { + if (GUID != null) { + String hex = new String(Hex.encodeHex(GUID)); + return hex.replaceFirst("^(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)$","$4$3$2$1-$6$5-$8$7-$9$10-$11$12$13$14$15$16").toUpperCase(); + } + return ""; + } + /* The Hash of values to set or to unset */ private static final Map setHexValue = new HashMap(); private static final Map unsetHexValue = new HashMap(); diff --git a/src/test/java/org/lsc/utils/directory/ADTest.java b/src/test/java/org/lsc/utils/directory/ADTest.java index 8e8ccc0f..67cc9760 100644 --- a/src/test/java/org/lsc/utils/directory/ADTest.java +++ b/src/test/java/org/lsc/utils/directory/ADTest.java @@ -45,6 +45,7 @@ */ package org.lsc.utils.directory; +import java.util.Base64; import java.util.Calendar; import java.util.TimeZone; @@ -69,7 +70,11 @@ public class ADTest { // representation of a date where Unix time need a long: Tue, 19 Jan 2038 03:14:10 GMT private static final long otherTimeADLong = 137919572500000000L; private static final long otherTimeUnixLong= 2147483650L; - + // base64 encoding of a binary objectGUID + private static final byte[] refObjectGUIDBase64 = Base64.getDecoder().decode(new String("Pd0OMI8MTEiq0mBIG8tg2A==").getBytes()); + // UUID/String reprensentation of above binary objectGUID + private static final String refObjectGUIDAsUUIDString = "300EDD3D-0C8F-484C-AAD2-60481BCB60D8"; + @Before public void setUp() { refTimeCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); @@ -177,4 +182,11 @@ public final void testUnixTimestampToADTimeWithLong() { assertEquals(otherTimeADLong, AD.unixTimestampToADTime(otherTimeUnixLong)); } + /** + * Test for the {@link AD#binaryGuidToReadableUUID(byte[])} method. + */ + @Test + public final void testBinaryGuidToReadableUUID () { + assertEquals(refObjectGUIDAsUUIDString, AD.binaryGuidToReadableUUID(refObjectGUIDBase64)); + } }