From f75e455745fb1c63d1fa242e627bbf21d809e45c Mon Sep 17 00:00:00 2001 From: Soisik Froger Date: Mon, 16 Oct 2023 11:27:50 +0200 Subject: [PATCH] Added utility method + unit test to convert a byte[] objectGUID attribute from AD to a UUID string representation #43 --- src/main/java/org/lsc/utils/directory/AD.java | 18 ++++++++++++++++++ .../java/org/lsc/utils/directory/ADTest.java | 13 ++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/lsc/utils/directory/AD.java b/src/main/java/org/lsc/utils/directory/AD.java index 51686c2c..d6d8746e 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 binakt 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 UUID representation of the binary ojectGUID + */ + 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..555850a6 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,10 @@ 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; - + // base65 representation of an objectGUID + private static final byte[] refObjectGUIDBase64 = Base64.getDecoder().decode(new String("Pd0OMI8MTEiq0mBIG8tg2A==").getBytes()); + private static final String refObjectGUIDAsUUIDString = "300EDD3D-0C8F-484C-AAD2-60481BCB60D8"; + @Before public void setUp() { refTimeCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); @@ -177,4 +181,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)); + } }