|
@@ -37,6 +37,7 @@ import org.springframework.http.converter.HttpMessageConverter;
|
|
|
import org.springframework.mock.http.client.MockClientHttpResponse;
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
+import org.springframework.security.authentication.AuthenticationDetailsSource;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
@@ -115,6 +116,13 @@ public class OAuth2TokenEndpointFilterTests {
|
|
|
.hasMessage("tokenEndpointUri cannot be empty");
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void setAuthenticationDetailsSourceWhenNullThenThrowIllegalArgumentException() {
|
|
|
+ assertThatThrownBy(() -> this.filter.setAuthenticationDetailsSource(null))
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
+ .hasMessage("authenticationDetailsSource cannot be null");
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void setAuthenticationConverterWhenNullThenThrowIllegalArgumentException() {
|
|
|
assertThatThrownBy(() -> this.filter.setAuthenticationConverter(null))
|
|
@@ -444,6 +452,41 @@ public class OAuth2TokenEndpointFilterTests {
|
|
|
assertThat(refreshTokenResult.getTokenValue()).isEqualTo(refreshToken.getTokenValue());
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void doFilterWhenCustomAuthenticationDetailsSourceThenUsed() throws Exception {
|
|
|
+ RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
+ Authentication clientPrincipal = new OAuth2ClientAuthenticationToken(
|
|
|
+ registeredClient, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, registeredClient.getClientSecret());
|
|
|
+
|
|
|
+ MockHttpServletRequest request = createAuthorizationCodeTokenRequest(registeredClient);
|
|
|
+
|
|
|
+ AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> authenticationDetailsSource =
|
|
|
+ mock(AuthenticationDetailsSource.class);
|
|
|
+ WebAuthenticationDetails webAuthenticationDetails = new WebAuthenticationDetails(request);
|
|
|
+ when(authenticationDetailsSource.buildDetails(request)).thenReturn(webAuthenticationDetails);
|
|
|
+ this.filter.setAuthenticationDetailsSource(authenticationDetailsSource);
|
|
|
+
|
|
|
+ OAuth2AccessToken accessToken = new OAuth2AccessToken(
|
|
|
+ OAuth2AccessToken.TokenType.BEARER, "token",
|
|
|
+ Instant.now(), Instant.now().plus(Duration.ofHours(1)),
|
|
|
+ new HashSet<>(Arrays.asList("scope1", "scope2")));
|
|
|
+ OAuth2AccessTokenAuthenticationToken accessTokenAuthentication =
|
|
|
+ new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken);
|
|
|
+
|
|
|
+ when(this.authenticationManager.authenticate(any())).thenReturn(accessTokenAuthentication);
|
|
|
+
|
|
|
+ SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
|
|
|
+ securityContext.setAuthentication(clientPrincipal);
|
|
|
+ SecurityContextHolder.setContext(securityContext);
|
|
|
+
|
|
|
+ MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
+ FilterChain filterChain = mock(FilterChain.class);
|
|
|
+
|
|
|
+ this.filter.doFilter(request, response, filterChain);
|
|
|
+
|
|
|
+ verify(authenticationDetailsSource).buildDetails(request);
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void doFilterWhenCustomAuthenticationConverterThenUsed() throws Exception {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|