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")));
}
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
}
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
}
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
}
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");
}
Aggregations