43
43
import org .springframework .security .web .webauthn .api .PublicKeyCredentialCreationOptions ;
44
44
import org .springframework .security .web .webauthn .api .TestPublicKeyCredentialCreationOptions ;
45
45
import org .springframework .security .web .webauthn .management .WebAuthnRelyingPartyOperations ;
46
+ import org .springframework .security .web .webauthn .registration .HttpSessionPublicKeyCredentialCreationOptionsRepository ;
46
47
import org .springframework .test .web .servlet .MockMvc ;
47
48
48
49
import static org .assertj .core .api .Assertions .assertThat ;
55
56
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
56
57
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
57
58
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .header ;
59
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .request ;
58
60
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
59
61
60
62
/**
@@ -141,13 +143,53 @@ public void webauthnWhenConfiguredAndNoDefaultRegistrationPageThenDoesNotServeJa
141
143
}
142
144
143
145
@ 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 {
145
167
TestingAuthenticationToken user = new TestingAuthenticationToken ("user" , "password" , "ROLE_USER" );
146
168
SecurityContextHolder .setContext (new SecurityContextImpl (user ));
147
169
PublicKeyCredentialCreationOptions options = TestPublicKeyCredentialCreationOptions
148
170
.createPublicKeyCredentialCreationOptions ()
149
171
.build ();
150
172
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 );
151
193
ConfigMessageConverter .rpOperations = rpOperations ;
152
194
given (rpOperations .createPublicKeyCredentialCreationOptions (any ())).willReturn (options );
153
195
HttpMessageConverter <Object > converter = mock (HttpMessageConverter .class );
@@ -161,8 +203,65 @@ public void webauthnWhenConfiguredMessageConverter() throws Exception {
161
203
ConfigMessageConverter .converter = converter ;
162
204
this .spring .register (ConfigMessageConverter .class ).autowire ();
163
205
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
+
166
265
}
167
266
168
267
@ Configuration
0 commit comments