Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.logging.Logger;

Expand Down Expand Up @@ -98,10 +101,14 @@ public static AccessToken getAccessToken(String resource, String aadAuthenticati
.append(OAUTH2_TOKEN_POSTFIX);

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);
try {
requestBody.append(GRANT_TYPE_FRAGMENT)
.append(CLIENT_ID_FRAGMENT).append(URLEncoder.encode(clientId, "UTF-8"))
.append(CLIENT_SECRET_FRAGMENT).append(URLEncoder.encode(clientSecret, "UTF-8"))
.append(RESOURCE_FRAGMENT).append(URLEncoder.encode(resource, "UTF-8"));
} catch (UnsupportedEncodingException e) {
LOGGER.warning("Failed to construct requestBody");
}

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