-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug: Can't change userNameAttributeName for azure sso. (#20340)
- Loading branch information
Rujun Chen
authored
Apr 7, 2021
1 parent
1ead73b
commit 4c200a4
Showing
9 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
sdk/spring/azure-spring-boot-starter-active-directory/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...test/java/com/azure/test/aad/resource/server/AADWeiResourceServerUserNameAttributeIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.test.aad.resource.server; | ||
|
||
import com.azure.spring.test.aad.AADWebApiITHelper; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.security.Principal; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.azure.spring.test.Constant.MULTI_TENANT_SCOPE_GRAPH_READ; | ||
import static com.azure.spring.test.EnvironmentVariable.AAD_MULTI_TENANT_CLIENT_ID; | ||
import static com.azure.spring.test.EnvironmentVariable.AAD_MULTI_TENANT_CLIENT_SECRET; | ||
import static com.azure.spring.test.EnvironmentVariable.AAD_USER_NAME_1; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class AADWeiResourceServerUserNameAttributeIT { | ||
|
||
private AADWebApiITHelper aadWebApiITHelper; | ||
|
||
@Before | ||
public void init() { | ||
Map<String, String> properties = new HashMap<>(); | ||
properties.put("azure.activedirectory.client-id", AAD_MULTI_TENANT_CLIENT_ID); | ||
properties.put("azure.activedirectory.client-secret", AAD_MULTI_TENANT_CLIENT_SECRET); | ||
properties.put("azure.activedirectory.app-id-uri", "api://" + AAD_MULTI_TENANT_CLIENT_ID); | ||
aadWebApiITHelper = new AADWebApiITHelper( | ||
DumbApp.class, | ||
properties, | ||
AAD_MULTI_TENANT_CLIENT_ID, | ||
AAD_MULTI_TENANT_CLIENT_SECRET, | ||
Collections.singletonList(MULTI_TENANT_SCOPE_GRAPH_READ)); | ||
} | ||
|
||
@Test | ||
public void testPrincipalName() { | ||
assertEquals(aadWebApiITHelper.httpGetStringByAccessToken("principalName"), AAD_USER_NAME_1); | ||
} | ||
|
||
@EnableWebSecurity | ||
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) | ||
@SpringBootApplication | ||
@RestController | ||
public static class DumbApp extends WebSecurityConfigurerAdapter { | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
http.authorizeRequests() | ||
.anyRequest().authenticated() | ||
.and() | ||
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); | ||
} | ||
|
||
@GetMapping(value = "/principalName") | ||
public ResponseEntity<String> home(Principal principal) { | ||
return ResponseEntity.ok(principal.getName()); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...ad/src/test/java/com/azure/test/aad/selenium/user/name/attribute/UserNameAttributeIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.test.aad.selenium.user.name.attribute; | ||
|
||
import com.azure.test.aad.selenium.AADSeleniumITHelper; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.security.Principal; | ||
import java.util.Map; | ||
|
||
import static com.azure.spring.test.EnvironmentVariable.AAD_SINGLE_TENANT_CLIENT_ID_WITH_ROLE; | ||
import static com.azure.spring.test.EnvironmentVariable.AAD_SINGLE_TENANT_CLIENT_SECRET_WITH_ROLE; | ||
import static com.azure.spring.test.EnvironmentVariable.AAD_USER_NAME_1; | ||
import static com.azure.spring.test.EnvironmentVariable.AAD_USER_PASSWORD_1; | ||
import static com.azure.test.aad.selenium.AADSeleniumITHelper.createDefaultProperties; | ||
|
||
public class UserNameAttributeIT { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(UserNameAttributeIT.class); | ||
private AADSeleniumITHelper aadSeleniumITHelper; | ||
|
||
@Test | ||
public void roleTest() { | ||
Map<String, String> properties = createDefaultProperties(); | ||
properties.put("azure.activedirectory.client-id", AAD_SINGLE_TENANT_CLIENT_ID_WITH_ROLE); | ||
properties.put("azure.activedirectory.client-secret", AAD_SINGLE_TENANT_CLIENT_SECRET_WITH_ROLE); | ||
aadSeleniumITHelper = new AADSeleniumITHelper(DumbApp.class, properties, AAD_USER_NAME_1, AAD_USER_PASSWORD_1); | ||
aadSeleniumITHelper.logIn(); | ||
String httpResponse = aadSeleniumITHelper.httpGet("api/principalName"); | ||
Assert.assertTrue(httpResponse.contains(AAD_USER_NAME_1)); | ||
} | ||
|
||
@After | ||
public void destroy() { | ||
aadSeleniumITHelper.destroy(); | ||
} | ||
|
||
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) | ||
@SpringBootApplication | ||
@RestController | ||
public static class DumbApp { | ||
|
||
@GetMapping(value = "/api/principalName") | ||
public ResponseEntity<String> home(Principal principal) { | ||
String principalName = principal.getName(); | ||
LOGGER.info(principalName); | ||
return ResponseEntity.ok(principalName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters