|
| 1 | +/* |
| 2 | + * Copyright 2002-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.test.web.servlet.request; |
| 17 | + |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 26 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 27 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 28 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 29 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; |
| 30 | +import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient; |
| 31 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 32 | +import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
| 33 | +import org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizedClientRepository; |
| 34 | +import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository; |
| 35 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 36 | +import org.springframework.security.test.context.TestSecurityContextHolder; |
| 37 | +import org.springframework.test.context.ContextConfiguration; |
| 38 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 39 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 40 | +import org.springframework.test.web.servlet.MockMvc; |
| 41 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 42 | +import org.springframework.web.bind.annotation.GetMapping; |
| 43 | +import org.springframework.web.bind.annotation.RestController; |
| 44 | +import org.springframework.web.context.WebApplicationContext; |
| 45 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 46 | + |
| 47 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 48 | +import static org.mockito.Mockito.mock; |
| 49 | +import static org.springframework.security.oauth2.client.registration.TestClientRegistrations.clientRegistration; |
| 50 | +import static org.springframework.security.oauth2.core.TestOAuth2AccessTokens.noScopes; |
| 51 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Client; |
| 52 | +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; |
| 53 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 54 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 55 | + |
| 56 | +/** |
| 57 | + * Tests for {@link SecurityMockMvcRequestPostProcessors#oidcLogin()} |
| 58 | + * |
| 59 | + * @author Josh Cummings |
| 60 | + * @since 5.3 |
| 61 | + */ |
| 62 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 63 | +@ContextConfiguration |
| 64 | +@WebAppConfiguration |
| 65 | +public class SecurityMockMvcRequestPostProcessorsOAuth2ClientTests { |
| 66 | + @Autowired |
| 67 | + WebApplicationContext context; |
| 68 | + |
| 69 | + MockMvc mvc; |
| 70 | + |
| 71 | + @Before |
| 72 | + public void setup() { |
| 73 | + // @formatter:off |
| 74 | + this.mvc = MockMvcBuilders |
| 75 | + .webAppContextSetup(this.context) |
| 76 | + .apply(springSecurity()) |
| 77 | + .build(); |
| 78 | + // @formatter:on |
| 79 | + } |
| 80 | + |
| 81 | + @After |
| 82 | + public void cleanup() { |
| 83 | + TestSecurityContextHolder.clearContext(); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + @Test |
| 88 | + public void oauth2ClientWhenUsingDefaultsThenException() |
| 89 | + throws Exception { |
| 90 | + |
| 91 | + assertThatCode(() -> oauth2Client().postProcessRequest(new MockHttpServletRequest())) |
| 92 | + .isInstanceOf(IllegalArgumentException.class) |
| 93 | + .hasMessageContaining("ClientRegistration"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void oauth2ClientWhenUsingDefaultsThenProducesDefaultAuthorizedClient() |
| 98 | + throws Exception { |
| 99 | + |
| 100 | + this.mvc.perform(get("/access-token").with(oauth2Client("registration-id"))) |
| 101 | + .andExpect(content().string("access-token")); |
| 102 | + this.mvc.perform(get("/client-id").with(oauth2Client("registration-id"))) |
| 103 | + .andExpect(content().string("test-client")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void oauth2ClientWhenClientRegistrationThenUses() |
| 108 | + throws Exception { |
| 109 | + |
| 110 | + ClientRegistration clientRegistration = clientRegistration() |
| 111 | + .registrationId("registration-id").clientId("client-id").build(); |
| 112 | + this.mvc.perform(get("/client-id") |
| 113 | + .with(oauth2Client().clientRegistration(clientRegistration))) |
| 114 | + .andExpect(content().string("client-id")); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void oauth2ClientWhenClientRegistrationConsumerThenUses() |
| 119 | + throws Exception { |
| 120 | + |
| 121 | + this.mvc.perform(get("/client-id") |
| 122 | + .with(oauth2Client("registration-id").clientRegistration(c -> c.clientId("client-id")))) |
| 123 | + .andExpect(content().string("client-id")); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void oauth2ClientWhenAccessTokenThenUses() throws Exception { |
| 128 | + OAuth2AccessToken accessToken = noScopes(); |
| 129 | + this.mvc.perform(get("/access-token") |
| 130 | + .with(oauth2Client("registration-id").accessToken(accessToken))) |
| 131 | + .andExpect(content().string("no-scopes")); |
| 132 | + } |
| 133 | + |
| 134 | + @EnableWebSecurity |
| 135 | + @EnableWebMvc |
| 136 | + static class OAuth2ClientConfig extends WebSecurityConfigurerAdapter { |
| 137 | + @Override |
| 138 | + protected void configure(HttpSecurity http) throws Exception { |
| 139 | + http |
| 140 | + .authorizeRequests(authz -> authz |
| 141 | + .anyRequest().permitAll() |
| 142 | + ) |
| 143 | + .oauth2Client(); |
| 144 | + } |
| 145 | + |
| 146 | + @Bean |
| 147 | + ClientRegistrationRepository clientRegistrationRepository() { |
| 148 | + return mock(ClientRegistrationRepository.class); |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + @Bean |
| 153 | + OAuth2AuthorizedClientRepository authorizedClientRepository() { |
| 154 | + return new HttpSessionOAuth2AuthorizedClientRepository(); |
| 155 | + } |
| 156 | + |
| 157 | + @RestController |
| 158 | + static class PrincipalController { |
| 159 | + @GetMapping("/access-token") |
| 160 | + String accessToken(@RegisteredOAuth2AuthorizedClient("registration-id") OAuth2AuthorizedClient authorizedClient) { |
| 161 | + return authorizedClient.getAccessToken().getTokenValue(); |
| 162 | + } |
| 163 | + |
| 164 | + @GetMapping("/client-id") |
| 165 | + String clientId(@RegisteredOAuth2AuthorizedClient("registration-id") OAuth2AuthorizedClient authorizedClient) { |
| 166 | + return authorizedClient.getClientRegistration().getClientId(); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments