Skip to content

Commit

Permalink
Added utility method + unit test to convert a byte[] objectGUID attri…
Browse files Browse the repository at this point in the history
…bute from AD to a UUID string representation lsc-project#43
  • Loading branch information
soisik authored and xavierba committed Oct 30, 2023
1 parent 719ab48 commit 3228a1d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main/java/org/lsc/utils/directory/AD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -243,6 +245,22 @@ public static long getAccountExpires(String expireDate) throws ParseException {
return getAccountExpires(expireDate, "yyyy-MM-dd");
}

/**
* <p>Decode the binary value of a GUID and convert into a readble string representation of this UUID.</p>
* <p>Call this method with binary value of objectGUID attribute:<br/>
* AD.binaryGuidToReadableUUID(srcBean.getDatasetFirstBinaryValueById("objectGUID"));</p>
* <p>Attribute objectGUID needs to be declared as binary in your AD connexion for this method to work.</p>
* @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<Integer, Integer> setHexValue = new HashMap<Integer, Integer>();
private static final Map<Integer, Integer> unsetHexValue = new HashMap<Integer, Integer>();
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/org/lsc/utils/directory/ADTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*/
package org.lsc.utils.directory;

import java.util.Base64;
import java.util.Calendar;
import java.util.TimeZone;

Expand All @@ -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"));
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 3228a1d

Please sign in to comment.