|
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 |
|
|
52 | 54 | import static org.mockito.BDDMockito.given; |
53 | 55 | import static org.mockito.BDDMockito.willAnswer; |
54 | 56 | import static org.mockito.Mockito.mock; |
| 57 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; |
| 58 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
55 | 59 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
56 | 60 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
57 | 61 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
@@ -127,6 +131,42 @@ public void webauthnWhenFormLoginAndDefaultRegistrationPageConfiguredThenNoDupli |
127 | 131 | .hasSize(1); |
128 | 132 | } |
129 | 133 |
|
| 134 | + @Test |
| 135 | + void webauthnWhenConfiguredDefaultsRpNameToRpId() throws Exception { |
| 136 | + ObjectMapper mapper = new ObjectMapper(); |
| 137 | + this.spring.register(DefaultWebauthnConfiguration.class).autowire(); |
| 138 | + String response = this.mvc |
| 139 | + .perform(post("/webauthn/register/options").with(csrf()) |
| 140 | + .with(authentication(new TestingAuthenticationToken("test", "ignored", "ROLE_user")))) |
| 141 | + .andExpect(status().is2xxSuccessful()) |
| 142 | + .andReturn() |
| 143 | + .getResponse() |
| 144 | + .getContentAsString(); |
| 145 | + |
| 146 | + JsonNode parsedResponse = mapper.readTree(response); |
| 147 | + |
| 148 | + assertThat(parsedResponse.get("rp").get("id").asText()).isEqualTo("example.com"); |
| 149 | + assertThat(parsedResponse.get("rp").get("name").asText()).isEqualTo("example.com"); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void webauthnWhenRpNameConfiguredUsesRpName() throws Exception { |
| 154 | + ObjectMapper mapper = new ObjectMapper(); |
| 155 | + this.spring.register(CustomRpNameWebauthnConfiguration.class).autowire(); |
| 156 | + String response = this.mvc |
| 157 | + .perform(post("/webauthn/register/options").with(csrf()) |
| 158 | + .with(authentication(new TestingAuthenticationToken("test", "ignored", "ROLE_user")))) |
| 159 | + .andExpect(status().is2xxSuccessful()) |
| 160 | + .andReturn() |
| 161 | + .getResponse() |
| 162 | + .getContentAsString(); |
| 163 | + |
| 164 | + JsonNode parsedResponse = mapper.readTree(response); |
| 165 | + |
| 166 | + assertThat(parsedResponse.get("rp").get("id").asText()).isEqualTo("example.com"); |
| 167 | + assertThat(parsedResponse.get("rp").get("name").asText()).isEqualTo("Test RP Name"); |
| 168 | + } |
| 169 | + |
130 | 170 | @Test |
131 | 171 | public void webauthnWhenConfiguredAndFormLoginThenDoesServesJavascript() throws Exception { |
132 | 172 | this.spring.register(FormLoginAndNoDefaultRegistrationPageConfiguration.class).autowire(); |
@@ -300,7 +340,27 @@ UserDetailsService userDetailsService() { |
300 | 340 |
|
301 | 341 | @Bean |
302 | 342 | SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
303 | | - return http.formLogin(Customizer.withDefaults()).webAuthn(Customizer.withDefaults()).build(); |
| 343 | + return http.formLogin(Customizer.withDefaults()) |
| 344 | + .webAuthn((webauthn) -> webauthn.rpId("example.com")) |
| 345 | + .build(); |
| 346 | + } |
| 347 | + |
| 348 | + } |
| 349 | + |
| 350 | + @Configuration |
| 351 | + @EnableWebSecurity |
| 352 | + static class CustomRpNameWebauthnConfiguration { |
| 353 | + |
| 354 | + @Bean |
| 355 | + UserDetailsService userDetailsService() { |
| 356 | + return new InMemoryUserDetailsManager(); |
| 357 | + } |
| 358 | + |
| 359 | + @Bean |
| 360 | + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 361 | + return http.formLogin(Customizer.withDefaults()) |
| 362 | + .webAuthn((webauthn) -> webauthn.rpId("example.com").rpName("Test RP Name")) |
| 363 | + .build(); |
304 | 364 | } |
305 | 365 |
|
306 | 366 | } |
|
0 commit comments