Skip to content

Added setPrincipalClaimName to JwtAuthenticationConverter #8318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,30 @@
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtClaimNames;
import org.springframework.util.Assert;

/**
* @author Rob Winch
* @author Josh Cummings
* @author Evgeniy Cheban
* @since 5.1
*/
public class JwtAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken> {
private Converter<Jwt, Collection<GrantedAuthority>> jwtGrantedAuthoritiesConverter
= new JwtGrantedAuthoritiesConverter();

private String principalClaimName;

@Override
public final AbstractAuthenticationToken convert(Jwt jwt) {
Collection<GrantedAuthority> authorities = extractAuthorities(jwt);
return new JwtAuthenticationToken(jwt, authorities);
if (this.principalClaimName == null) {
return new JwtAuthenticationToken(jwt, authorities);
}

String name = jwt.getClaim(this.principalClaimName);
return new JwtAuthenticationToken(jwt, authorities, name);
}

/**
Expand Down Expand Up @@ -65,4 +74,16 @@ public void setJwtGrantedAuthoritiesConverter(Converter<Jwt, Collection<GrantedA
Assert.notNull(jwtGrantedAuthoritiesConverter, "jwtGrantedAuthoritiesConverter cannot be null");
this.jwtGrantedAuthoritiesConverter = jwtGrantedAuthoritiesConverter;
}

/**
* Sets the principal claim name.
* Defaults to {@link JwtClaimNames#SUB}.
*
* @param principalClaimName The principal claim name
* @since 5.4
*/
public void setPrincipalClaimName(String principalClaimName) {
Assert.hasText(principalClaimName, "principalClaimName cannot be empty");
this.principalClaimName = principalClaimName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* Tests for {@link JwtAuthenticationConverter}
*
* @author Josh Cummings
* @author Evgeniy Cheban
*/
public class JwtAuthenticationConverterTests {
JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
Expand Down Expand Up @@ -73,4 +74,35 @@ public void convertWithOverriddenGrantedAuthoritiesConverter() {
assertThat(authorities).containsExactly(
new SimpleGrantedAuthority("blah"));
}

@Test
public void whenSettingNullPrincipalClaimName() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.jwtAuthenticationConverter.setPrincipalClaimName(null))
.withMessage("principalClaimName cannot be empty");
}

@Test
public void whenSettingEmptyPrincipalClaimName() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.jwtAuthenticationConverter.setPrincipalClaimName(""))
.withMessage("principalClaimName cannot be empty");
}

@Test
public void whenSettingBlankPrincipalClaimName() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.jwtAuthenticationConverter.setPrincipalClaimName(" "))
.withMessage("principalClaimName cannot be empty");
}

@Test
public void convertWhenPrincipalClaimNameSet() {
this.jwtAuthenticationConverter.setPrincipalClaimName("user_id");

Jwt jwt = jwt().claim("user_id", "100").build();
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt);

assertThat(authentication.getName()).isEqualTo("100");
}
}