|
24 | 24 | import org.springframework.beans.factory.annotation.Autowired;
|
25 | 25 | import org.springframework.context.annotation.Bean;
|
26 | 26 | import org.springframework.context.annotation.Configuration;
|
| 27 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
27 | 28 | import org.springframework.security.config.Customizer;
|
28 | 29 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
29 | 30 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
30 | 31 | import org.springframework.security.config.test.SpringTestContext;
|
31 | 32 | import org.springframework.security.config.test.SpringTestContextExtension;
|
| 33 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 34 | +import org.springframework.security.core.context.SecurityContextImpl; |
32 | 35 | import org.springframework.security.core.userdetails.UserDetailsService;
|
33 | 36 | import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
34 | 37 | import org.springframework.security.web.FilterChainProxy;
|
35 | 38 | import org.springframework.security.web.SecurityFilterChain;
|
36 | 39 | import org.springframework.security.web.authentication.ui.DefaultResourcesFilter;
|
| 40 | +import org.springframework.security.web.webauthn.api.PublicKeyCredentialCreationOptions; |
| 41 | +import org.springframework.security.web.webauthn.api.TestPublicKeyCredentialCreationOptions; |
| 42 | +import org.springframework.security.web.webauthn.management.WebAuthnRelyingPartyOperations; |
| 43 | +import org.springframework.security.web.webauthn.registration.HttpSessionPublicKeyCredentialCreationOptionsRepository; |
37 | 44 | import org.springframework.test.web.servlet.MockMvc;
|
38 | 45 |
|
39 | 46 | import static org.assertj.core.api.Assertions.assertThat;
|
40 | 47 | import static org.hamcrest.Matchers.containsString;
|
| 48 | +import static org.mockito.ArgumentMatchers.any; |
| 49 | +import static org.mockito.BDDMockito.given; |
| 50 | +import static org.mockito.Mockito.mock; |
41 | 51 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
| 52 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
42 | 53 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
43 | 54 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
| 55 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; |
44 | 56 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
45 | 57 |
|
46 | 58 | /**
|
@@ -126,6 +138,103 @@ public void webauthnWhenConfiguredAndNoDefaultRegistrationPageThenDoesNotServeJa
|
126 | 138 | this.mvc.perform(get("/login/webauthn.js")).andExpect(status().isNotFound());
|
127 | 139 | }
|
128 | 140 |
|
| 141 | + @Test |
| 142 | + public void webauthnWhenConfiguredPublicKeyCredentialCreationOptionsRepository() throws Exception { |
| 143 | + TestingAuthenticationToken user = new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
| 144 | + SecurityContextHolder.setContext(new SecurityContextImpl(user)); |
| 145 | + PublicKeyCredentialCreationOptions options = TestPublicKeyCredentialCreationOptions |
| 146 | + .createPublicKeyCredentialCreationOptions() |
| 147 | + .build(); |
| 148 | + WebAuthnRelyingPartyOperations rpOperations = mock(WebAuthnRelyingPartyOperations.class); |
| 149 | + ConfigCredentialCreationOptionsRepository.rpOperations = rpOperations; |
| 150 | + given(rpOperations.createPublicKeyCredentialCreationOptions(any())).willReturn(options); |
| 151 | + String attrName = "attrName"; |
| 152 | + HttpSessionPublicKeyCredentialCreationOptionsRepository creationOptionsRepository = new HttpSessionPublicKeyCredentialCreationOptionsRepository(); |
| 153 | + creationOptionsRepository.setAttrName(attrName); |
| 154 | + ConfigCredentialCreationOptionsRepository.creationOptionsRepository = creationOptionsRepository; |
| 155 | + this.spring.register(ConfigCredentialCreationOptionsRepository.class).autowire(); |
| 156 | + this.mvc.perform(post("/webauthn/register/options")) |
| 157 | + .andExpect(status().isOk()) |
| 158 | + .andExpect(request().sessionAttribute(attrName, options)); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void webauthnWhenConfiguredPublicKeyCredentialCreationOptionsRepositoryBeanPresent() throws Exception { |
| 163 | + TestingAuthenticationToken user = new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
| 164 | + SecurityContextHolder.setContext(new SecurityContextImpl(user)); |
| 165 | + PublicKeyCredentialCreationOptions options = TestPublicKeyCredentialCreationOptions |
| 166 | + .createPublicKeyCredentialCreationOptions() |
| 167 | + .build(); |
| 168 | + WebAuthnRelyingPartyOperations rpOperations = mock(WebAuthnRelyingPartyOperations.class); |
| 169 | + ConfigCredentialCreationOptionsRepositoryFromBean.rpOperations = rpOperations; |
| 170 | + given(rpOperations.createPublicKeyCredentialCreationOptions(any())).willReturn(options); |
| 171 | + String attrName = "attrName"; |
| 172 | + HttpSessionPublicKeyCredentialCreationOptionsRepository creationOptionsRepository = new HttpSessionPublicKeyCredentialCreationOptionsRepository(); |
| 173 | + creationOptionsRepository.setAttrName(attrName); |
| 174 | + ConfigCredentialCreationOptionsRepositoryFromBean.creationOptionsRepository = creationOptionsRepository; |
| 175 | + this.spring.register(ConfigCredentialCreationOptionsRepositoryFromBean.class).autowire(); |
| 176 | + this.mvc.perform(post("/webauthn/register/options")) |
| 177 | + .andExpect(status().isOk()) |
| 178 | + .andExpect(request().sessionAttribute(attrName, options)); |
| 179 | + } |
| 180 | + |
| 181 | + @Configuration |
| 182 | + @EnableWebSecurity |
| 183 | + static class ConfigCredentialCreationOptionsRepository { |
| 184 | + |
| 185 | + private static HttpSessionPublicKeyCredentialCreationOptionsRepository creationOptionsRepository; |
| 186 | + |
| 187 | + private static WebAuthnRelyingPartyOperations rpOperations; |
| 188 | + |
| 189 | + @Bean |
| 190 | + WebAuthnRelyingPartyOperations webAuthnRelyingPartyOperations() { |
| 191 | + return ConfigCredentialCreationOptionsRepository.rpOperations; |
| 192 | + } |
| 193 | + |
| 194 | + @Bean |
| 195 | + UserDetailsService userDetailsService() { |
| 196 | + return new InMemoryUserDetailsManager(); |
| 197 | + } |
| 198 | + |
| 199 | + @Bean |
| 200 | + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 201 | + return http.csrf(AbstractHttpConfigurer::disable) |
| 202 | + .webAuthn((c) -> c.creationOptionsRepository(creationOptionsRepository)) |
| 203 | + .build(); |
| 204 | + } |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + @Configuration |
| 209 | + @EnableWebSecurity |
| 210 | + static class ConfigCredentialCreationOptionsRepositoryFromBean { |
| 211 | + |
| 212 | + private static HttpSessionPublicKeyCredentialCreationOptionsRepository creationOptionsRepository; |
| 213 | + |
| 214 | + private static WebAuthnRelyingPartyOperations rpOperations; |
| 215 | + |
| 216 | + @Bean |
| 217 | + WebAuthnRelyingPartyOperations webAuthnRelyingPartyOperations() { |
| 218 | + return ConfigCredentialCreationOptionsRepositoryFromBean.rpOperations; |
| 219 | + } |
| 220 | + |
| 221 | + @Bean |
| 222 | + UserDetailsService userDetailsService() { |
| 223 | + return new InMemoryUserDetailsManager(); |
| 224 | + } |
| 225 | + |
| 226 | + @Bean |
| 227 | + HttpSessionPublicKeyCredentialCreationOptionsRepository creationOptionsRepository() { |
| 228 | + return ConfigCredentialCreationOptionsRepositoryFromBean.creationOptionsRepository; |
| 229 | + } |
| 230 | + |
| 231 | + @Bean |
| 232 | + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 233 | + return http.csrf(AbstractHttpConfigurer::disable).webAuthn(Customizer.withDefaults()).build(); |
| 234 | + } |
| 235 | + |
| 236 | + } |
| 237 | + |
129 | 238 | @Configuration
|
130 | 239 | @EnableWebSecurity
|
131 | 240 | static class DefaultWebauthnConfiguration {
|
|
0 commit comments