Search in sources :

Example 1 with BearerTokenAuthenticationToken

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

the class OpaqueTokenAuthenticationProviderTests method authenticateWhenIntrospectionEndpointThrowsExceptionThenInvalidToken.

@Test
public void authenticateWhenIntrospectionEndpointThrowsExceptionThenInvalidToken() {
    OpaqueTokenIntrospector introspector = mock(OpaqueTokenIntrospector.class);
    given(introspector.introspect(any())).willThrow(new OAuth2IntrospectionException("with \"invalid\" chars"));
    OpaqueTokenAuthenticationProvider provider = new OpaqueTokenAuthenticationProvider(introspector);
    assertThatExceptionOfType(AuthenticationServiceException.class).isThrownBy(() -> provider.authenticate(new BearerTokenAuthenticationToken("token")));
}
Also used : OpaqueTokenIntrospector(org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector) OAuth2IntrospectionException(org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionException) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException) Test(org.junit.jupiter.api.Test)

Example 2 with BearerTokenAuthenticationToken

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

the class JwtAuthenticationProviderTests method authenticateWhenJwtDecodeFailsThenRespondsWithInvalidToken.

@Test
public void authenticateWhenJwtDecodeFailsThenRespondsWithInvalidToken() {
    BearerTokenAuthenticationToken token = this.authentication();
    given(this.jwtDecoder.decode("token")).willThrow(BadJwtException.class);
    // @formatter:off
    assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> this.provider.authenticate(token)).matches(errorCode(BearerTokenErrorCodes.INVALID_TOKEN));
// @formatter:on
}
Also used : BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 3 with BearerTokenAuthenticationToken

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

the class JwtAuthenticationProviderTests method authenticateWhenConverterReturnsAuthenticationThenProviderPropagatesIt.

@Test
public void authenticateWhenConverterReturnsAuthenticationThenProviderPropagatesIt() {
    BearerTokenAuthenticationToken token = this.authentication();
    Object details = mock(Object.class);
    token.setDetails(details);
    Jwt jwt = TestJwts.jwt().build();
    JwtAuthenticationToken authentication = new JwtAuthenticationToken(jwt);
    given(this.jwtDecoder.decode(token.getToken())).willReturn(jwt);
    given(this.jwtAuthenticationConverter.convert(jwt)).willReturn(authentication);
    // @formatter:off
    assertThat(this.provider.authenticate(token)).isEqualTo(authentication).hasFieldOrPropertyWithValue("details", details);
// @formatter:on
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 4 with BearerTokenAuthenticationToken

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

the class JwtAuthenticationProviderTests method authenticateWhenDecoderThrowsIncompatibleErrorMessageThenWrapsWithGenericOne.

@Test
public void authenticateWhenDecoderThrowsIncompatibleErrorMessageThenWrapsWithGenericOne() {
    BearerTokenAuthenticationToken token = this.authentication();
    given(this.jwtDecoder.decode(token.getToken())).willThrow(new BadJwtException("with \"invalid\" chars"));
    // @formatter:off
    assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> this.provider.authenticate(token)).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 5 with BearerTokenAuthenticationToken

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

the class JwtAuthenticationProviderTests method authenticateWhenJwtDecodesThenAuthenticationHasAttributesContainedInJwt.

@Test
public void authenticateWhenJwtDecodesThenAuthenticationHasAttributesContainedInJwt() {
    BearerTokenAuthenticationToken token = this.authentication();
    Jwt jwt = TestJwts.jwt().claim("name", "value").build();
    given(this.jwtDecoder.decode("token")).willReturn(jwt);
    given(this.jwtAuthenticationConverter.convert(jwt)).willReturn(new JwtAuthenticationToken(jwt));
    JwtAuthenticationToken authentication = (JwtAuthenticationToken) this.provider.authenticate(token);
    assertThat(authentication.getTokenAttributes()).containsEntry("name", "value");
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) 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