Skip to content

Commit 7213c0c

Browse files
authored
Merge pull request #266 from AzureAD/pesomka/extra_scopes
Pesomka/extra scopes
2 parents f7f85f3 + 22cb8a0 commit 7213c0c

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@
1010

1111
import java.net.MalformedURLException;
1212
import java.net.URL;
13-
import java.util.Arrays;
14-
import java.util.Collections;
15-
import java.util.HashMap;
16-
import java.util.List;
17-
import java.util.Map;
18-
import java.util.Set;
19-
import java.util.TreeSet;
13+
import java.util.*;
2014

2115
/**
2216
* Parameters for {@link AbstractClientApplicationBase#getAuthorizationRequestUrl(AuthorizationRequestUrlParameters)}
@@ -62,9 +56,15 @@ private AuthorizationRequestUrlParameters(Builder builder){
6256
requestParameters.put("redirect_uri", Collections.singletonList(this.redirectUri));
6357
this.scopes = builder.scopes;
6458

65-
Set<String> scopesParam = new TreeSet<>(builder.scopes);
6659
String[] commonScopes = AbstractMsalAuthorizationGrant.COMMON_SCOPES_PARAM.split(" ");
67-
scopesParam.addAll(Arrays.asList(commonScopes));
60+
61+
Set<String> scopesParam = new LinkedHashSet<>(Arrays.asList(commonScopes));
62+
63+
scopesParam.addAll(builder.scopes);
64+
65+
if(builder.extraScopesToConsent != null) {
66+
scopesParam.addAll(builder.extraScopesToConsent);
67+
}
6868

6969
this.scopes = scopesParam;
7070
requestParameters.put("scope", Collections.singletonList(String.join(" ", scopesParam)));
@@ -151,6 +151,7 @@ public static class Builder {
151151

152152
private String redirectUri;
153153
private Set<String> scopes;
154+
private Set<String> extraScopesToConsent;
154155
private Set<String> claims;
155156
private String claimsChallenge;
156157
private String codeChallenge;
@@ -188,6 +189,15 @@ public Builder scopes(Set<String> val){
188189
return self();
189190
}
190191

192+
/**
193+
* Scopes that you can request the end user to consent upfront,
194+
* in addition to scopes which the application is requesting access to.
195+
*/
196+
public Builder extraScopesToConsent(Set<String> val){
197+
this.extraScopesToConsent = val;
198+
return self();
199+
}
200+
191201
/**
192202
* In cases where Azure AD tenant admin has enabled conditional access policies, and the
193203
* policy has not been met,{@link MsalServiceException} will contain claims that need be

src/test/java/com/microsoft/aad/msal4j/AuthorizationRequestUrlParametersTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
import java.io.UnsupportedEncodingException;
1010
import java.net.URL;
1111
import java.net.URLDecoder;
12-
import java.util.Collections;
13-
import java.util.HashMap;
14-
import java.util.Map;
15-
import java.util.Set;
16-
import java.util.HashSet;
12+
import java.util.*;
1713

1814
public class AuthorizationRequestUrlParametersTest {
1915

@@ -57,7 +53,7 @@ public void testBuilder_onlyRequiredParameters() throws UnsupportedEncodingExcep
5753
URLDecoder.decode(pair.substring(idx+1), "UTF-8"));
5854
}
5955

60-
Assert.assertEquals(queryParameters.get("scope"), "offline_access openid profile scope");
56+
Assert.assertEquals(queryParameters.get("scope"), "openid profile offline_access scope");
6157
Assert.assertEquals(queryParameters.get("response_type"), "code");
6258
Assert.assertEquals(queryParameters.get("redirect_uri"), "http://localhost:8080");
6359
Assert.assertEquals(queryParameters.get("client_id"), "client_id");
@@ -89,6 +85,7 @@ public void testBuilder_optionalParameters() throws UnsupportedEncodingException
8985
AuthorizationRequestUrlParameters parameters =
9086
AuthorizationRequestUrlParameters
9187
.builder(redirectUri, scope)
88+
.extraScopesToConsent(new LinkedHashSet<>(Arrays.asList("extraScopeToConsent1", "extraScopeToConsent2")))
9289
.responseMode(ResponseMode.QUERY)
9390
.codeChallenge("challenge")
9491
.codeChallengeMethod("method")
@@ -114,7 +111,8 @@ public void testBuilder_optionalParameters() throws UnsupportedEncodingException
114111
URLDecoder.decode(pair.substring(idx+1), "UTF-8"));
115112
}
116113

117-
Assert.assertEquals(queryParameters.get("scope"), "offline_access openid profile scope");
114+
Assert.assertEquals(queryParameters.get("scope"),
115+
"openid profile offline_access scope extraScopeToConsent1 extraScopeToConsent2");
118116
Assert.assertEquals(queryParameters.get("response_type"), "code");
119117
Assert.assertEquals(queryParameters.get("redirect_uri"), "http://localhost:8080");
120118
Assert.assertEquals(queryParameters.get("client_id"), "client_id");

0 commit comments

Comments
 (0)