|
| 1 | +/* |
| 2 | + * Copyright 2020 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.config.annotation.web.configurers.oauth2.server.authorization; |
| 17 | + |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.mockito.ArgumentCaptor; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.context.annotation.Import; |
| 26 | +import org.springframework.http.HttpHeaders; |
| 27 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 28 | +import org.springframework.security.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration; |
| 29 | +import org.springframework.security.config.test.SpringTestRule; |
| 30 | +import org.springframework.security.crypto.key.CryptoKeySource; |
| 31 | +import org.springframework.security.crypto.key.StaticKeyGeneratingCryptoKeySource; |
| 32 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 33 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponseType; |
| 34 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 35 | +import org.springframework.security.oauth2.core.endpoint.PkceParameterNames; |
| 36 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
| 37 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; |
| 38 | +import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations; |
| 39 | +import org.springframework.security.oauth2.server.authorization.TokenType; |
| 40 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 41 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; |
| 42 | +import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients; |
| 43 | +import org.springframework.security.oauth2.server.authorization.config.ProviderSettings; |
| 44 | +import org.springframework.security.oauth2.server.authorization.token.OAuth2AuthorizationCode; |
| 45 | +import org.springframework.security.oauth2.server.authorization.web.OAuth2AuthorizationEndpointFilter; |
| 46 | +import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenEndpointFilter; |
| 47 | +import org.springframework.security.oauth2.server.authorization.web.OidcProviderConfigurationEndpointFilter; |
| 48 | +import org.springframework.test.web.servlet.MockMvc; |
| 49 | +import org.springframework.test.web.servlet.MvcResult; |
| 50 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 51 | +import org.springframework.util.LinkedMultiValueMap; |
| 52 | +import org.springframework.util.MultiValueMap; |
| 53 | +import org.springframework.util.StringUtils; |
| 54 | + |
| 55 | +import java.net.MalformedURLException; |
| 56 | +import java.net.URL; |
| 57 | +import java.net.URLEncoder; |
| 58 | +import java.nio.charset.StandardCharsets; |
| 59 | +import java.util.Base64; |
| 60 | + |
| 61 | +import static org.assertj.core.api.Assertions.assertThat; |
| 62 | +import static org.hamcrest.CoreMatchers.containsString; |
| 63 | +import static org.mockito.ArgumentMatchers.any; |
| 64 | +import static org.mockito.ArgumentMatchers.eq; |
| 65 | +import static org.mockito.Mockito.mock; |
| 66 | +import static org.mockito.Mockito.reset; |
| 67 | +import static org.mockito.Mockito.times; |
| 68 | +import static org.mockito.Mockito.verify; |
| 69 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 70 | +import static org.mockito.Mockito.when; |
| 71 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; |
| 72 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 73 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 74 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; |
| 75 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 76 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 77 | + |
| 78 | +/** |
| 79 | + * Integration tests for the OpenID Connect. |
| 80 | + * |
| 81 | + * @author Daniel Garnier-Moiroux |
| 82 | + */ |
| 83 | +public class OidcTests { |
| 84 | + private static RegisteredClientRepository registeredClientRepository; |
| 85 | + private static CryptoKeySource keySource; |
| 86 | + private static ProviderSettings providerSettings; |
| 87 | + private static final String issuerUrl = "https://example.com/issuer1"; |
| 88 | + |
| 89 | + @Rule |
| 90 | + public final SpringTestRule spring = new SpringTestRule(); |
| 91 | + |
| 92 | + @Autowired |
| 93 | + private MockMvc mvc; |
| 94 | + |
| 95 | + @BeforeClass |
| 96 | + public static void init() { |
| 97 | + registeredClientRepository = mock(RegisteredClientRepository.class); |
| 98 | + keySource = new StaticKeyGeneratingCryptoKeySource(); |
| 99 | + providerSettings = new ProviderSettings(); |
| 100 | + } |
| 101 | + |
| 102 | + @Before |
| 103 | + public void setup() { |
| 104 | + reset(registeredClientRepository); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void requestWhenIssuerSetAndOpenIDProviderConfigurationRequestThenReturnProviderConfigurationResponse() throws Exception { |
| 109 | + this.spring.register(AuthorizationServerConfigurationWithIssuer.class).autowire(); |
| 110 | + |
| 111 | + this.mvc.perform(MockMvcRequestBuilders.get(OidcProviderConfigurationEndpointFilter.DEFAULT_OIDC_PROVIDER_CONFIGURATION_ENDPOINT_URI)) |
| 112 | + .andExpect(status().is2xxSuccessful()) |
| 113 | + .andExpect(jsonPath("issuer").value(issuerUrl)) |
| 114 | + .andReturn(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void requestWhenIssuerNotSetAndOpenIDProviderConfigurationRequestThenRedirectsToLogin() throws Exception { |
| 119 | + this.spring.register(AuthorizationServerConfiguration.class).autowire(); |
| 120 | + |
| 121 | + MvcResult mvcResult = this.mvc.perform(get(OidcProviderConfigurationEndpointFilter.DEFAULT_OIDC_PROVIDER_CONFIGURATION_ENDPOINT_URI)) |
| 122 | + .andExpect(status().is3xxRedirection()) |
| 123 | + .andReturn(); |
| 124 | + assertThat(mvcResult.getResponse().getRedirectedUrl()).endsWith("/login"); |
| 125 | + } |
| 126 | + |
| 127 | + @EnableWebSecurity |
| 128 | + @Import(OAuth2AuthorizationServerConfiguration.class) |
| 129 | + static class AuthorizationServerConfiguration { |
| 130 | + |
| 131 | + @Bean |
| 132 | + RegisteredClientRepository registeredClientRepository() { |
| 133 | + return registeredClientRepository; |
| 134 | + } |
| 135 | + |
| 136 | + @Bean |
| 137 | + CryptoKeySource keySource() { |
| 138 | + return keySource; |
| 139 | + } |
| 140 | + |
| 141 | + @Bean |
| 142 | + ProviderSettings providerSettings() { |
| 143 | + return providerSettings; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @EnableWebSecurity |
| 148 | + @Import(OAuth2AuthorizationServerConfiguration.class) |
| 149 | + static class AuthorizationServerConfigurationWithIssuer extends AuthorizationServerConfiguration { |
| 150 | + @Bean |
| 151 | + @Override |
| 152 | + ProviderSettings providerSettings() { |
| 153 | + URL url = null; |
| 154 | + try { |
| 155 | + url = new URL(issuerUrl); |
| 156 | + } catch (MalformedURLException ignored) { } |
| 157 | + return new ProviderSettings().issuer(url); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments