|
79 | 79 | import org.springframework.security.oauth2.jwt.Jwt; |
80 | 80 | import org.springframework.security.oauth2.jwt.JwtDecoder; |
81 | 81 | import org.springframework.security.oauth2.jwt.NimbusJwtEncoder; |
| 82 | +import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationService; |
82 | 83 | import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService; |
83 | 84 | import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
84 | 85 | import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationCode; |
|
99 | 100 | import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenCustomizer; |
100 | 101 | import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator; |
101 | 102 | import org.springframework.security.web.SecurityFilterChain; |
| 103 | +import org.springframework.security.web.webauthn.api.TestPublicKeyCredentialUserEntities; |
| 104 | +import org.springframework.security.web.webauthn.authentication.WebAuthnAuthentication; |
102 | 105 | import org.springframework.test.web.servlet.MockMvc; |
103 | 106 | import org.springframework.test.web.servlet.MvcResult; |
104 | 107 | import org.springframework.util.LinkedMultiValueMap; |
|
113 | 116 | import static org.mockito.Mockito.spy; |
114 | 117 | import static org.mockito.Mockito.times; |
115 | 118 | import static org.mockito.Mockito.verify; |
| 119 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; |
116 | 120 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; |
117 | 121 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
118 | 122 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
@@ -324,6 +328,52 @@ public void requestWhenRefreshTokenRequestThenIdTokenContainsSidClaim() throws E |
324 | 328 | assertThat(idToken.<String>getClaim("sid")).isEqualTo(sidClaim); |
325 | 329 | } |
326 | 330 |
|
| 331 | + // gh-19202 |
| 332 | + @Test |
| 333 | + public void requestWhenWebAuthnAuthenticationThenIdTokenContainsSidAndAuthTimeClaims() throws Exception { |
| 334 | + this.spring.register(AuthorizationServerConfigurationWithWebAuthn.class).autowire(); |
| 335 | + |
| 336 | + RegisteredClient registeredClient = TestRegisteredClients.registeredClient().scope(OidcScopes.OPENID).build(); |
| 337 | + this.registeredClientRepository.save(registeredClient); |
| 338 | + |
| 339 | + WebAuthnAuthentication webAuthnAuthentication = new WebAuthnAuthentication( |
| 340 | + TestPublicKeyCredentialUserEntities.userEntity().build(), |
| 341 | + List.of(FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY))); |
| 342 | + |
| 343 | + MultiValueMap<String, String> authorizationRequestParameters = getAuthorizationRequestParameters( |
| 344 | + registeredClient); |
| 345 | + MvcResult mvcResult = this.mvc |
| 346 | + .perform(get(DEFAULT_AUTHORIZATION_ENDPOINT_URI).queryParams(authorizationRequestParameters) |
| 347 | + .with(authentication(webAuthnAuthentication))) |
| 348 | + .andExpect(status().is3xxRedirection()) |
| 349 | + .andReturn(); |
| 350 | + String redirectedUrl = mvcResult.getResponse().getRedirectedUrl(); |
| 351 | + assertThat(redirectedUrl).matches(".+\\?code=.{15,}&state=state"); |
| 352 | + |
| 353 | + String authorizationCode = extractParameterFromRedirectUri(redirectedUrl, "code"); |
| 354 | + OAuth2Authorization authorization = this.authorizationService.findByToken(authorizationCode, |
| 355 | + AUTHORIZATION_CODE_TOKEN_TYPE); |
| 356 | + |
| 357 | + mvcResult = this.mvc |
| 358 | + .perform(post(DEFAULT_TOKEN_ENDPOINT_URI).params(getTokenRequestParameters(registeredClient, authorization)) |
| 359 | + .header(HttpHeaders.AUTHORIZATION, |
| 360 | + "Basic " + encodeBasicAuth(registeredClient.getClientId(), registeredClient.getClientSecret()))) |
| 361 | + .andExpect(status().isOk()) |
| 362 | + .andReturn(); |
| 363 | + |
| 364 | + MockHttpServletResponse servletResponse = mvcResult.getResponse(); |
| 365 | + MockClientHttpResponse httpResponse = new MockClientHttpResponse(servletResponse.getContentAsByteArray(), |
| 366 | + HttpStatus.valueOf(servletResponse.getStatus())); |
| 367 | + OAuth2AccessTokenResponse accessTokenResponse = accessTokenHttpResponseConverter |
| 368 | + .read(OAuth2AccessTokenResponse.class, httpResponse); |
| 369 | + |
| 370 | + Jwt idToken = this.jwtDecoder |
| 371 | + .decode((String) accessTokenResponse.getAdditionalParameters().get(OidcParameterNames.ID_TOKEN)); |
| 372 | + |
| 373 | + assertThat(idToken.<String>getClaim("sid")).isNotNull(); |
| 374 | + assertThat(idToken.<Object>getClaim("auth_time")).isNotNull(); |
| 375 | + } |
| 376 | + |
327 | 377 | @Test |
328 | 378 | public void requestWhenLogoutRequestThenLogout() throws Exception { |
329 | 379 | this.spring.register(AuthorizationServerConfiguration.class).autowire(); |
@@ -696,6 +746,23 @@ SessionRegistry sessionRegistry() { |
696 | 746 |
|
697 | 747 | } |
698 | 748 |
|
| 749 | + // gh-19202 |
| 750 | + // Uses InMemoryOAuth2AuthorizationService to avoid Jackson serialization complexity |
| 751 | + // when storing WebAuthnAuthentication (which requires both Jackson 2 and 3 WebAuthn |
| 752 | + // modules to be registered in JdbcOAuth2AuthorizationService). |
| 753 | + @EnableWebSecurity |
| 754 | + @Configuration |
| 755 | + static class AuthorizationServerConfigurationWithWebAuthn extends AuthorizationServerConfiguration { |
| 756 | + |
| 757 | + @Bean |
| 758 | + @Override |
| 759 | + OAuth2AuthorizationService authorizationService(JdbcOperations jdbcOperations, |
| 760 | + RegisteredClientRepository registeredClientRepository) { |
| 761 | + return new InMemoryOAuth2AuthorizationService(); |
| 762 | + } |
| 763 | + |
| 764 | + } |
| 765 | + |
699 | 766 | @EnableWebSecurity |
700 | 767 | @Configuration |
701 | 768 | static class AuthorizationServerConfigurationWithTokenGenerator extends AuthorizationServerConfiguration { |
|
0 commit comments