Search in sources :

Example 36 with JWT

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"));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Jwt(org.springframework.security.oauth2.jwt.Jwt) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Example 37 with JWT

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");
}
Also used : JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) Jwt(org.springframework.security.oauth2.jwt.Jwt) SecurityContext(org.springframework.security.core.context.SecurityContext) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 38 with JWT

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);
}
Also used : JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) Arrays(java.util.Arrays) HttpHeaders(org.springframework.http.HttpHeaders) CurrentSecurityContextArgumentResolver(org.springframework.security.web.reactive.result.method.annotation.CurrentSecurityContextArgumentResolver) MediaType(org.springframework.http.MediaType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) TestJwts(org.springframework.security.oauth2.jwt.TestJwts) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) List(java.util.List) SecurityContext(org.springframework.security.core.context.SecurityContext) Jwt(org.springframework.security.oauth2.jwt.Jwt) SecurityContextServerWebExchangeWebFilter(org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) SecurityContext(org.springframework.security.core.context.SecurityContext) Test(org.junit.jupiter.api.Test)

Example 39 with JWT

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);
}
Also used : JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) Arrays(java.util.Arrays) HttpHeaders(org.springframework.http.HttpHeaders) CurrentSecurityContextArgumentResolver(org.springframework.security.web.reactive.result.method.annotation.CurrentSecurityContextArgumentResolver) MediaType(org.springframework.http.MediaType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) TestJwts(org.springframework.security.oauth2.jwt.TestJwts) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) List(java.util.List) SecurityContext(org.springframework.security.core.context.SecurityContext) Jwt(org.springframework.security.oauth2.jwt.Jwt) SecurityContextServerWebExchangeWebFilter(org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) SecurityContext(org.springframework.security.core.context.SecurityContext) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 40 with JWT

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);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) OidcUser(org.springframework.security.oauth2.core.oidc.user.OidcUser) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) Jwt(org.springframework.security.oauth2.jwt.Jwt) StringKeyGenerator(org.springframework.security.crypto.keygen.StringKeyGenerator) TestClientRegistrations(org.springframework.security.oauth2.client.registration.TestClientRegistrations) OAuth2AuthorizationExchange(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationExchange) OAuth2AuthenticationException(org.springframework.security.oauth2.core.OAuth2AuthenticationException) Set(java.util.Set) TestJwts(org.springframework.security.oauth2.jwt.TestJwts) Instant(java.time.Instant) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test) OAuth2AuthorizationCodeGrantRequest(org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest) Base64(java.util.Base64) List(java.util.List) Base64StringKeyGenerator(org.springframework.security.crypto.keygen.Base64StringKeyGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) OidcUserRequest(org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IdTokenClaimNames(org.springframework.security.oauth2.core.oidc.IdTokenClaimNames) TestOAuth2AuthorizationRequests(org.springframework.security.oauth2.core.endpoint.TestOAuth2AuthorizationRequests) OidcParameterNames(org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames) HashMap(java.util.HashMap) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) LinkedHashSet(java.util.LinkedHashSet) OAuth2LoginAuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2LoginAuthenticationToken) OAuth2AuthorizationResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) OAuth2ErrorCodes(org.springframework.security.oauth2.core.OAuth2ErrorCodes) OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) OAuth2AccessTokenResponseClient(org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) GrantedAuthoritiesMapper(org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper) OAuth2UserService(org.springframework.security.oauth2.client.userinfo.OAuth2UserService) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) TestOAuth2AuthorizationResponses(org.springframework.security.oauth2.core.endpoint.TestOAuth2AuthorizationResponses) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) JwtException(org.springframework.security.oauth2.jwt.JwtException) Collections(java.util.Collections) AuthorityUtils(org.springframework.security.core.authority.AuthorityUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Jwt(org.springframework.security.oauth2.jwt.Jwt) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder)

Aggregations

Jwt (org.springframework.security.oauth2.jwt.Jwt)96 Test (org.junit.jupiter.api.Test)78 GrantedAuthority (org.springframework.security.core.GrantedAuthority)49 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)39 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)19 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)18 TestJwts (org.springframework.security.oauth2.jwt.TestJwts)18 Arrays (java.util.Arrays)17 List (java.util.List)17 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)16 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)16 Test (org.junit.Test)14 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)13 Authentication (org.springframework.security.core.Authentication)12 HashMap (java.util.HashMap)11 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)11 BeforeEach (org.junit.jupiter.api.BeforeEach)11 Map (java.util.Map)10 SecurityContext (org.springframework.security.core.context.SecurityContext)10 JwtAuthenticationToken (org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken)10