-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add DefaultResourcesFilter.webauthn() #15970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rwinch
merged 3 commits into
spring-projects:main
from
Kehrlann:webauthn-default-resource-filter
Nov 14, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
...g/springframework/security/config/annotation/web/configurers/WebAuthnConfigurerTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.config.annotation.web.configurers; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.Customizer; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.test.SpringTestContext; | ||
import org.springframework.security.config.test.SpringTestContextExtension; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.provisioning.InMemoryUserDetailsManager; | ||
import org.springframework.security.web.FilterChainProxy; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.ui.DefaultResourcesFilter; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
/** | ||
* @author Daniel Garnier-Moiroux | ||
*/ | ||
@ExtendWith(SpringTestContextExtension.class) | ||
public class WebAuthnConfigurerTests { | ||
|
||
public final SpringTestContext spring = new SpringTestContext(this); | ||
|
||
@Autowired | ||
MockMvc mvc; | ||
|
||
@Test | ||
public void webauthnWhenConfiguredConfiguredThenServesJavascript() throws Exception { | ||
this.spring.register(DefaultWebauthnConfiguration.class).autowire(); | ||
this.mvc.perform(get("/login/webauthn.js")) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().string("content-type", "text/javascript;charset=UTF-8")) | ||
.andExpect(content().string(containsString("async function authenticate("))); | ||
} | ||
|
||
@Test | ||
public void webauthnWhenConfiguredConfiguredThenServesCss() throws Exception { | ||
this.spring.register(DefaultWebauthnConfiguration.class).autowire(); | ||
this.mvc.perform(get("/default-ui.css")) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().string("content-type", "text/css;charset=UTF-8")) | ||
.andExpect(content().string(containsString("body {"))); | ||
} | ||
|
||
@Test | ||
public void webauthnWhenNoFormLoginAndDefaultRegistrationPageConfiguredThenServesJavascript() throws Exception { | ||
this.spring.register(NoFormLoginAndDefaultRegistrationPageConfiguration.class).autowire(); | ||
this.mvc.perform(get("/login/webauthn.js")) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().string("content-type", "text/javascript;charset=UTF-8")) | ||
.andExpect(content().string(containsString("async function authenticate("))); | ||
} | ||
|
||
@Test | ||
public void webauthnWhenNoFormLoginAndDefaultRegistrationPageConfiguredThenServesCss() throws Exception { | ||
this.spring.register(NoFormLoginAndDefaultRegistrationPageConfiguration.class).autowire(); | ||
this.mvc.perform(get("/default-ui.css")) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().string("content-type", "text/css;charset=UTF-8")) | ||
.andExpect(content().string(containsString("body {"))); | ||
} | ||
|
||
@Test | ||
public void webauthnWhenFormLoginAndDefaultRegistrationPageConfiguredThenNoDuplicateFilters() { | ||
this.spring.register(DefaultWebauthnConfiguration.class).autowire(); | ||
FilterChainProxy filterChain = this.spring.getContext().getBean(FilterChainProxy.class); | ||
|
||
List<DefaultResourcesFilter> defaultResourcesFilters = filterChain.getFilterChains() | ||
.get(0) | ||
.getFilters() | ||
.stream() | ||
.filter(DefaultResourcesFilter.class::isInstance) | ||
.map(DefaultResourcesFilter.class::cast) | ||
.toList(); | ||
|
||
assertThat(defaultResourcesFilters).map(DefaultResourcesFilter::toString) | ||
.filteredOn((filterDescription) -> filterDescription.contains("login/webauthn.js")) | ||
.hasSize(1); | ||
assertThat(defaultResourcesFilters).map(DefaultResourcesFilter::toString) | ||
.filteredOn((filterDescription) -> filterDescription.contains("default-ui.css")) | ||
.hasSize(1); | ||
} | ||
|
||
@Test | ||
public void webauthnWhenConfiguredAndFormLoginThenDoesServesJavascript() throws Exception { | ||
this.spring.register(FormLoginAndNoDefaultRegistrationPageConfiguration.class).autowire(); | ||
this.mvc.perform(get("/login/webauthn.js")) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().string("content-type", "text/javascript;charset=UTF-8")) | ||
.andExpect(content().string(containsString("async function authenticate("))); | ||
} | ||
|
||
@Test | ||
public void webauthnWhenConfiguredAndNoDefaultRegistrationPageThenDoesNotServeJavascript() throws Exception { | ||
this.spring.register(NoDefaultRegistrationPageConfiguration.class).autowire(); | ||
this.mvc.perform(get("/login/webauthn.js")).andExpect(status().isNotFound()); | ||
} | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
static class DefaultWebauthnConfiguration { | ||
|
||
@Bean | ||
UserDetailsService userDetailsService() { | ||
return new InMemoryUserDetailsManager(); | ||
} | ||
|
||
@Bean | ||
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
return http.formLogin(Customizer.withDefaults()).webAuthn(Customizer.withDefaults()).build(); | ||
} | ||
|
||
} | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
static class NoFormLoginAndDefaultRegistrationPageConfiguration { | ||
|
||
@Bean | ||
UserDetailsService userDetailsService() { | ||
return new InMemoryUserDetailsManager(); | ||
} | ||
|
||
@Bean | ||
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
return http.webAuthn(Customizer.withDefaults()).build(); | ||
} | ||
|
||
} | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
static class FormLoginAndNoDefaultRegistrationPageConfiguration { | ||
|
||
@Bean | ||
UserDetailsService userDetailsService() { | ||
return new InMemoryUserDetailsManager(); | ||
} | ||
|
||
@Bean | ||
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
return http.formLogin(Customizer.withDefaults()) | ||
.webAuthn((webauthn) -> webauthn.disableDefaultRegistrationPage(true)) | ||
.build(); | ||
} | ||
|
||
} | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
static class NoDefaultRegistrationPageConfiguration { | ||
|
||
@Bean | ||
UserDetailsService userDetailsService() { | ||
return new InMemoryUserDetailsManager(); | ||
} | ||
|
||
@Bean | ||
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
return http.formLogin((login) -> login.loginPage("/custom-login-page")) | ||
.webAuthn((webauthn) -> webauthn.disableDefaultRegistrationPage(true)) | ||
.build(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.