|
| 1 | +/* |
| 2 | + * Copyright 2020-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.security.oauth2.server.authorization.web.authentication; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.time.Instant; |
| 20 | +import java.time.temporal.ChronoUnit; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import jakarta.servlet.ServletException; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.mockito.ArgumentCaptor; |
| 28 | +import org.springframework.http.converter.HttpMessageConverter; |
| 29 | +import org.springframework.http.server.ServletServerHttpResponse; |
| 30 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 31 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 32 | +import org.springframework.security.core.Authentication; |
| 33 | +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
| 34 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 35 | +import org.springframework.security.oauth2.core.OAuth2RefreshToken; |
| 36 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; |
| 37 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationToken; |
| 38 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken; |
| 39 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientCredentialsAuthenticationToken; |
| 40 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 41 | +import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients; |
| 42 | + |
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 45 | +import static org.assertj.core.api.Assertions.within; |
| 46 | +import static org.mockito.ArgumentMatchers.isNull; |
| 47 | +import static org.mockito.Mockito.mock; |
| 48 | +import static org.mockito.Mockito.verify; |
| 49 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 50 | + |
| 51 | +/** |
| 52 | + * Tests for {@link OAuth2TokenEndpointAuthenticationSuccessHandler}. |
| 53 | + * |
| 54 | + * @author Dmitriy Dubson |
| 55 | + */ |
| 56 | +public class OAuth2TokenEndpointAuthenticationSuccessHandlerTests { |
| 57 | + private final OAuth2TokenEndpointAuthenticationSuccessHandler authenticationSuccessHandler = new OAuth2TokenEndpointAuthenticationSuccessHandler(); |
| 58 | + |
| 59 | + @Test |
| 60 | + public void setAccessTokenHttpResponseConverterWhenNullThenThrowIllegalArgumentException() { |
| 61 | + // @formatter:off |
| 62 | + assertThatThrownBy(() -> this.authenticationSuccessHandler.setAccessTokenHttpResponseConverter(null)) |
| 63 | + .isInstanceOf(IllegalArgumentException.class) |
| 64 | + .hasMessage("accessTokenHttpResponseConverter cannot be null"); |
| 65 | + // @formatter:on |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void onAuthenticationSuccessWritesAccessTokenToHttpResponse() throws ServletException, IOException { |
| 70 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 71 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 72 | + |
| 73 | + RegisteredClient testClient = TestRegisteredClients.registeredClient().build(); |
| 74 | + OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken( |
| 75 | + testClient, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, testClient.getClientSecret()); |
| 76 | + Instant issuedAt = Instant.now(); |
| 77 | + Instant expiresAt = Instant.now().plusSeconds(300); |
| 78 | + OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, |
| 79 | + "access-token-value", issuedAt, expiresAt, Set.of("scope1")); |
| 80 | + OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token-value", issuedAt); |
| 81 | + Map<String, Object> additionalParameters = Collections.singletonMap("param1", "value1"); |
| 82 | + Authentication authentication = new OAuth2AccessTokenAuthenticationToken(testClient, clientPrincipal, accessToken, refreshToken, additionalParameters); |
| 83 | + |
| 84 | + HttpMessageConverter<OAuth2AccessTokenResponse> responseConverter = mock(HttpMessageConverter.class); |
| 85 | + authenticationSuccessHandler.setAccessTokenHttpResponseConverter(responseConverter); |
| 86 | + authenticationSuccessHandler.onAuthenticationSuccess(request, response, authentication); |
| 87 | + |
| 88 | + ArgumentCaptor<OAuth2AccessTokenResponse> accessTokenResponseCaptor = ArgumentCaptor.forClass(OAuth2AccessTokenResponse.class); |
| 89 | + ArgumentCaptor<ServletServerHttpResponse> servletServerHttpResponseArgumentCaptor = ArgumentCaptor.forClass(ServletServerHttpResponse.class); |
| 90 | + verify(responseConverter).write(accessTokenResponseCaptor.capture(), isNull(), servletServerHttpResponseArgumentCaptor.capture()); |
| 91 | + |
| 92 | + OAuth2AccessTokenResponse actualAccessTokenResponse = accessTokenResponseCaptor.getValue(); |
| 93 | + assertThat(actualAccessTokenResponse.getAccessToken().getTokenValue()).isEqualTo("access-token-value"); |
| 94 | + assertThat(actualAccessTokenResponse.getAccessToken().getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER); |
| 95 | + assertThat(actualAccessTokenResponse.getAccessToken().getScopes()).containsExactlyInAnyOrder("scope1"); |
| 96 | + assertThat(actualAccessTokenResponse.getAccessToken().getIssuedAt()).isCloseTo(issuedAt, within(2, ChronoUnit.SECONDS)); |
| 97 | + assertThat(actualAccessTokenResponse.getAccessToken().getExpiresAt()).isCloseTo(expiresAt, within(2, ChronoUnit.SECONDS)); |
| 98 | + assertThat(actualAccessTokenResponse.getRefreshToken()).isNotNull(); |
| 99 | + assertThat(actualAccessTokenResponse.getRefreshToken().getTokenValue()).isEqualTo("refresh-token-value"); |
| 100 | + assertThat(actualAccessTokenResponse.getAdditionalParameters()).containsExactlyEntriesOf(additionalParameters); |
| 101 | + |
| 102 | + assertThat(servletServerHttpResponseArgumentCaptor.getValue().getServletResponse()).isEqualTo(response); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void onAuthenticationSuccessWhenInvaliAuthenticationThenNoResponse() throws ServletException, IOException { |
| 107 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 108 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 109 | + |
| 110 | + RegisteredClient testClient = TestRegisteredClients.registeredClient().build(); |
| 111 | + OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken( |
| 112 | + testClient, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, testClient.getClientSecret()); |
| 113 | + |
| 114 | + HttpMessageConverter<OAuth2AccessTokenResponse> responseConverter = mock(HttpMessageConverter.class); |
| 115 | + authenticationSuccessHandler.setAccessTokenHttpResponseConverter(responseConverter); |
| 116 | + authenticationSuccessHandler.onAuthenticationSuccess(request, response, new OAuth2ClientCredentialsAuthenticationToken(clientPrincipal, Set.of(), Map.of())); |
| 117 | + |
| 118 | + verifyNoInteractions(responseConverter); |
| 119 | + } |
| 120 | +} |
0 commit comments