Skip to content

Commit aca5fc8

Browse files
WebAuthnDsl Bug Fix
Closes spring-projectsgh-16338
1 parent fe9edc8 commit aca5fc8

File tree

2 files changed

+82
-6
lines changed
  • config/src

2 files changed

+82
-6
lines changed

config/src/main/kotlin/org/springframework/security/config/annotation/web/WebAuthnDsl.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,18 +26,21 @@ import org.springframework.security.config.annotation.web.configurers.WebAuthnCo
2626
* @property the allowed origins
2727
* @since 6.4
2828
* @author Rob Winch
29+
* @author Max Batischev
2930
*/
3031
@SecurityMarker
3132
class WebAuthnDsl {
3233
var rpName: String? = null
3334
var rpId: String? = null
3435
var allowedOrigins: Set<String>? = null
36+
var disableDefaultRegistrationPage: Boolean? = false
3537

3638
internal fun get(): (WebAuthnConfigurer<HttpSecurity>) -> Unit {
37-
return { webAuthn -> webAuthn
38-
.rpId(rpId)
39-
.rpName(rpName)
40-
.allowedOrigins(allowedOrigins);
39+
return { webAuthn ->
40+
rpName?.also { webAuthn.rpName(rpName) }
41+
rpId?.also { webAuthn.rpId(rpId) }
42+
allowedOrigins?.also { webAuthn.allowedOrigins(allowedOrigins) }
43+
disableDefaultRegistrationPage?.also { webAuthn.disableDefaultRegistrationPage(disableDefaultRegistrationPage!!) }
4144
}
4245
}
4346
}

config/src/test/kotlin/org/springframework/security/config/annotation/web/WebAuthnDslTests.kt

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.config.annotation.web
1818

19+
import org.hamcrest.Matchers
1920
import org.junit.jupiter.api.Test
2021
import org.junit.jupiter.api.extension.ExtendWith
2122
import org.springframework.beans.factory.annotation.Autowired
@@ -30,7 +31,9 @@ import org.springframework.security.core.userdetails.UserDetailsService
3031
import org.springframework.security.provisioning.InMemoryUserDetailsManager
3132
import org.springframework.security.web.SecurityFilterChain
3233
import org.springframework.test.web.servlet.MockMvc
34+
import org.springframework.test.web.servlet.get
3335
import org.springframework.test.web.servlet.post
36+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
3437

3538
/**
3639
* Tests for [WebAuthnDsl]
@@ -80,4 +83,74 @@ class WebAuthnDslTests {
8083
return InMemoryUserDetailsManager(userDetails)
8184
}
8285
}
86+
87+
@Test
88+
fun `webauthn and formLogin configured with default registration page`() {
89+
spring.register(DefaultWebauthnConfig::class.java).autowire()
90+
91+
this.mockMvc.get("/login/webauthn.js")
92+
.andExpect {
93+
MockMvcResultMatchers.status().isOk
94+
header {
95+
string("content-type", "text/javascript;charset=UTF-8")
96+
}
97+
content {
98+
string(Matchers.containsString("async function authenticate("))
99+
}
100+
}
101+
}
102+
103+
@Test
104+
fun `webauthn and formLogin configured with disabled default registration page`() {
105+
spring.register(FormLoginAndNoDefaultRegistrationPageConfiguration::class.java).autowire()
106+
107+
this.mockMvc.get("/login/webauthn.js")
108+
.andExpect {
109+
MockMvcResultMatchers.status().isOk
110+
header {
111+
string("content-type", "text/javascript;charset=UTF-8")
112+
}
113+
content {
114+
string(Matchers.containsString("async function authenticate("))
115+
}
116+
}
117+
}
118+
119+
@Configuration
120+
@EnableWebSecurity
121+
open class DefaultWebauthnConfig {
122+
@Bean
123+
open fun userDetailsService(): UserDetailsService =
124+
InMemoryUserDetailsManager()
125+
126+
127+
@Bean
128+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
129+
http{
130+
formLogin { }
131+
webAuthn { }
132+
}
133+
return http.build()
134+
}
135+
}
136+
137+
@Configuration
138+
@EnableWebSecurity
139+
open class FormLoginAndNoDefaultRegistrationPageConfiguration {
140+
@Bean
141+
open fun userDetailsService(): UserDetailsService =
142+
InMemoryUserDetailsManager()
143+
144+
145+
@Bean
146+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
147+
http{
148+
formLogin { }
149+
webAuthn {
150+
disableDefaultRegistrationPage = true
151+
}
152+
}
153+
return http.build()
154+
}
155+
}
83156
}

0 commit comments

Comments
 (0)