1111import org .springframework .security .oauth2 .client .web .OAuth2AuthorizedClientRepository ;
1212
1313import java .util .ArrayList ;
14+ import java .util .HashSet ;
1415import java .util .List ;
16+ import java .util .Set ;
1517
18+ import static com .azure .spring .aad .webapp .AADWebAppConfiguration .resourceServerCount ;
1619import static org .assertj .core .api .Assertions .assertThat ;
1720import static org .junit .jupiter .api .Assertions .assertEquals ;
1821import 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