Skip to content

Commit 1643890

Browse files
committed
PR feedback, and correctly adjust parameters in ADFS username/password scenarios
1 parent 7580409 commit 1643890

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

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/AcquireTokenByAuthorizationGrantSupplier.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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,39 +73,32 @@ private boolean IsUiRequiredCacheSupported() {
7473
clientApplication instanceof PublicClientApplication;
7574
}
7675

77-
private OAuthAuthorizationGrant processPasswordGrant(
78-
OAuthAuthorizationGrant authGrant) throws Exception {
79-
80-
if (!(authGrant.toParameters().get(GrantConstants.GRANT_TYPE_PARAMETER).get(0).equals(GrantConstants.PASSWORD))) {
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 a non-AAD authority
79+
if (!(authGrant.getParamValue(GrantConstants.GRANT_TYPE_PARAMETER).equals(GrantConstants.PASSWORD))
80+
|| msalRequest.application().authenticationAuthority.authorityType != AuthorityType.AAD) {
81+
return;
8682
}
8783

8884
UserDiscoveryResponse userDiscoveryResponse = UserDiscoveryRequest.execute(
89-
this.clientApplication.authenticationAuthority.getUserRealmEndpoint(authGrant.toParameters().get("username").get(0)),
85+
this.clientApplication.authenticationAuthority.getUserRealmEndpoint(authGrant.getParamValue(GrantConstants.USERNAME_PARAMETER)),
9086
msalRequest.headers().getReadonlyHeaderMap(),
9187
msalRequest.requestContext(),
9288
this.clientApplication.serviceBundle());
9389

9490
if (userDiscoveryResponse.isAccountFederated()) {
9591
WSTrustResponse response = WSTrustRequest.execute(
9692
userDiscoveryResponse.federationMetadataUrl(),
97-
authGrant.toParameters().get(GrantConstants.USERNAME_PARAMETER).get(0),
98-
authGrant.toParameters().get(GrantConstants.PASSWORD_PARAMETER).get(0),
93+
authGrant.getParamValue(GrantConstants.USERNAME_PARAMETER),
94+
authGrant.getParamValue(GrantConstants.PASSWORD_PARAMETER),
9995
userDiscoveryResponse.cloudAudienceUrn(),
10096
msalRequest.requestContext(),
10197
this.clientApplication.serviceBundle(),
10298
this.clientApplication.logPii());
10399

104-
Map<String, List<String>> params = getSAMLAuthGrantParameters(response);
105-
params.putAll(authGrant.toParameters());
106-
107-
authGrant = new OAuthAuthorizationGrant(params, null);
100+
authGrant.addAndReplaceParams(getSAMLAuthGrantParameters(response));
108101
}
109-
return authGrant;
110102
}
111103

112104
private Map<String, List<String>> getSAMLAuthGrantParameters(WSTrustResponse response) {

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,35 @@ class OAuthAuthorizationGrant extends AbstractMsalAuthorizationGrant {
3737
}
3838
}
3939

40-
/**
41-
* Constructor to create an OAuthAuthorizationGrant
42-
*
43-
* @param params parameters relevant for the specific authorization grant type
44-
* @param scopes additional scopes which will be added to a default set of common scopes
45-
* @param claims optional claims
46-
*/
47-
OAuthAuthorizationGrant(Map<String, List<String>> params, Set<String> scopes, ClaimsRequest claims) {
48-
this(params, scopes);
49-
50-
if (claims != null) {
51-
this.claims = claims;
52-
this.params.put("claims", Collections.singletonList(claims.formatAsJSONString()));
53-
}
40+
/**
41+
* Constructor to create an OAuthAuthorizationGrant
42+
*
43+
* @param params parameters relevant for the specific authorization grant type
44+
* @param scopes additional scopes which will be added to a default set of common scopes
45+
* @param claims optional claims
46+
*/
47+
OAuthAuthorizationGrant(Map<String, List<String>> params, Set<String> scopes, ClaimsRequest claims) {
48+
this(params, scopes);
49+
50+
if (claims != null) {
51+
this.claims = claims;
52+
this.params.put("claims", Collections.singletonList(claims.formatAsJSONString()));
53+
}
54+
}
55+
56+
void addAndReplaceParams(Map<String, List<String>> params) {
57+
if (params != null) {
58+
//putAll() will overwrite existing values if the key already exists in the map
59+
this.params.putAll(params);
5460
}
61+
}
62+
63+
String getParamValue(String paramKey) {
64+
if (this.params.containsKey(paramKey)) {
65+
return this.params.get(paramKey).get(0);
66+
}
67+
return null;
68+
}
5569

5670
/**
5771
* Returns an unmodifiable version of the parameters map

0 commit comments

Comments
 (0)