Search in sources :

Example 96 with Jwt

use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method requestWhenRealmNameConfiguredThenUsesOnAccessDenied.

@Test
public void requestWhenRealmNameConfiguredThenUsesOnAccessDenied() throws Exception {
    this.spring.register(RealmNameConfiguredOnAccessDeniedHandler.class, JwtDecoderConfig.class).autowire();
    JwtDecoder decoder = this.spring.getContext().getBean(JwtDecoder.class);
    given(decoder.decode(anyString())).willReturn(JWT);
    // @formatter:off
    this.mvc.perform(get("/authenticated").with(bearerToken("insufficiently_scoped"))).andExpect(status().isForbidden()).andExpect(header().string(HttpHeaders.WWW_AUTHENTICATE, startsWith("Bearer realm=\"myRealm\"")));
// @formatter:on
}
Also used : NimbusJwtDecoder(org.springframework.security.oauth2.jwt.NimbusJwtDecoder) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) Test(org.junit.jupiter.api.Test)

Example 97 with Jwt

use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method getJwtDecoderWhenConfiguredWithDecoderAndJwkSetUriThenLastOneWins.

@Test
public void getJwtDecoderWhenConfiguredWithDecoderAndJwkSetUriThenLastOneWins() {
    ApplicationContext context = mock(ApplicationContext.class);
    OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt();
    JwtDecoder decoder = mock(JwtDecoder.class);
    jwtConfigurer.jwkSetUri(JWK_SET_URI);
    jwtConfigurer.decoder(decoder);
    assertThat(jwtConfigurer.getJwtDecoder()).isEqualTo(decoder);
    jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt();
    jwtConfigurer.decoder(decoder);
    jwtConfigurer.jwkSetUri(JWK_SET_URI);
    assertThat(jwtConfigurer.getJwtDecoder()).isInstanceOf(NimbusJwtDecoder.class);
}
Also used : GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) NimbusJwtDecoder(org.springframework.security.oauth2.jwt.NimbusJwtDecoder) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) Test(org.junit.jupiter.api.Test)

Example 98 with Jwt

use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method getJwtDecoderWhenTwoJwtDecoderBeansThenThrowsException.

@Test
public void getJwtDecoderWhenTwoJwtDecoderBeansThenThrowsException() {
    JwtDecoder decoder = mock(JwtDecoder.class);
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.registerBean("decoderOne", JwtDecoder.class, () -> decoder);
    context.registerBean("decoderTwo", JwtDecoder.class, () -> decoder);
    this.spring.context(context).autowire();
    OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt();
    assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> jwtConfigurer.getJwtDecoder());
}
Also used : NimbusJwtDecoder(org.springframework.security.oauth2.jwt.NimbusJwtDecoder) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException) Test(org.junit.jupiter.api.Test)

Example 99 with Jwt

use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method getJwtAuthenticationConverterWhenDuplicateConverterBeansThenThrowsException.

@Test
public void getJwtAuthenticationConverterWhenDuplicateConverterBeansThenThrowsException() {
    JwtAuthenticationConverter converterBean = new JwtAuthenticationConverter();
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.registerBean("converterOne", JwtAuthenticationConverter.class, () -> converterBean);
    context.registerBean("converterTwo", JwtAuthenticationConverter.class, () -> converterBean);
    this.spring.context(context).autowire();
    OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt();
    assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(jwtConfigurer::getJwtAuthenticationConverter);
}
Also used : JwtAuthenticationConverter(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException) Test(org.junit.jupiter.api.Test)

Example 100 with Jwt

use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method requestWhenBearerTokenResolverAllowsRequestBodyThenEitherHeaderOrRequestBodyIsAccepted.

@Test
public void requestWhenBearerTokenResolverAllowsRequestBodyThenEitherHeaderOrRequestBodyIsAccepted() throws Exception {
    this.spring.register(AllowBearerTokenInRequestBodyConfig.class, JwtDecoderConfig.class, BasicController.class).autowire();
    JwtDecoder decoder = this.spring.getContext().getBean(JwtDecoder.class);
    given(decoder.decode(anyString())).willReturn(JWT);
    // @formatter:off
    this.mvc.perform(get("/authenticated").with(bearerToken(JWT_TOKEN))).andExpect(status().isOk()).andExpect(content().string(JWT_SUBJECT));
    this.mvc.perform(post("/authenticated").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE).param("access_token", JWT_TOKEN)).andExpect(status().isOk()).andExpect(content().string(JWT_SUBJECT));
// @formatter:on
}
Also used : NimbusJwtDecoder(org.springframework.security.oauth2.jwt.NimbusJwtDecoder) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)139 Jwt (org.springframework.security.oauth2.jwt.Jwt)83 GrantedAuthority (org.springframework.security.core.GrantedAuthority)47 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)37 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)36 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)36 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)30 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)29 BeforeEach (org.junit.jupiter.api.BeforeEach)29 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)27 TestClientRegistrations (org.springframework.security.oauth2.client.registration.TestClientRegistrations)24 Instant (java.time.Instant)23 HttpHeaders (org.springframework.http.HttpHeaders)22 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)22 JwtDecoder (org.springframework.security.oauth2.jwt.JwtDecoder)22 Collections (java.util.Collections)21 MediaType (org.springframework.http.MediaType)21 NimbusJwtDecoder (org.springframework.security.oauth2.jwt.NimbusJwtDecoder)21 MockWebServer (okhttp3.mockwebserver.MockWebServer)20 OAuth2Error (org.springframework.security.oauth2.core.OAuth2Error)20