Skip to content

Commit eb0d42f

Browse files
authored
Fix AccessTokenUtil does not url encode its parameters (#40697)
* Fix bug: clientSecret not url encoded.
1 parent eda5ef1 commit eb0d42f

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

sdk/keyvault/azure-security-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/implementation/utils/AccessTokenUtil.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import static java.util.logging.Level.INFO;
77

88
import com.azure.security.keyvault.jca.implementation.model.AccessToken;
9+
10+
import java.io.UnsupportedEncodingException;
11+
import java.net.URLEncoder;
912
import java.util.HashMap;
1013
import java.util.logging.Logger;
1114

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

100103
StringBuilder requestBody = new StringBuilder();
101-
requestBody.append(GRANT_TYPE_FRAGMENT)
102-
.append(CLIENT_ID_FRAGMENT).append(clientId)
103-
.append(CLIENT_SECRET_FRAGMENT).append(clientSecret)
104-
.append(RESOURCE_FRAGMENT).append(resource);
104+
try {
105+
requestBody.append(GRANT_TYPE_FRAGMENT)
106+
.append(CLIENT_ID_FRAGMENT).append(clientId)
107+
.append(CLIENT_SECRET_FRAGMENT).append(URLEncoder.encode(clientSecret, "UTF-8"))
108+
.append(RESOURCE_FRAGMENT).append(resource);
109+
} catch (UnsupportedEncodingException e) {
110+
LOGGER.warning("Failed to construct requestBody");
111+
}
105112

106113
String body = HttpUtil
107114
.post(oauth2Url.toString(), requestBody.toString(), "application/x-www-form-urlencoded");

sdk/keyvault/azure-security-keyvault-jca/src/test/java/com/azure/security/keyvault/jca/AccessTokenUtilTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import org.junit.jupiter.api.Test;
99
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
1010

11-
import java.net.URLEncoder;
12-
1311
import static org.junit.jupiter.api.Assertions.assertNotNull;
1412

1513
/**
@@ -20,11 +18,9 @@ public class AccessTokenUtilTest {
2018

2119
/**
2220
* Test getAuthorizationToken method.
23-
*
24-
* @throws Exception when a serious error occurs.
2521
*/
2622
@Test
27-
public void testGetAuthorizationToken() throws Exception {
23+
public void testGetAuthorizationToken() {
2824
String tenantId = PropertyConvertorUtils.getPropertyValue("AZURE_KEYVAULT_TENANT_ID");
2925
String clientId = PropertyConvertorUtils.getPropertyValue("AZURE_KEYVAULT_CLIENT_ID");
3026
String clientSecret = PropertyConvertorUtils.getPropertyValue("AZURE_KEYVAULT_CLIENT_SECRET");
@@ -37,7 +33,7 @@ public void testGetAuthorizationToken() throws Exception {
3733
aadAuthenticationUrl,
3834
tenantId,
3935
clientId,
40-
URLEncoder.encode(clientSecret, "UTF-8")
36+
clientSecret
4137
);
4238
assertNotNull(result);
4339
}

0 commit comments

Comments
 (0)