Skip to content

Commit 700a850

Browse files
authored
Merge pull request #926 from AzureAD/avdunn/nimbus-grants
Remove usage of com.nimbusds.oauth2 from grant-related classes
2 parents 450d71a + cac03fc commit 700a850

26 files changed

+239
-238
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/integrationtest/java/infrastructure/SeleniumExtensions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static WebDriver createDefaultWebDriver() {
4040
//No visual rendering, remove to see browser window when debugging
4141
options.addArguments("--headless");
4242
//Add to avoid issues if your real browser's history/cookies are affecting tests, should not be needed in ADO pipelines
43-
//options.addArguments("--incognito");
43+
options.addArguments("--incognito");
4444

4545
System.setProperty("webdriver.chrome.driver", "C:/Windows/chromedriver.exe");
4646
ChromeDriver driver = new ChromeDriver(options);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
package com.microsoft.aad.msal4j;
55

6+
import java.util.HashSet;
67
import java.util.List;
78
import java.util.Map;
9+
import java.util.Set;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.Stream;
812

913
/**
1014
* Abstract class for an MSAL grant.
@@ -25,13 +29,12 @@ abstract class AbstractMsalAuthorizationGrant {
2529
static final String SCOPE_PROFILE = "profile";
2630
static final String SCOPE_OFFLINE_ACCESS = "offline_access";
2731

28-
static final String COMMON_SCOPES_PARAM = SCOPE_OPEN_ID + SCOPES_DELIMITER +
29-
SCOPE_PROFILE + SCOPES_DELIMITER +
30-
SCOPE_OFFLINE_ACCESS;
32+
static final Set<String> COMMON_SCOPES = Stream.of(SCOPE_OPEN_ID, SCOPE_PROFILE, SCOPE_OFFLINE_ACCESS)
33+
.collect(Collectors.toCollection(HashSet::new));
3134

32-
String scopes;
35+
Set<String> scopes;
3336

34-
String getScopes() {
37+
Set<String> getScopes() {
3538
return scopes;
3639
}
3740

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

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

66
import com.nimbusds.jose.util.Base64URL;
7-
import com.nimbusds.oauth2.sdk.AuthorizationGrant;
8-
import com.nimbusds.oauth2.sdk.ResourceOwnerPasswordCredentialsGrant;
9-
import com.nimbusds.oauth2.sdk.SAML2BearerGrant;
107

11-
import java.io.UnsupportedEncodingException;
128
import java.net.URLEncoder;
139
import java.nio.charset.StandardCharsets;
1410
import java.util.Base64;
11+
import java.util.Collections;
12+
import java.util.LinkedHashMap;
13+
import java.util.List;
14+
import java.util.Map;
1515

1616
class AcquireTokenByAuthorizationGrantSupplier extends AuthenticationResultSupplier {
1717

@@ -39,8 +39,7 @@ AuthenticationResult execute() throws Exception {
3939
}
4040

4141
if (authGrant instanceof OAuthAuthorizationGrant) {
42-
msalRequest.msalAuthorizationGrant =
43-
processPasswordGrant((OAuthAuthorizationGrant) authGrant);
42+
processPasswordGrant((OAuthAuthorizationGrant) authGrant);
4443
}
4544

4645
if (authGrant instanceof IntegratedWindowsAuthorizationGrant) {
@@ -74,58 +73,52 @@ private boolean IsUiRequiredCacheSupported() {
7473
clientApplication instanceof PublicClientApplication;
7574
}
7675

77-
private OAuthAuthorizationGrant processPasswordGrant(
78-
OAuthAuthorizationGrant authGrant) throws Exception {
79-
80-
if (!(authGrant.getAuthorizationGrant() instanceof ResourceOwnerPasswordCredentialsGrant)) {
81-
return authGrant;
82-
}
76+
private void processPasswordGrant(OAuthAuthorizationGrant authGrant) throws Exception {
8377

84-
if (msalRequest.application().authenticationAuthority.authorityType != AuthorityType.AAD) {
85-
return authGrant;
78+
//Additional processing is only needed if it's a password grant with an AAD authority
79+
if (!(authGrant.getParamValue(GrantConstants.GRANT_TYPE_PARAMETER).equals(GrantConstants.PASSWORD))
80+
|| msalRequest.application().authenticationAuthority.authorityType != AuthorityType.AAD) {
81+
return;
8682
}
8783

88-
ResourceOwnerPasswordCredentialsGrant grant =
89-
(ResourceOwnerPasswordCredentialsGrant) authGrant.getAuthorizationGrant();
90-
9184
UserDiscoveryResponse userDiscoveryResponse = UserDiscoveryRequest.execute(
92-
this.clientApplication.authenticationAuthority.getUserRealmEndpoint(grant.getUsername()),
85+
this.clientApplication.authenticationAuthority.getUserRealmEndpoint(authGrant.getParamValue(GrantConstants.USERNAME_PARAMETER)),
9386
msalRequest.headers().getReadonlyHeaderMap(),
9487
msalRequest.requestContext(),
9588
this.clientApplication.serviceBundle());
9689

9790
if (userDiscoveryResponse.isAccountFederated()) {
9891
WSTrustResponse response = WSTrustRequest.execute(
9992
userDiscoveryResponse.federationMetadataUrl(),
100-
grant.getUsername(),
101-
grant.getPassword().getValue(),
93+
authGrant.getParamValue(GrantConstants.USERNAME_PARAMETER),
94+
authGrant.getParamValue(GrantConstants.PASSWORD_PARAMETER),
10295
userDiscoveryResponse.cloudAudienceUrn(),
10396
msalRequest.requestContext(),
10497
this.clientApplication.serviceBundle(),
10598
this.clientApplication.logPii());
10699

107-
AuthorizationGrant updatedGrant = getSAMLAuthorizationGrant(response);
108-
109-
authGrant = new OAuthAuthorizationGrant(updatedGrant, authGrant.getParameters());
100+
authGrant.addAndReplaceParams(getSAMLAuthGrantParameters(response));
110101
}
111-
return authGrant;
112102
}
113103

114-
private AuthorizationGrant getSAMLAuthorizationGrant(WSTrustResponse response) throws UnsupportedEncodingException {
115-
AuthorizationGrant updatedGrant;
104+
private Map<String, List<String>> getSAMLAuthGrantParameters(WSTrustResponse response) {
105+
Map<String, List<String>> params = new LinkedHashMap<>();
106+
116107
if (response.isTokenSaml2()) {
117-
updatedGrant = new SAML2BearerGrant(new Base64URL(
118-
Base64.getEncoder().encodeToString(response.getToken().getBytes(StandardCharsets.UTF_8))));
108+
params.put(GrantConstants.GRANT_TYPE_PARAMETER, Collections.singletonList(GrantConstants.SAML_2_BEARER));
119109
} else {
120-
updatedGrant = new SAML11BearerGrant(new Base64URL(
121-
Base64.getEncoder().encodeToString(response.getToken()
122-
.getBytes(StandardCharsets.UTF_8))));
110+
params.put(GrantConstants.GRANT_TYPE_PARAMETER, Collections.singletonList(GrantConstants.SAML_1_1_BEARER));
123111
}
124-
return updatedGrant;
112+
113+
params.put(GrantConstants.ASSERTION_PARAMETER, Collections.singletonList(new Base64URL(
114+
Base64.getEncoder().encodeToString(response.getToken()
115+
.getBytes(StandardCharsets.UTF_8))).toString()));
116+
117+
return params;
125118
}
126119

127-
private AuthorizationGrant getAuthorizationGrantIntegrated(String userName) throws Exception {
128-
AuthorizationGrant updatedGrant;
120+
private Map<String, List<String>> getAuthorizationGrantIntegrated(String userName) throws Exception {
121+
Map<String, List<String>> params;
129122

130123
String userRealmEndpoint = this.clientApplication.authenticationAuthority.
131124
getUserRealmEndpoint(URLEncoder.encode(userName, StandardCharsets.UTF_8.name()));
@@ -152,7 +145,7 @@ private AuthorizationGrant getAuthorizationGrantIntegrated(String userName) thro
152145
this.clientApplication.serviceBundle(),
153146
this.clientApplication.logPii());
154147

155-
updatedGrant = getSAMLAuthorizationGrant(wsTrustResponse);
148+
params = getSAMLAuthGrantParameters(wsTrustResponse);
156149
} else if (userRealmResponse.isAccountManaged()) {
157150
throw new MsalClientException(
158151
"Password is required for managed user",
@@ -163,6 +156,6 @@ private AuthorizationGrant getAuthorizationGrantIntegrated(String userName) thro
163156
AuthenticationErrorCode.USER_REALM_DISCOVERY_FAILED);
164157
}
165158

166-
return updatedGrant;
159+
return params;
167160
}
168161
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
import com.nimbusds.oauth2.sdk.AuthorizationCode;
7-
import com.nimbusds.oauth2.sdk.AuthorizationCodeGrant;
8-
import com.nimbusds.oauth2.sdk.AuthorizationGrant;
9-
import com.nimbusds.oauth2.sdk.pkce.CodeVerifier;
6+
import java.util.Collections;
7+
import java.util.LinkedHashMap;
8+
import java.util.List;
9+
import java.util.Map;
1010

1111
class AuthorizationCodeRequest extends MsalRequest {
1212

@@ -17,19 +17,19 @@ class AuthorizationCodeRequest extends MsalRequest {
1717
}
1818

1919
private static AbstractMsalAuthorizationGrant createMsalGrant(AuthorizationCodeParameters parameters) {
20+
Map<String, List<String>> params = new LinkedHashMap<>();
21+
22+
params.put(GrantConstants.GRANT_TYPE_PARAMETER, Collections.singletonList(GrantConstants.AUTHORIZATION_CODE));
23+
params.put("code", Collections.singletonList(parameters.authorizationCode()));
24+
25+
if (parameters.redirectUri() != null) {
26+
params.put("redirect_uri", Collections.singletonList(parameters.redirectUri().toString()));
27+
}
2028

21-
AuthorizationGrant authorizationGrant;
2229
if (parameters.codeVerifier() != null) {
23-
authorizationGrant = new AuthorizationCodeGrant(
24-
new AuthorizationCode(parameters.authorizationCode()),
25-
parameters.redirectUri(),
26-
new CodeVerifier(parameters.codeVerifier()));
27-
28-
} else {
29-
authorizationGrant = new AuthorizationCodeGrant(
30-
new AuthorizationCode(parameters.authorizationCode()), parameters.redirectUri());
30+
params.put("code_verifier", Collections.singletonList(parameters.codeVerifier()));
3131
}
3232

33-
return new OAuthAuthorizationGrant(authorizationGrant, parameters.scopes(), parameters.claims());
33+
return new OAuthAuthorizationGrant(params, parameters.scopes(), parameters.claims());
3434
}
3535
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ private AuthorizationRequestUrlParameters(Builder builder) {
6161
requestParameters.put("redirect_uri", Collections.singletonList(this.redirectUri));
6262
this.scopes = builder.scopes;
6363

64-
String[] commonScopes = AbstractMsalAuthorizationGrant.COMMON_SCOPES_PARAM.split(" ");
65-
66-
Set<String> scopesParam = new LinkedHashSet<>(Arrays.asList(commonScopes));
64+
Set<String> scopesParam = new LinkedHashSet<>(AbstractMsalAuthorizationGrant.COMMON_SCOPES);
6765

6866
scopesParam.addAll(builder.scopes);
6967

0 commit comments

Comments
 (0)