use of com.auth0.jwt.JWT in project spring-security by spring-projects.
the class JwtGrantedAuthoritiesConverterTests method convertWithCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities.
@Test
public void convertWithCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
// @formatter:off
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
// @formatter:on
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
assertThat(authorities).containsExactly(new SimpleGrantedAuthority("ROLE_message:read"), new SimpleGrantedAuthority("ROLE_message:write"));
}
use of com.auth0.jwt.JWT in project spring-security by spring-projects.
the class SecurityMockMvcRequestPostProcessorsJwtTests method jwtWhenProvidingPreparedJwtThenUsesItForAuthentication.
@Test
public void jwtWhenProvidingPreparedJwtThenUsesItForAuthentication() {
Jwt originalToken = TestJwts.jwt().header("header1", "value1").subject("some_user").build();
jwt().jwt(originalToken).postProcessRequest(this.request);
verify(this.repository).saveContext(this.contextCaptor.capture(), eq(this.request), any(HttpServletResponse.class));
SecurityContext context = this.contextCaptor.getValue();
JwtAuthenticationToken retrievedToken = (JwtAuthenticationToken) context.getAuthentication();
assertThat(retrievedToken.getToken().getSubject()).isEqualTo("some_user");
assertThat(retrievedToken.getToken().getTokenValue()).isEqualTo("token");
assertThat(retrievedToken.getToken().getHeaders().get("header1")).isEqualTo("value1");
}
use of com.auth0.jwt.JWT in project spring-security by spring-projects.
the class SecurityMockServerConfigurersJwtTests method mockJwtWhenProvidingBuilderConsumerThenProducesJwtAuthentication.
@Test
public void mockJwtWhenProvidingBuilderConsumerThenProducesJwtAuthentication() {
String name = new String("user");
this.client.mutateWith(SecurityMockServerConfigurers.mockJwt().jwt((jwt) -> jwt.subject(name))).get().exchange().expectStatus().isOk();
SecurityContext context = this.securityContextController.removeSecurityContext();
assertThat(context.getAuthentication()).isInstanceOf(JwtAuthenticationToken.class);
JwtAuthenticationToken token = (JwtAuthenticationToken) context.getAuthentication();
assertThat(token.getToken().getSubject()).isSameAs(name);
}
use of com.auth0.jwt.JWT in project spring-security by spring-projects.
the class SecurityMockServerConfigurersJwtTests method mockJwtWhenProvidingGrantedAuthoritiesThenProducesJwtAuthentication.
@Test
public void mockJwtWhenProvidingGrantedAuthoritiesThenProducesJwtAuthentication() {
this.client.mutateWith(SecurityMockServerConfigurers.mockJwt().jwt((jwt) -> jwt.claim("scope", "ignored authorities")).authorities((jwt) -> Arrays.asList(this.authority1))).get().exchange().expectStatus().isOk();
SecurityContext context = this.securityContextController.removeSecurityContext();
assertThat((List<GrantedAuthority>) context.getAuthentication().getAuthorities()).containsOnly(this.authority1);
}
use of com.auth0.jwt.JWT in project spring-security by spring-projects.
the class OidcAuthorizationCodeAuthenticationProviderTests method setUpIdToken.
private void setUpIdToken(Map<String, Object> claims) {
Jwt idToken = TestJwts.jwt().claims((c) -> c.putAll(claims)).build();
JwtDecoder jwtDecoder = mock(JwtDecoder.class);
given(jwtDecoder.decode(anyString())).willReturn(idToken);
this.authenticationProvider.setJwtDecoderFactory((registration) -> jwtDecoder);
}
Aggregations