|
19 | 19 | import java.nio.charset.StandardCharsets; |
20 | 20 | import java.util.List; |
21 | 21 |
|
| 22 | +import com.fasterxml.jackson.databind.JsonNode; |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
22 | 24 | import org.junit.jupiter.api.Test; |
23 | 25 | import org.junit.jupiter.api.extension.ExtendWith; |
24 | 26 |
|
|
54 | 56 | import static org.mockito.BDDMockito.given; |
55 | 57 | import static org.mockito.BDDMockito.willAnswer; |
56 | 58 | import static org.mockito.Mockito.mock; |
| 59 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; |
| 60 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
57 | 61 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
58 | 62 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
59 | 63 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
@@ -137,6 +141,42 @@ public void webauthnWhenFormLoginAndDefaultRegistrationPageConfiguredThenNoDupli |
137 | 141 | .hasSize(1); |
138 | 142 | } |
139 | 143 |
|
| 144 | + @Test |
| 145 | + void webauthnWhenConfiguredDefaultsRpNameToRpId() throws Exception { |
| 146 | + ObjectMapper mapper = new ObjectMapper(); |
| 147 | + this.spring.register(DefaultWebauthnConfiguration.class).autowire(); |
| 148 | + String response = this.mvc |
| 149 | + .perform(post("/webauthn/register/options").with(csrf()) |
| 150 | + .with(authentication(new TestingAuthenticationToken("test", "ignored", "ROLE_user")))) |
| 151 | + .andExpect(status().is2xxSuccessful()) |
| 152 | + .andReturn() |
| 153 | + .getResponse() |
| 154 | + .getContentAsString(); |
| 155 | + |
| 156 | + JsonNode parsedResponse = mapper.readTree(response); |
| 157 | + |
| 158 | + assertThat(parsedResponse.get("rp").get("id").asText()).isEqualTo("example.com"); |
| 159 | + assertThat(parsedResponse.get("rp").get("name").asText()).isEqualTo("example.com"); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void webauthnWhenRpNameConfiguredUsesRpName() throws Exception { |
| 164 | + ObjectMapper mapper = new ObjectMapper(); |
| 165 | + this.spring.register(CustomRpNameWebauthnConfiguration.class).autowire(); |
| 166 | + String response = this.mvc |
| 167 | + .perform(post("/webauthn/register/options").with(csrf()) |
| 168 | + .with(authentication(new TestingAuthenticationToken("test", "ignored", "ROLE_user")))) |
| 169 | + .andExpect(status().is2xxSuccessful()) |
| 170 | + .andReturn() |
| 171 | + .getResponse() |
| 172 | + .getContentAsString(); |
| 173 | + |
| 174 | + JsonNode parsedResponse = mapper.readTree(response); |
| 175 | + |
| 176 | + assertThat(parsedResponse.get("rp").get("id").asText()).isEqualTo("example.com"); |
| 177 | + assertThat(parsedResponse.get("rp").get("name").asText()).isEqualTo("Test RP Name"); |
| 178 | + } |
| 179 | + |
140 | 180 | @Test |
141 | 181 | public void webauthnWhenConfiguredAndFormLoginThenDoesServesJavascript() throws Exception { |
142 | 182 | this.spring.register(FormLoginAndNoDefaultRegistrationPageConfiguration.class).autowire(); |
@@ -334,15 +374,32 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
334 | 374 | http |
335 | 375 | .formLogin(Customizer.withDefaults()) |
336 | 376 | .webAuthn((authn) -> authn |
337 | | - .rpId("spring.io") |
338 | | - .rpName("spring") |
| 377 | + .rpId("example.com") |
339 | 378 | ); |
340 | 379 | // @formatter:on |
341 | 380 | return http.build(); |
342 | 381 | } |
343 | 382 |
|
344 | 383 | } |
345 | 384 |
|
| 385 | + @Configuration |
| 386 | + @EnableWebSecurity |
| 387 | + static class CustomRpNameWebauthnConfiguration { |
| 388 | + |
| 389 | + @Bean |
| 390 | + UserDetailsService userDetailsService() { |
| 391 | + return new InMemoryUserDetailsManager(); |
| 392 | + } |
| 393 | + |
| 394 | + @Bean |
| 395 | + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 396 | + return http.formLogin(Customizer.withDefaults()) |
| 397 | + .webAuthn((webauthn) -> webauthn.rpId("example.com").rpName("Test RP Name")) |
| 398 | + .build(); |
| 399 | + } |
| 400 | + |
| 401 | + } |
| 402 | + |
346 | 403 | @Configuration |
347 | 404 | @EnableWebSecurity |
348 | 405 | static class NoFormLoginAndDefaultRegistrationPageConfiguration { |
|
0 commit comments