|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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 | + |
| 17 | +package org.springframework.security.config.annotation.web.configurers.oauth2.client; |
| 18 | + |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; |
| 23 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 24 | + |
| 25 | +import java.util.Collections; |
| 26 | + |
| 27 | +import javax.servlet.http.HttpServletRequest; |
| 28 | +import javax.servlet.http.HttpServletResponse; |
| 29 | + |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Rule; |
| 32 | +import org.junit.Test; |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 36 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 37 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 38 | +import org.springframework.security.config.test.SpringTestRule; |
| 39 | +import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; |
| 40 | +import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest; |
| 41 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 42 | +import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository; |
| 43 | +import org.springframework.security.oauth2.client.registration.TestClientRegistrations; |
| 44 | +import org.springframework.security.oauth2.client.web.AuthorizationRequestRepository; |
| 45 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 46 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; |
| 47 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; |
| 48 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 49 | +import org.springframework.test.web.servlet.MockMvc; |
| 50 | +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
| 51 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 52 | + |
| 53 | +/** |
| 54 | + * Tests for OAuth2 Authorization Code Grant final redirect |
| 55 | + * |
| 56 | + * @author Tadaya Tsuyukubo |
| 57 | + * @see org.springframework.security.oauth2.client.web.OAuth2AuthorizationCodeGrantFilter |
| 58 | + */ |
| 59 | +public class OAuth2AuthorizationCodeGrantRedirectTests { |
| 60 | + |
| 61 | + private static AuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository; |
| 62 | + private static OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> accessTokenResponseClient; |
| 63 | + |
| 64 | + @Rule |
| 65 | + public final SpringTestRule spring = new SpringTestRule(); |
| 66 | + |
| 67 | + @Autowired |
| 68 | + private MockMvc mvc; |
| 69 | + |
| 70 | + @Before |
| 71 | + public void setUp() { |
| 72 | + authorizationRequestRepository = mock(AuthorizationRequestRepository.class); |
| 73 | + accessTokenResponseClient = mock(OAuth2AccessTokenResponseClient.class); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void redirect() throws Exception { |
| 78 | + perform("/redirect?code=MY-CODE&state=MY-STATE", |
| 79 | + "http://localhost/redirect", |
| 80 | + "http://localhost/redirect"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void redirectWithParamAppended() throws Exception { |
| 85 | + perform("/redirect?code=MY-CODE&state=MY-STATE&extra=EXTRA", |
| 86 | + "http://localhost/redirect", |
| 87 | + "http://localhost/redirect?extra=EXTRA"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void redirectWithParameters() throws Exception { |
| 92 | + perform("/redirect?foo=FOO&code=MY-CODE&state=MY-STATE", |
| 93 | + "http://localhost/redirect?foo=FOO", |
| 94 | + "http://localhost/redirect?foo=FOO"); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void redirectUrlWithParametersWithParamAppended() throws Exception { |
| 99 | + perform("/redirect?foo=FOO&code=MY-CODE&state=MY-STATE&extra=EXTRA", |
| 100 | + "http://localhost/redirect?foo=FOO", |
| 101 | + "http://localhost/redirect?foo=FOO&extra=EXTRA"); |
| 102 | + } |
| 103 | + |
| 104 | + private void perform(String requestUri, String authorizationRequestRedirectUri, String expectedRedirectUrl) throws Exception { |
| 105 | + this.spring.register(WebSecurityConfiguration.class).autowire(); |
| 106 | + |
| 107 | + OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest |
| 108 | + .authorizationCode() |
| 109 | + .authorizationUri("http://localhost/auth") |
| 110 | + .clientId("example") |
| 111 | + .state("MY-STATE") |
| 112 | + .redirectUri(authorizationRequestRedirectUri) |
| 113 | + .attributes(Collections.singletonMap(OAuth2ParameterNames.REGISTRATION_ID, "registration-id")) // comes from TestClientRegistrations.clientRegistration |
| 114 | + .build(); |
| 115 | + |
| 116 | + when(authorizationRequestRepository.loadAuthorizationRequest(any(HttpServletRequest.class))) |
| 117 | + .thenReturn(authorizationRequest); |
| 118 | + when(authorizationRequestRepository.removeAuthorizationRequest(any(HttpServletRequest.class), any(HttpServletResponse.class))) |
| 119 | + .thenReturn(authorizationRequest); |
| 120 | + |
| 121 | + OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse |
| 122 | + .withToken("MY-ACCESS-TOKEN") |
| 123 | + .tokenType(OAuth2AccessToken.TokenType.BEARER) |
| 124 | + .build(); |
| 125 | + when(accessTokenResponseClient |
| 126 | + .getTokenResponse(any(OAuth2AuthorizationCodeGrantRequest.class))) |
| 127 | + .thenReturn(accessTokenResponse); |
| 128 | + |
| 129 | + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(requestUri); |
| 130 | + this.mvc.perform(builder) |
| 131 | + .andExpect(status().is3xxRedirection()) |
| 132 | + .andExpect(redirectedUrl(expectedRedirectUrl)); |
| 133 | + } |
| 134 | + |
| 135 | + @Configuration |
| 136 | + @EnableWebSecurity |
| 137 | + static class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { |
| 138 | + @Override |
| 139 | + protected void configure(HttpSecurity http) throws Exception { |
| 140 | + |
| 141 | + ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().build(); |
| 142 | + |
| 143 | + InMemoryClientRegistrationRepository inMemoryClientRegistrationRepository = |
| 144 | + new InMemoryClientRegistrationRepository(clientRegistration); |
| 145 | + |
| 146 | + http.oauth2Client(oauth2client -> |
| 147 | + oauth2client |
| 148 | + .clientRegistrationRepository(inMemoryClientRegistrationRepository) |
| 149 | + .authorizationCodeGrant() |
| 150 | + .authorizationRequestRepository(authorizationRequestRepository) |
| 151 | + .accessTokenResponseClient(accessTokenResponseClient) |
| 152 | + ); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments