Skip to content

Commit 38bd24a

Browse files
gavingoldenjgrandja
authored andcommitted
Add support for custom username claim
Fixes spring-atticgh-1696
1 parent 849ae23 commit 38bd24a

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/token/DefaultUserAuthenticationConverter.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class DefaultUserAuthenticationConverter implements UserAuthenticationCon
4242

4343
private UserDetailsService userDetailsService;
4444

45+
private String userClaimName = USERNAME;
46+
4547
/**
4648
* Optional {@link UserDetailsService} to use when extracting an {@link Authentication} from the incoming map.
4749
*
@@ -51,6 +53,15 @@ public void setUserDetailsService(UserDetailsService userDetailsService) {
5153
this.userDetailsService = userDetailsService;
5254
}
5355

56+
/**
57+
* Set the name of the user claim to use when extracting an {@link Authentication} from the incoming map
58+
* or when converting an {@link Authentication} to a map.
59+
* @param claimName the claim name to use (default {@link UserAuthenticationConverter#USERNAME})
60+
*/
61+
public void setUserClaimName(String claimName) {
62+
this.userClaimName = claimName;
63+
}
64+
5465
/**
5566
* Default value for authorities if an Authentication is being created and the input has no data for authorities.
5667
* Note that unless this property is set, the default Authentication created by {@link #extractAuthentication(Map)}
@@ -65,19 +76,19 @@ public void setDefaultAuthorities(String[] defaultAuthorities) {
6576

6677
public Map<String, ?> convertUserAuthentication(Authentication authentication) {
6778
Map<String, Object> response = new LinkedHashMap<String, Object>();
68-
response.put(USERNAME, authentication.getName());
79+
response.put(userClaimName, authentication.getName());
6980
if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
7081
response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
7182
}
7283
return response;
7384
}
7485

7586
public Authentication extractAuthentication(Map<String, ?> map) {
76-
if (map.containsKey(USERNAME)) {
77-
Object principal = map.get(USERNAME);
87+
if (map.containsKey(userClaimName)) {
88+
Object principal = map.get(userClaimName);
7889
Collection<? extends GrantedAuthority> authorities = getAuthorities(map);
7990
if (userDetailsService != null) {
80-
UserDetails user = userDetailsService.loadUserByUsername((String) map.get(USERNAME));
91+
UserDetails user = userDetailsService.loadUserByUsername((String) map.get(userClaimName));
8192
authorities = user.getAuthorities();
8293
principal = user;
8394
}

spring-security-oauth2/src/test/java/org/springframework/security/oauth2/provider/token/DefaultUserAuthenticationConverterTests.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.junit.Test;
1010
import org.mockito.Mockito;
11+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
1112
import org.springframework.security.core.Authentication;
1213
import org.springframework.security.core.authority.AuthorityUtils;
1314
import org.springframework.security.core.userdetails.User;
@@ -59,4 +60,50 @@ public void shouldExtractAuthenticationWhenUserDetailsProvided() throws Exceptio
5960

6061
assertEquals("ROLE_SPAM", authentication.getAuthorities().iterator().next().toString());
6162
}
63+
64+
@Test
65+
public void shouldExtractWithDefaultUsernameClaimWhenNotSet() throws Exception {
66+
Map<String, Object> map = new HashMap<String, Object>();
67+
map.put(UserAuthenticationConverter.USERNAME, "test_user");
68+
69+
Authentication authentication = converter.extractAuthentication(map);
70+
71+
assertEquals("test_user", authentication.getPrincipal());
72+
}
73+
74+
@Test
75+
public void shouldConvertUserWithDefaultUsernameClaimWhenNotSet() throws Exception {
76+
Authentication authentication = new UsernamePasswordAuthenticationToken("test_user", "");
77+
78+
Map<String, ?> map = converter.convertUserAuthentication(authentication);
79+
80+
assertEquals("test_user", map.get(UserAuthenticationConverter.USERNAME));
81+
}
82+
83+
@Test
84+
public void shouldExtractWithCustomUsernameClaimWhenSet() throws Exception {
85+
String customUserClaim = "custom_user_name";
86+
DefaultUserAuthenticationConverter converter = new DefaultUserAuthenticationConverter();
87+
converter.setUserClaimName(customUserClaim);
88+
89+
Map<String, Object> map = new HashMap<String, Object>();
90+
map.put(customUserClaim, "test_user");
91+
92+
Authentication authentication = converter.extractAuthentication(map);
93+
94+
assertEquals("test_user", authentication.getPrincipal());
95+
}
96+
97+
@Test
98+
public void shouldConvertUserWithCustomUsernameClaimWhenSet() throws Exception {
99+
String customUserClaim = "custom_user_name";
100+
DefaultUserAuthenticationConverter converter = new DefaultUserAuthenticationConverter();
101+
converter.setUserClaimName(customUserClaim);
102+
103+
Authentication authentication = new UsernamePasswordAuthenticationToken("test_user", "");
104+
105+
Map<String, ?> map = converter.convertUserAuthentication(authentication);
106+
107+
assertEquals("test_user", map.get(customUserClaim));
108+
}
62109
}

0 commit comments

Comments
 (0)