Skip to content

Commit b24bd25

Browse files
committed
Address comments
1 parent db89fb8 commit b24bd25

File tree

5 files changed

+38
-54
lines changed

5 files changed

+38
-54
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/Constants.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ final class Constants {
3030

3131
// Only Service Fabric and App Service managed identity environments support token revocation
3232
public static final ManagedIdentitySourceType[] TOKEN_REVOCATION_SUPPORTED_ENVIRONMENTS = {
33-
ManagedIdentitySourceType.APP_SERVICE,
3433
ManagedIdentitySourceType.SERVICE_FABRIC
3534
};
3635

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ManagedIdentityRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public ManagedIdentityRequest(ManagedIdentityApplication managedIdentityApplicat
5757
if (queryParameters == null) {
5858
queryParameters = new HashMap<>();
5959
}
60-
String tokenHash = TokenRevocationUtil.convertTokenToSHA256HashString(parameters.getTokenToRevoke());
60+
String tokenHash = StringHelper.createSha256HashHexString(parameters.getTokenToRevoke());
6161
queryParameters.put(Constants.TOKEN_REVOCATION_REQUEST_PARAM, Collections.singletonList(tokenHash));
6262
}
6363
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/StringHelper.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,39 @@ static private String createSha256Hash(String stringToHash, boolean base64Encode
4141
return res;
4242
}
4343

44+
/**
45+
* Creates a SHA-256 hash of the input string and returns it as a lowercase hex string.
46+
* This is used for token revocation and other scenarios requiring hex hash representation.
47+
*
48+
* @param stringToHash The string to hash
49+
* @return The SHA-256 hash of the string as a lowercase hex string
50+
* @throws MsalClientException If the SHA-256 algorithm is not available
51+
*/
52+
public static String createSha256HashHexString(String stringToHash) {
53+
if (stringToHash == null || stringToHash.isEmpty()) {
54+
throw new IllegalArgumentException("String to hash cannot be null or empty");
55+
}
56+
57+
try {
58+
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
59+
byte[] hash = messageDigest.digest(stringToHash.getBytes(StandardCharsets.UTF_8));
60+
61+
// Convert to hex string
62+
StringBuilder hexString = new StringBuilder();
63+
for (byte b : hash) {
64+
String hex = Integer.toHexString(0xff & b);
65+
if (hex.length() == 1) {
66+
hexString.append('0');
67+
}
68+
hexString.append(hex);
69+
}
70+
return hexString.toString();
71+
} catch (NoSuchAlgorithmException e) {
72+
throw new MsalClientException("Failed to create SHA-256 hash: " + e.getMessage(),
73+
AuthenticationErrorCode.CRYPTO_ERROR);
74+
}
75+
}
76+
4477
public static boolean isNullOrBlank(final String str) {
4578
return str == null || str.trim().length() == 0;
4679
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/TokenRevocationUtil.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/TokenRevocationTest.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,29 @@
44
package com.microsoft.aad.msal4j;
55

66
import org.junit.jupiter.api.Test;
7-
import org.junit.jupiter.api.BeforeEach;
87
import static org.junit.jupiter.api.Assertions.*;
98

10-
import java.util.Arrays;
11-
import java.util.HashMap;
12-
import java.util.Map;
13-
149
public class TokenRevocationTest {
1510

1611
private static final String TEST_TOKEN = "test_token";
1712
private static final String EXPECTED_TOKEN_HASH = "cc0af97287543b65da2c7e1476426021826cab166f1e063ed012b855ff819656";
1813
private static final String TEST_RESOURCE = "https://management.azure.com";
19-
20-
@Test
14+
@Test
2115
public void testConvertTokenToSHA256Hash() {
22-
String hash = TokenRevocationUtil.convertTokenToSHA256HashString(TEST_TOKEN);
16+
String hash = StringHelper.createSha256HashHexString(TEST_TOKEN);
2317
assertEquals(EXPECTED_TOKEN_HASH, hash);
2418
}
2519

2620
@Test
2721
public void testTokenToRevokeValidation() {
2822
// Should throw exception when null
2923
assertThrows(IllegalArgumentException.class, () -> {
30-
TokenRevocationUtil.convertTokenToSHA256HashString(null);
24+
StringHelper.createSha256HashHexString(null);
3125
});
3226

3327
// Should throw exception when empty
3428
assertThrows(IllegalArgumentException.class, () -> {
35-
TokenRevocationUtil.convertTokenToSHA256HashString("");
29+
StringHelper.createSha256HashHexString("");
3630
});
3731
}
3832

0 commit comments

Comments
 (0)