Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import static java.util.logging.Level.INFO;

import com.azure.security.keyvault.jca.implementation.model.AccessToken;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.logging.Logger;

Expand Down Expand Up @@ -99,9 +102,9 @@ public static AccessToken getAccessToken(String resource, String aadAuthenticati

StringBuilder requestBody = new StringBuilder();
requestBody.append(GRANT_TYPE_FRAGMENT)
.append(CLIENT_ID_FRAGMENT).append(clientId)
.append(CLIENT_SECRET_FRAGMENT).append(clientSecret)
.append(RESOURCE_FRAGMENT).append(resource);
.append(CLIENT_ID_FRAGMENT).append(URLEncoder.encode(clientId, StandardCharsets.UTF_8))
.append(CLIENT_SECRET_FRAGMENT).append(URLEncoder.encode(clientSecret, StandardCharsets.UTF_8))
.append(RESOURCE_FRAGMENT).append(URLEncoder.encode(resource, StandardCharsets.UTF_8));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI:

  1. According to the ms doc, every parameters should url encoded, not encode one specific parameter nor the whole requestBody.
  2. According to the ms doc, the parameter should be scope, not resource. I don't know why it use resource here. Anyway, I just keep the original behavior.

Refs: https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-client-creds-grant-flow#first-case-access-token-request-with-a-shared-secret


String body = HttpUtil
.post(oauth2Url.toString(), requestBody.toString(), "application/x-www-form-urlencoded");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

import java.net.URLEncoder;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
Expand All @@ -20,11 +18,9 @@ public class AccessTokenUtilTest {

/**
* Test getAuthorizationToken method.
*
* @throws Exception when a serious error occurs.
*/
@Test
public void testGetAuthorizationToken() throws Exception {
public void testGetAuthorizationToken() {
String tenantId = PropertyConvertorUtils.getPropertyValue("AZURE_KEYVAULT_TENANT_ID");
String clientId = PropertyConvertorUtils.getPropertyValue("AZURE_KEYVAULT_CLIENT_ID");
String clientSecret = PropertyConvertorUtils.getPropertyValue("AZURE_KEYVAULT_CLIENT_SECRET");
Expand All @@ -37,7 +33,7 @@ public void testGetAuthorizationToken() throws Exception {
aadAuthenticationUrl,
tenantId,
clientId,
URLEncoder.encode(clientSecret, "UTF-8")
clientSecret
);
assertNotNull(result);
}
Expand Down