Search in sources :

Example 6 with BearerTokenAuthenticationToken

use of org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken in project spring-security by spring-projects.

the class JwtReactiveAuthenticationManagerTests method authenticateWhenJwtExceptionThenOAuth2AuthenticationException.

@Test
public void authenticateWhenJwtExceptionThenOAuth2AuthenticationException() {
    BearerTokenAuthenticationToken token = new BearerTokenAuthenticationToken("token-1");
    given(this.jwtDecoder.decode(any())).willReturn(Mono.error(new BadJwtException("Oops")));
    assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> this.manager.authenticate(token).block());
}
Also used : BadJwtException(org.springframework.security.oauth2.jwt.BadJwtException) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) OAuth2AuthenticationException(org.springframework.security.oauth2.core.OAuth2AuthenticationException) Test(org.junit.jupiter.api.Test)

Example 7 with BearerTokenAuthenticationToken

use of org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken in project spring-security by spring-projects.

the class JwtReactiveAuthenticationManagerTests method authenticateWhenDecoderThrowsIncompatibleErrorMessageThenWrapsWithGenericOne.

// gh-7549
@Test
public void authenticateWhenDecoderThrowsIncompatibleErrorMessageThenWrapsWithGenericOne() {
    BearerTokenAuthenticationToken token = new BearerTokenAuthenticationToken("token-1");
    given(this.jwtDecoder.decode(token.getToken())).willThrow(new BadJwtException("with \"invalid\" chars"));
    // @formatter:off
    assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> this.manager.authenticate(token).block()).satisfies((ex) -> assertThat(ex).hasFieldOrPropertyWithValue("error.description", "Invalid token"));
// @formatter:on
}
Also used : BadJwtException(org.springframework.security.oauth2.jwt.BadJwtException) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 8 with BearerTokenAuthenticationToken

use of org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken in project spring-security by spring-projects.

the class JwtReactiveAuthenticationManagerTests method authenticateWhenEmptyJwtThenEmpty.

@Test
public void authenticateWhenEmptyJwtThenEmpty() {
    BearerTokenAuthenticationToken token = new BearerTokenAuthenticationToken("token-1");
    given(this.jwtDecoder.decode(token.getToken())).willReturn(Mono.empty());
    assertThat(this.manager.authenticate(token).block()).isNull();
}
Also used : BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 9 with BearerTokenAuthenticationToken

use of org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken in project spring-security by spring-projects.

the class JwtReactiveAuthenticationManagerTests method authenticateWhenDecoderFailsGenericallyThenThrowsGenericException.

// gh-7785
@Test
public void authenticateWhenDecoderFailsGenericallyThenThrowsGenericException() {
    BearerTokenAuthenticationToken token = new BearerTokenAuthenticationToken("token-1");
    given(this.jwtDecoder.decode(token.getToken())).willThrow(new JwtException("no jwk set"));
    // @formatter:off
    assertThatExceptionOfType(AuthenticationException.class).isThrownBy(() -> this.manager.authenticate(token).block()).isNotInstanceOf(OAuth2AuthenticationException.class);
// @formatter:on
}
Also used : JwtException(org.springframework.security.oauth2.jwt.JwtException) BadJwtException(org.springframework.security.oauth2.jwt.BadJwtException) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 10 with BearerTokenAuthenticationToken

use of org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken in project spring-security by spring-projects.

the class JwtReactiveAuthenticationManagerTests method authenticateWhenNotJwtExceptionThenPropagates.

@Test
public void authenticateWhenNotJwtExceptionThenPropagates() {
    BearerTokenAuthenticationToken token = new BearerTokenAuthenticationToken("token-1");
    given(this.jwtDecoder.decode(any())).willReturn(Mono.error(new RuntimeException("Oops")));
    // @formatter:off
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> this.manager.authenticate(token).block());
// @formatter:on
}
Also used : BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) Test(org.junit.jupiter.api.Test)

Aggregations

BearerTokenAuthenticationToken (org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken)23 Test (org.junit.jupiter.api.Test)18 Authentication (org.springframework.security.core.Authentication)8 OAuth2AuthenticatedPrincipal (org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal)5 BadJwtException (org.springframework.security.oauth2.jwt.BadJwtException)5 AuthenticationServiceException (org.springframework.security.authentication.AuthenticationServiceException)3 Jwt (org.springframework.security.oauth2.jwt.Jwt)3 OpaqueTokenIntrospector (org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector)3 ReactiveOpaqueTokenIntrospector (org.springframework.security.oauth2.server.resource.introspection.ReactiveOpaqueTokenIntrospector)3 URL (java.net.URL)2 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 OAuth2AuthenticationException (org.springframework.security.oauth2.core.OAuth2AuthenticationException)2 JwtException (org.springframework.security.oauth2.jwt.JwtException)2 OAuth2IntrospectionAuthenticatedPrincipal (org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionAuthenticatedPrincipal)2 OAuth2IntrospectionException (org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionException)2 HttpModuleAuthentication (com.evolveum.midpoint.authentication.impl.module.authentication.HttpModuleAuthentication)1 MidPointPrincipal (com.evolveum.midpoint.security.api.MidPointPrincipal)1 JWSHeader (com.nimbusds.jose.JWSHeader)1 JWSObject (com.nimbusds.jose.JWSObject)1