From 4eeb624b09ebc49a77b9d60a05eedf7151275107 Mon Sep 17 00:00:00 2001 From: Soisik Froger Date: Mon, 16 Oct 2023 11:27:50 +0200 Subject: [PATCH 1/2] 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 | 14 +++++++++++++- 2 files changed, 31 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..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)); + } } From c7c39d4cea0711bb53c275b41fd3f12faa52b532 Mon Sep 17 00:00:00 2001 From: Soisik Froger Date: Mon, 23 Oct 2023 18:31:28 +0200 Subject: [PATCH 2/2] Update src/test/java/org/lsc/utils/directory/ADTest.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Raphaƫl Ouazana --- src/test/java/org/lsc/utils/directory/ADTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/lsc/utils/directory/ADTest.java b/src/test/java/org/lsc/utils/directory/ADTest.java index 67cc9760..887cdd4f 100644 --- a/src/test/java/org/lsc/utils/directory/ADTest.java +++ b/src/test/java/org/lsc/utils/directory/ADTest.java @@ -72,7 +72,7 @@ public class ADTest { 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 + // UUID/String representation of above binary objectGUID private static final String refObjectGUIDAsUUIDString = "300EDD3D-0C8F-484C-AAD2-60481BCB60D8"; @Before