|
1 | 1 | /* |
2 | | - * Copyright 2002-2024 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.web.configurers; |
18 | 18 |
|
| 19 | +import java.nio.charset.StandardCharsets; |
19 | 20 | import java.util.List; |
20 | 21 |
|
21 | 22 | import org.junit.jupiter.api.Test; |
|
24 | 25 | import org.springframework.beans.factory.annotation.Autowired; |
25 | 26 | import org.springframework.context.annotation.Bean; |
26 | 27 | import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.http.HttpOutputMessage; |
| 29 | +import org.springframework.http.converter.HttpMessageConverter; |
| 30 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
27 | 31 | import org.springframework.security.config.Customizer; |
28 | 32 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
29 | 33 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
30 | 34 | import org.springframework.security.config.test.SpringTestContext; |
31 | 35 | import org.springframework.security.config.test.SpringTestContextExtension; |
| 36 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 37 | +import org.springframework.security.core.context.SecurityContextImpl; |
32 | 38 | import org.springframework.security.core.userdetails.UserDetailsService; |
33 | 39 | import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
34 | 40 | import org.springframework.security.web.FilterChainProxy; |
35 | 41 | import org.springframework.security.web.SecurityFilterChain; |
36 | 42 | import org.springframework.security.web.authentication.ui.DefaultResourcesFilter; |
| 43 | +import org.springframework.security.web.webauthn.api.PublicKeyCredentialCreationOptions; |
| 44 | +import org.springframework.security.web.webauthn.api.TestPublicKeyCredentialCreationOptions; |
| 45 | +import org.springframework.security.web.webauthn.management.WebAuthnRelyingPartyOperations; |
37 | 46 | import org.springframework.test.web.servlet.MockMvc; |
38 | 47 |
|
39 | 48 | import static org.assertj.core.api.Assertions.assertThat; |
40 | 49 | import static org.hamcrest.Matchers.containsString; |
| 50 | +import static org.mockito.ArgumentMatchers.any; |
| 51 | +import static org.mockito.BDDMockito.given; |
| 52 | +import static org.mockito.BDDMockito.willAnswer; |
| 53 | +import static org.mockito.Mockito.mock; |
41 | 54 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 55 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
42 | 56 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
43 | 57 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; |
44 | 58 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
@@ -126,6 +140,56 @@ public void webauthnWhenConfiguredAndNoDefaultRegistrationPageThenDoesNotServeJa |
126 | 140 | this.mvc.perform(get("/login/webauthn.js")).andExpect(status().isNotFound()); |
127 | 141 | } |
128 | 142 |
|
| 143 | + @Test |
| 144 | + public void webauthnWhenConfiguredMessageConverter() throws Exception { |
| 145 | + TestingAuthenticationToken user = new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
| 146 | + SecurityContextHolder.setContext(new SecurityContextImpl(user)); |
| 147 | + PublicKeyCredentialCreationOptions options = TestPublicKeyCredentialCreationOptions |
| 148 | + .createPublicKeyCredentialCreationOptions() |
| 149 | + .build(); |
| 150 | + WebAuthnRelyingPartyOperations rpOperations = mock(WebAuthnRelyingPartyOperations.class); |
| 151 | + ConfigMessageConverter.rpOperations = rpOperations; |
| 152 | + given(rpOperations.createPublicKeyCredentialCreationOptions(any())).willReturn(options); |
| 153 | + HttpMessageConverter<Object> converter = mock(HttpMessageConverter.class); |
| 154 | + given(converter.canWrite(any(), any())).willReturn(true); |
| 155 | + String expectedBody = "123"; |
| 156 | + willAnswer((args) -> { |
| 157 | + HttpOutputMessage out = (HttpOutputMessage) args.getArguments()[2]; |
| 158 | + out.getBody().write(expectedBody.getBytes(StandardCharsets.UTF_8)); |
| 159 | + return null; |
| 160 | + }).given(converter).write(any(), any(), any()); |
| 161 | + ConfigMessageConverter.converter = converter; |
| 162 | + this.spring.register(ConfigMessageConverter.class).autowire(); |
| 163 | + this.mvc.perform(post("/webauthn/register/options")) |
| 164 | + .andExpect(status().isOk()) |
| 165 | + .andExpect(content().string(expectedBody)); |
| 166 | + } |
| 167 | + |
| 168 | + @Configuration |
| 169 | + @EnableWebSecurity |
| 170 | + static class ConfigMessageConverter { |
| 171 | + |
| 172 | + private static HttpMessageConverter<Object> converter; |
| 173 | + |
| 174 | + private static WebAuthnRelyingPartyOperations rpOperations; |
| 175 | + |
| 176 | + @Bean |
| 177 | + WebAuthnRelyingPartyOperations webAuthnRelyingPartyOperations() { |
| 178 | + return ConfigMessageConverter.rpOperations; |
| 179 | + } |
| 180 | + |
| 181 | + @Bean |
| 182 | + UserDetailsService userDetailsService() { |
| 183 | + return new InMemoryUserDetailsManager(); |
| 184 | + } |
| 185 | + |
| 186 | + @Bean |
| 187 | + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 188 | + return http.csrf(AbstractHttpConfigurer::disable).webAuthn((c) -> c.messageConverter(converter)).build(); |
| 189 | + } |
| 190 | + |
| 191 | + } |
| 192 | + |
129 | 193 | @Configuration |
130 | 194 | @EnableWebSecurity |
131 | 195 | static class DefaultWebauthnConfiguration { |
|
0 commit comments