Skip to content
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

WebAuthnDsl Bug Fix #16339

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* 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.
Expand All @@ -26,18 +26,21 @@ import org.springframework.security.config.annotation.web.configurers.WebAuthnCo
* @property the allowed origins
* @since 6.4
* @author Rob Winch
* @author Max Batischev
*/
@SecurityMarker
class WebAuthnDsl {
var rpName: String? = null
var rpId: String? = null
var allowedOrigins: Set<String>? = null
var disableDefaultRegistrationPage: Boolean? = false

internal fun get(): (WebAuthnConfigurer<HttpSecurity>) -> Unit {
return { webAuthn -> webAuthn
.rpId(rpId)
.rpName(rpName)
.allowedOrigins(allowedOrigins);
return { webAuthn ->
rpName?.also { webAuthn.rpName(rpName) }
rpId?.also { webAuthn.rpId(rpId) }
allowedOrigins?.also { webAuthn.allowedOrigins(allowedOrigins) }
disableDefaultRegistrationPage?.also { webAuthn.disableDefaultRegistrationPage(disableDefaultRegistrationPage!!) }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* 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.
Expand All @@ -16,6 +16,7 @@

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

import org.hamcrest.Matchers
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -30,7 +31,9 @@ import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.provisioning.InMemoryUserDetailsManager
import org.springframework.security.web.SecurityFilterChain
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.get
import org.springframework.test.web.servlet.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers

/**
* Tests for [WebAuthnDsl]
Expand Down Expand Up @@ -80,4 +83,74 @@ class WebAuthnDslTests {
return InMemoryUserDetailsManager(userDetails)
}
}

@Test
fun `webauthn and formLogin configured with default registration page`() {
spring.register(DefaultWebauthnConfig::class.java).autowire()

this.mockMvc.get("/login/webauthn.js")
.andExpect {
MockMvcResultMatchers.status().isOk
header {
string("content-type", "text/javascript;charset=UTF-8")
}
content {
string(Matchers.containsString("async function authenticate("))
}
}
}

@Test
fun `webauthn and formLogin configured with disabled default registration page`() {
spring.register(FormLoginAndNoDefaultRegistrationPageConfiguration::class.java).autowire()

this.mockMvc.get("/login/webauthn.js")
.andExpect {
MockMvcResultMatchers.status().isOk
header {
string("content-type", "text/javascript;charset=UTF-8")
}
content {
string(Matchers.containsString("async function authenticate("))
}
}
}

@Configuration
@EnableWebSecurity
open class DefaultWebauthnConfig {
@Bean
open fun userDetailsService(): UserDetailsService =
InMemoryUserDetailsManager()


@Bean
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http{
formLogin { }
webAuthn { }
}
return http.build()
}
}

@Configuration
@EnableWebSecurity
open class FormLoginAndNoDefaultRegistrationPageConfiguration {
@Bean
open fun userDetailsService(): UserDetailsService =
InMemoryUserDetailsManager()


@Bean
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http{
formLogin { }
webAuthn {
disableDefaultRegistrationPage = true
}
}
return http.build()
}
}
}
Loading