4343import org .springframework .security .web .webauthn .api .PublicKeyCredentialCreationOptions ;
4444import org .springframework .security .web .webauthn .api .TestPublicKeyCredentialCreationOptions ;
4545import org .springframework .security .web .webauthn .management .WebAuthnRelyingPartyOperations ;
46+ import org .springframework .security .web .webauthn .registration .HttpSessionPublicKeyCredentialCreationOptionsRepository ;
4647import org .springframework .test .web .servlet .MockMvc ;
4748
4849import static org .assertj .core .api .Assertions .assertThat ;
5556import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
5657import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
5758import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .header ;
59+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .request ;
5860import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
5961
6062/**
@@ -141,13 +143,53 @@ public void webauthnWhenConfiguredAndNoDefaultRegistrationPageThenDoesNotServeJa
141143 }
142144
143145 @ Test
144- public void webauthnWhenConfiguredMessageConverter () throws Exception {
146+ public void webauthnWhenConfiguredPublicKeyCredentialCreationOptionsRepository () throws Exception {
147+ TestingAuthenticationToken user = new TestingAuthenticationToken ("user" , "password" , "ROLE_USER" );
148+ SecurityContextHolder .setContext (new SecurityContextImpl (user ));
149+ PublicKeyCredentialCreationOptions options = TestPublicKeyCredentialCreationOptions
150+ .createPublicKeyCredentialCreationOptions ()
151+ .build ();
152+ WebAuthnRelyingPartyOperations rpOperations = mock (WebAuthnRelyingPartyOperations .class );
153+ ConfigCredentialCreationOptionsRepository .rpOperations = rpOperations ;
154+ given (rpOperations .createPublicKeyCredentialCreationOptions (any ())).willReturn (options );
155+ String attrName = "attrName" ;
156+ HttpSessionPublicKeyCredentialCreationOptionsRepository creationOptionsRepository = new HttpSessionPublicKeyCredentialCreationOptionsRepository ();
157+ creationOptionsRepository .setAttrName (attrName );
158+ ConfigCredentialCreationOptionsRepository .creationOptionsRepository = creationOptionsRepository ;
159+ this .spring .register (ConfigCredentialCreationOptionsRepository .class ).autowire ();
160+ this .mvc .perform (post ("/webauthn/register/options" ))
161+ .andExpect (status ().isOk ())
162+ .andExpect (request ().sessionAttribute (attrName , options ));
163+ }
164+
165+ @ Test
166+ public void webauthnWhenConfiguredPublicKeyCredentialCreationOptionsRepositoryBeanPresent () throws Exception {
145167 TestingAuthenticationToken user = new TestingAuthenticationToken ("user" , "password" , "ROLE_USER" );
146168 SecurityContextHolder .setContext (new SecurityContextImpl (user ));
147169 PublicKeyCredentialCreationOptions options = TestPublicKeyCredentialCreationOptions
148170 .createPublicKeyCredentialCreationOptions ()
149171 .build ();
150172 WebAuthnRelyingPartyOperations rpOperations = mock (WebAuthnRelyingPartyOperations .class );
173+ ConfigCredentialCreationOptionsRepositoryFromBean .rpOperations = rpOperations ;
174+ given (rpOperations .createPublicKeyCredentialCreationOptions (any ())).willReturn (options );
175+ String attrName = "attrName" ;
176+ HttpSessionPublicKeyCredentialCreationOptionsRepository creationOptionsRepository = new HttpSessionPublicKeyCredentialCreationOptionsRepository ();
177+ creationOptionsRepository .setAttrName (attrName );
178+ ConfigCredentialCreationOptionsRepositoryFromBean .creationOptionsRepository = creationOptionsRepository ;
179+ this .spring .register (ConfigCredentialCreationOptionsRepositoryFromBean .class ).autowire ();
180+ this .mvc .perform (post ("/webauthn/register/options" ))
181+ .andExpect (status ().isOk ())
182+ .andExpect (request ().sessionAttribute (attrName , options ));
183+ }
184+
185+ @ Test
186+ public void webauthnWhenConfiguredMessageConverter () throws Exception {
187+ TestingAuthenticationToken user = new TestingAuthenticationToken ("user" , "password" , "ROLE_USER" );
188+ SecurityContextHolder .setContext (new SecurityContextImpl (user ));
189+ PublicKeyCredentialCreationOptions options = TestPublicKeyCredentialCreationOptions
190+ .createPublicKeyCredentialCreationOptions ()
191+ .build ();
192+ WebAuthnRelyingPartyOperations rpOperations = mock (WebAuthnRelyingPartyOperations .class );
151193 ConfigMessageConverter .rpOperations = rpOperations ;
152194 given (rpOperations .createPublicKeyCredentialCreationOptions (any ())).willReturn (options );
153195 HttpMessageConverter <Object > converter = mock (HttpMessageConverter .class );
@@ -161,8 +203,65 @@ public void webauthnWhenConfiguredMessageConverter() throws Exception {
161203 ConfigMessageConverter .converter = converter ;
162204 this .spring .register (ConfigMessageConverter .class ).autowire ();
163205 this .mvc .perform (post ("/webauthn/register/options" ))
164- .andExpect (status ().isOk ())
165- .andExpect (content ().string (expectedBody ));
206+ .andExpect (status ().isOk ())
207+ .andExpect (content ().string (expectedBody ));
208+ }
209+
210+ @ Configuration
211+ @ EnableWebSecurity
212+ static class ConfigCredentialCreationOptionsRepository {
213+
214+ private static HttpSessionPublicKeyCredentialCreationOptionsRepository creationOptionsRepository ;
215+
216+ private static WebAuthnRelyingPartyOperations rpOperations ;
217+
218+ @ Bean
219+ WebAuthnRelyingPartyOperations webAuthnRelyingPartyOperations () {
220+ return ConfigCredentialCreationOptionsRepository .rpOperations ;
221+ }
222+
223+ @ Bean
224+ UserDetailsService userDetailsService () {
225+ return new InMemoryUserDetailsManager ();
226+ }
227+
228+ @ Bean
229+ SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
230+ return http .csrf (AbstractHttpConfigurer ::disable )
231+ .webAuthn ((c ) -> c .creationOptionsRepository (creationOptionsRepository ))
232+ .build ();
233+ }
234+
235+ }
236+
237+ @ Configuration
238+ @ EnableWebSecurity
239+ static class ConfigCredentialCreationOptionsRepositoryFromBean {
240+
241+ private static HttpSessionPublicKeyCredentialCreationOptionsRepository creationOptionsRepository ;
242+
243+ private static WebAuthnRelyingPartyOperations rpOperations ;
244+
245+ @ Bean
246+ WebAuthnRelyingPartyOperations webAuthnRelyingPartyOperations () {
247+ return ConfigCredentialCreationOptionsRepositoryFromBean .rpOperations ;
248+ }
249+
250+ @ Bean
251+ UserDetailsService userDetailsService () {
252+ return new InMemoryUserDetailsManager ();
253+ }
254+
255+ @ Bean
256+ HttpSessionPublicKeyCredentialCreationOptionsRepository creationOptionsRepository () {
257+ return ConfigCredentialCreationOptionsRepositoryFromBean .creationOptionsRepository ;
258+ }
259+
260+ @ Bean
261+ SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
262+ return http .csrf (AbstractHttpConfigurer ::disable ).webAuthn (Customizer .withDefaults ()).build ();
263+ }
264+
166265 }
167266
168267 @ Configuration
0 commit comments