Skip to content

Commit 4009b7a

Browse files
committed
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentication-library-for-java into avdunn/nimbus-grants
# Conflicts: # msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/TokenRequestExecutor.java
2 parents 1643890 + 54f3d44 commit 4009b7a

File tree

10 files changed

+59
-9
lines changed

10 files changed

+59
-9
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Quick links:
1616
The library supports the following Java environments:
1717
- Java 8 (or higher)
1818

19-
Current version - 1.20.0
19+
Current version - 1.20.1
2020

2121
You can find the changes for each version in the [change log](https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/main/msal4j-sdk/changelog.txt).
2222

@@ -28,13 +28,13 @@ Find [the latest package in the Maven repository](https://mvnrepository.com/arti
2828
<dependency>
2929
<groupId>com.microsoft.azure</groupId>
3030
<artifactId>msal4j</artifactId>
31-
<version>1.20.0</version>
31+
<version>1.20.1</version>
3232
</dependency>
3333
```
3434
### Gradle
3535

3636
```gradle
37-
implementation group: 'com.microsoft.azure', name: 'com.microsoft.aad.msal4j', version: '1.20.0'
37+
implementation group: 'com.microsoft.azure', name: 'com.microsoft.aad.msal4j', version: '1.20.1'
3838
```
3939

4040
## Usage

changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Version 1.20.1
2+
=============
3+
- Fix Base64URL decoding bug (#938)
4+
15
Version 1.20.0
26
=============
37
- Replace some usage of jackson-databind with azure-json (#918)

msal4j-sdk/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Quick links:
1616
The library supports the following Java environments:
1717
- Java 8 (or higher)
1818

19-
Current version - 1.20.0
19+
Current version - 1.20.1
2020

2121
You can find the changes for each version in the [change log](https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/master/changelog.txt).
2222

@@ -28,13 +28,13 @@ Find [the latest package in the Maven repository](https://mvnrepository.com/arti
2828
<dependency>
2929
<groupId>com.microsoft.azure</groupId>
3030
<artifactId>msal4j</artifactId>
31-
<version>1.20.0</version>
31+
<version>1.20.1</version>
3232
</dependency>
3333
```
3434
### Gradle
3535

3636
```gradle
37-
compile group: 'com.microsoft.azure', name: 'msal4j', version: '1.20.0'
37+
compile group: 'com.microsoft.azure', name: 'msal4j', version: '1.20.1'
3838
```
3939

4040
## Usage

msal4j-sdk/bnd.bnd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Export-Package: com.microsoft.aad.msal4j;version="1.20.0"
1+
Export-Package: com.microsoft.aad.msal4j;version="1.20.1"
22
Automatic-Module-Name: com.microsoft.aad.msal4j

msal4j-sdk/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.microsoft.azure</groupId>
55
<artifactId>msal4j</artifactId>
6-
<version>1.20.0</version>
6+
<version>1.20.1</version>
77
<packaging>jar</packaging>
88
<name>msal4j</name>
99
<description>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030
class DefaultHttpClientManagedIdentity extends DefaultHttpClient {
3131

32-
// CodeQL [SM03767] False positive: in addTrustedCertificateThumbprint() we create a TrustManager that only trusts a certificate with a specific thumbprint.
3332
public static final HostnameVerifier ALL_HOSTS_ACCEPT_HOSTNAME_VERIFIER = new HostnameVerifier() {
3433
@SuppressWarnings("BadHostnameVerifier")
3534
@Override
@@ -85,6 +84,8 @@ public static void addTrustedCertificateThumbprint(HttpsURLConnection httpsUrlCo
8584
String certificateThumbprint) {
8685
//We expect the connection to work against a specific server side certificate only, so it's safe to disable the
8786
// host name verification.
87+
88+
// CodeQL [SM03767] False positive: the TrustManager created later on will only trust a certificate with a specific thumbprint.
8889
if (httpsUrlConnection.getHostnameVerifier() != ALL_HOSTS_ACCEPT_HOSTNAME_VERIFIER) {
8990
httpsUrlConnection.setHostnameVerifier(ALL_HOSTS_ACCEPT_HOSTNAME_VERIFIER);
9091
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private AuthenticationResult createAuthenticationResultFromOauthHttpResponse(
124124
if (!StringHelper.isNullOrBlank(response.idToken())) {
125125
String idTokenJson;
126126
try {
127+
idTokenJson = new String(Base64.getUrlDecoder().decode(tokens.getIDTokenString().split("\\.")[1]), StandardCharsets.UTF_8);
127128
idTokenJson = new String(Base64.getDecoder().decode(response.idToken().split("\\.")[1]), StandardCharsets.UTF_8);
128129
} catch (ArrayIndexOutOfBoundsException e) {
129130
throw new MsalServiceException("Error parsing ID token, missing payload section. Ensure that the ID token is following the JWT format.",

msal4j-sdk/src/samples/msal-b2c-web-sample/src/main/java/com/microsoft/azure/msalwebsample/CookieHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static void removeStateNonceCookies(HttpServletResponse httpResponse){
3131

3232
Cookie stateCookie = new Cookie(MSAL_WEB_APP_STATE_COOKIE, "");
3333
stateCookie.setMaxAge(0);
34+
stateCookie.setSecure(true);
3435

3536
httpResponse.addCookie(stateCookie);
3637

msal4j-sdk/src/samples/msal-web-sample/src/main/java/com/microsoft/azure/msalwebsample/CookieHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static void removeStateNonceCookies(HttpServletResponse httpResponse){
3131

3232
Cookie stateCookie = new Cookie(MSAL_WEB_APP_STATE_COOKIE, "");
3333
stateCookie.setMaxAge(0);
34+
stateCookie.setSecure(true);
3435

3536
httpResponse.addCookie(stateCookie);
3637

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import java.net.URI;
2626
import java.net.URISyntaxException;
2727
import java.net.URL;
28+
import java.util.Base64;
2829
import java.util.Collections;
2930
import java.util.HashMap;
31+
import java.util.concurrent.ExecutionException;
3032

3133
@ExtendWith(MockitoExtension.class)
3234
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@@ -291,4 +293,44 @@ void testExecuteOAuth_Failure() throws SerializeException,
291293

292294
assertThrows(MsalException.class, request::executeTokenRequest);
293295
}
296+
297+
@Test
298+
void testBase64UrlEncoding() throws MalformedURLException, ExecutionException, InterruptedException {
299+
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
300+
ConfidentialClientApplication cca =
301+
ConfidentialClientApplication.builder("clientId", ClientCredentialFactory.createFromSecret("password"))
302+
.authority("https://login.microsoftonline.com/tenant/")
303+
.instanceDiscovery(false)
304+
.validateAuthority(false)
305+
.httpClient(httpClientMock)
306+
.build();
307+
308+
//ID token payloads are parsed to get certain info to create Account and AccountCacheEntity objects, and the library must decode them using a Base64URL decoder.
309+
HashMap<String, String> tokenParameters = new HashMap<>();
310+
tokenParameters.put("preferred_username", "~nameWith~specialChars");
311+
String encodedIDToken = TestHelper.createIdToken(tokenParameters);
312+
try {
313+
//TestHelper.createIdToken() should use Base64URL encoding, so first we prove that the encoded token it produces cannot be decoded with Base64 decoder
314+
Base64.getDecoder().decode(encodedIDToken.split("\\.")[1]);
315+
316+
fail("IllegalArgumentException was expected but not thrown.");
317+
} catch (IllegalArgumentException e) {
318+
//Encoded token should have some "-" characters in it
319+
assertTrue(e.getMessage().contains("Illegal base64 character 2d"));
320+
}
321+
322+
//Now, send that encoded token through the library's token request flow, which will decode it using a Base64URL decoder
323+
HashMap<String, String> responseParameters = new HashMap<>();
324+
responseParameters.put("id_token", encodedIDToken);
325+
responseParameters.put("access_token", "token");
326+
TestHelper.createTokenRequestMock(httpClientMock, TestHelper.getSuccessfulTokenResponse(responseParameters), 200);
327+
328+
OnBehalfOfParameters parameters = OnBehalfOfParameters.builder(Collections.singleton("someScopes"), new UserAssertion(TestHelper.signedAssertion)).build();
329+
IAuthenticationResult result = cca.acquireToken(parameters).get();
330+
331+
//Ensure that the name was successfully parsed out of the encoded ID token
332+
assertNotNull(result.idToken());
333+
assertNotNull(result.account());
334+
assertEquals("~nameWith~specialChars", result.account().username());
335+
}
294336
}

0 commit comments

Comments
 (0)