Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added utility method + unit test to convert a byte[] objectGUID attribute #234

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
soisik marked this conversation as resolved.
Show resolved Hide resolved
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));
}
}