Skip to content

Commit dc8f3e1

Browse files
author
Rujun Chen
authored
Add test: haveResourceServerScopeInAccessTokenWhenThereAreMultiResourceServerScopesInAuthCode (Azure#18407)
* Add test: haveResourceServerScopeInAccessTokenWhenThereAreMultiResourceServerScopesInAuthCode
1 parent b6ecf17 commit dc8f3e1

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AADWebAppConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ private AzureClientRegistration createDefaultClient() {
8686
return new AzureClientRegistration(client, accessTokenScopes);
8787
}
8888

89-
private int resourceServerCount(Set<String> scopes) {
89+
public static int resourceServerCount(Set<String> scopes) {
9090
return (int) scopes.stream()
91-
.filter(scope -> scope.startsWith("http"))
91+
.filter(scope -> scope.contains("/"))
92+
.map(scope -> scope.substring(0, scope.lastIndexOf('/')))
93+
.distinct()
9294
.count();
9395
}
9496

sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/aad/webapp/AADWebAppConfigurationTest.java

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
1212

1313
import java.util.ArrayList;
14+
import java.util.HashSet;
1415
import java.util.List;
16+
import java.util.Set;
1517

18+
import static com.azure.spring.aad.webapp.AADWebAppConfiguration.resourceServerCount;
1619
import static org.assertj.core.api.Assertions.assertThat;
1720
import static org.junit.jupiter.api.Assertions.assertEquals;
1821
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -206,12 +209,12 @@ public void clientRequiresOnDemandPermissions() {
206209

207210
@Test
208211
public void groupConfiguration() {
209-
WebApplicationContextRunnerUtils.getContextRunnerWithRequiredProperties()
210-
.withPropertyValues(
211-
"azure.activedirectory.user-group.allowed-groups = group1, group2"
212-
)
212+
WebApplicationContextRunnerUtils
213+
.getContextRunnerWithRequiredProperties()
214+
.withPropertyValues("azure.activedirectory.user-group.allowed-groups = group1, group2")
213215
.run(context -> {
214-
AADWebAppClientRegistrationRepository clientRepo = context.getBean(AADWebAppClientRegistrationRepository.class);
216+
AADWebAppClientRegistrationRepository clientRepo =
217+
context.getBean(AADWebAppClientRegistrationRepository.class);
215218
assertDefaultScopes(
216219
clientRepo.getAzureClient(),
217220
"openid", "profile", "https://graph.microsoft.com/User.Read",
@@ -220,6 +223,48 @@ public void groupConfiguration() {
220223
});
221224
}
222225

226+
@Test
227+
public void haveResourceServerScopeInAccessTokenWhenThereAreMultiResourceServerScopesInAuthCode() {
228+
WebApplicationContextRunnerUtils
229+
.getContextRunnerWithRequiredProperties()
230+
.withPropertyValues(
231+
"azure.activedirectory.authorization-clients.office.scopes ="
232+
+ " https://manage.office.com/ActivityFeed.Read",
233+
"azure.activedirectory.authorization-clients.arm.scopes = "
234+
+ "https://management.core.windows.net/user_impersonation"
235+
)
236+
.run(context -> {
237+
AADWebAppClientRegistrationRepository repo =
238+
context.getBean(AADWebAppClientRegistrationRepository.class);
239+
AzureClientRegistration azure = repo.getAzureClient();
240+
assertNotNull(azure);
241+
int resourceServerCountInAuthCode = resourceServerCount(azure.getClient().getScopes());
242+
assertTrue(resourceServerCountInAuthCode > 1);
243+
int resourceServerCountInAccessToken = resourceServerCount(azure.getAccessTokenScopes());
244+
assertTrue(resourceServerCountInAccessToken != 0);
245+
});
246+
}
247+
248+
@Test
249+
public void resourceServerCountTest() {
250+
Set<String> scopes = new HashSet<>();
251+
assertEquals(resourceServerCount(scopes), 0);
252+
scopes.add("openid");
253+
scopes.add("profile");
254+
scopes.add("offline_access");
255+
assertEquals(resourceServerCount(scopes), 0);
256+
scopes.add("https://graph.microsoft.com/User.Read");
257+
assertEquals(resourceServerCount(scopes), 1);
258+
scopes.add("https://graph.microsoft.com/Directory.AccessAsUser.All");
259+
assertEquals(resourceServerCount(scopes), 1);
260+
scopes.add("https://manage.office.com/ActivityFeed.Read");
261+
assertEquals(resourceServerCount(scopes), 2);
262+
scopes.add("https://manage.office.com/ActivityFeed.ReadDlp");
263+
assertEquals(resourceServerCount(scopes), 2);
264+
scopes.add("https://manage.office.com/ServiceHealth.Read");
265+
assertEquals(resourceServerCount(scopes), 2);
266+
}
267+
223268
private void assertDefaultScopes(ClientRegistration client, String... scopes) {
224269
assertEquals(scopes.length, client.getScopes().size());
225270
for (String s : scopes) {

0 commit comments

Comments
 (0)