Skip to content

Commit c678825

Browse files
authored
Add property enable-full-list (Azure#21080)
* Enable full list
1 parent 1a50a0d commit c678825

File tree

6 files changed

+55
-2
lines changed

6 files changed

+55
-2
lines changed

sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-active-directory-webapp/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ azure:
8181
user-group:
8282
allowed-group-names: group1,group2
8383
allowed-group-ids: <group1-id>,<group2-id>
84+
enable-full-list: false
8485
post-logout-redirect-uri: http://localhost:8080
8586
authorization-clients:
8687
arm:
@@ -94,6 +95,8 @@ azure:
9495
# scopes:
9596
# - <Web-API-A-app-id-url>/Obo.WebApiA.ExampleScope
9697
98+
# enable-full-list is used to control whether to list all group id, default is false
99+
97100
# It's suggested the logged in user should at least belong to one of the above groups
98101
# If not, the logged in user will not be able to access any authorization controller rest APIs
99102
```

sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-active-directory-webapp/src/main/resources/application.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# WebapiA is an optional client, we can access obo resource servers.
2+
# We can also access a custom server according to the webapiA client.
3+
14
azure:
25
activedirectory:
36
client-id: <client-id>
@@ -6,6 +9,7 @@ azure:
69
user-group:
710
allowed-group-names: group1,group2
811
allowed-group-ids: <group1-id>,<group2-id>
12+
enable-full-list: false
913
post-logout-redirect-uri: http://localhost:8080
1014
authorization-clients:
1115
arm:

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ Set<String> extractGroupRolesFromAccessToken(OAuth2AccessToken accessToken) {
119119
.map(AbstractOAuth2Token::getTokenValue)
120120
.map(graphClient::getGroupsFromGraph)
121121
.orElseGet(Collections::emptySet);
122+
if (properties.getUserGroup().getEnableFullList()) {
123+
return groups;
124+
}
122125
Set<String> roles = Arrays
123126
.asList(properties.getUserGroup().getAllowedGroupIds(), properties.getUserGroup().getAllowedGroupNames())
124127
.stream()

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ public Set<String> getGroupsFromGraph(String accessToken) {
5454
memberships.getValue()
5555
.stream()
5656
.filter(this::isGroupObject)
57-
.map(membership -> Arrays.asList(membership.getDisplayName(), membership.getObjectID()))
58-
.forEach(groups::addAll);
57+
.map(membership -> {
58+
if (properties.getUserGroup().getEnableFullList()) {
59+
return Arrays.asList(membership.getObjectID());
60+
} else {
61+
return Arrays.asList(membership.getDisplayName(), membership.getObjectID());
62+
}
63+
}).forEach(groups::addAll);
5964
aadMembershipRestUri = Optional.of(memberships)
6065
.map(Memberships::getOdataNextLink)
6166
.orElse(null);

sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/aad/AADAuthenticationProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ public static class UserGroupProperties {
145145

146146
private Set<String> allowedGroupIds = new HashSet<>();
147147

148+
/**
149+
* enableFullList is used to control whether to list all group id, default is false
150+
*/
151+
private Boolean enableFullList = false;
152+
148153
public Set<String> getAllowedGroupIds() {
149154
return allowedGroupIds;
150155
}
@@ -161,6 +166,14 @@ public void setAllowedGroupNames(List<String> allowedGroupNames) {
161166
this.allowedGroupNames = allowedGroupNames;
162167
}
163168

169+
public Boolean getEnableFullList() {
170+
return enableFullList;
171+
}
172+
173+
public void setEnableFullList(Boolean enableFullList) {
174+
this.enableFullList = enableFullList;
175+
}
176+
164177
@Deprecated
165178
@DeprecatedConfigurationProperty(
166179
reason = "In order to distinguish between allowed-group-ids and allowed-group-names, set allowed-groups "

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,29 @@ public void testGroupsNameAndGroupsId() {
8181
assertThat(groupsName).doesNotContain("ROLE_d07c0bd6-4aab-45ac-b87c-23e8d00194abaaa");
8282
assertThat(groupsName).hasSize(2);
8383
}
84+
85+
@Test
86+
public void testEnableFullList() {
87+
when(properties.getUserGroup().getEnableFullList()).thenReturn(true);
88+
Set<String> groupIds = userService.extractGroupRolesFromAccessToken(accessToken);
89+
assertThat(groupIds).hasSize(4);
90+
}
91+
92+
@Test
93+
public void testDisableFullList() {
94+
when(properties.getUserGroup().getEnableFullList()).thenReturn(false);
95+
Set<String> allowedGroupIds = new HashSet<>();
96+
allowedGroupIds.add("d07c0bd6-4aab-45ac-b87c-23e8d00194ab");
97+
when(userGroup.getAllowedGroupIds()).thenReturn(allowedGroupIds);
98+
List<String> allowedGroupNames = new ArrayList<>();
99+
allowedGroupNames.add("group1");
100+
when(userGroup.getAllowedGroupNames()).thenReturn(allowedGroupNames);
101+
102+
Set<String> groupsName = userService.extractGroupRolesFromAccessToken(accessToken);
103+
assertThat(groupsName).contains("ROLE_group1");
104+
assertThat(groupsName).doesNotContain("ROLE_group5");
105+
assertThat(groupsName).contains("ROLE_d07c0bd6-4aab-45ac-b87c-23e8d00194ab");
106+
assertThat(groupsName).doesNotContain("ROLE_d07c0bd6-4aab-45ac-b87c-23e8d00194abaaa");
107+
assertThat(groupsName).hasSize(2);
108+
}
84109
}

0 commit comments

Comments
 (0)