|
@@ -29,6 +29,8 @@ import java.util.stream.Collectors;
|
|
|
import com.nimbusds.jose.jwk.JWKSet;
|
|
|
import com.nimbusds.jose.jwk.source.JWKSource;
|
|
|
import com.nimbusds.jose.proc.SecurityContext;
|
|
|
+import org.junit.After;
|
|
|
+import org.junit.AfterClass;
|
|
|
import org.junit.BeforeClass;
|
|
|
import org.junit.Rule;
|
|
|
import org.junit.Test;
|
|
@@ -39,8 +41,14 @@ import org.springframework.context.annotation.Import;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
|
+import org.springframework.jdbc.core.JdbcOperations;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
|
|
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
|
|
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
|
|
import org.springframework.mock.http.client.MockClientHttpResponse;
|
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
+import org.springframework.security.authentication.TestingAuthenticationToken;
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
import org.springframework.security.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration;
|
|
@@ -61,8 +69,8 @@ import org.springframework.security.oauth2.jwt.Jwt;
|
|
|
import org.springframework.security.oauth2.jwt.JwtDecoder;
|
|
|
import org.springframework.security.oauth2.jwt.JwtEncoder;
|
|
|
import org.springframework.security.oauth2.jwt.NimbusJwsEncoder;
|
|
|
-import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationConsentService;
|
|
|
-import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationService;
|
|
|
+import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationConsentService;
|
|
|
+import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService;
|
|
|
import org.springframework.security.oauth2.server.authorization.JwtEncodingContext;
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationCode;
|
|
@@ -70,11 +78,12 @@ import org.springframework.security.oauth2.server.authorization.OAuth2Authorizat
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer;
|
|
|
import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations;
|
|
|
-import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository;
|
|
|
+import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository;
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
|
|
|
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
|
|
|
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
|
|
|
+import org.springframework.security.oauth2.server.authorization.jackson2.TestingAuthenticationTokenMixin;
|
|
|
import org.springframework.security.oauth2.server.authorization.web.OAuth2AuthorizationEndpointFilter;
|
|
|
import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenEndpointFilter;
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
@@ -111,6 +120,7 @@ public class OAuth2AuthorizationCodeGrantTests {
|
|
|
private static final OAuth2TokenType AUTHORIZATION_CODE_TOKEN_TYPE = new OAuth2TokenType(OAuth2ParameterNames.CODE);
|
|
|
private static final OAuth2TokenType STATE_TOKEN_TYPE = new OAuth2TokenType(OAuth2ParameterNames.STATE);
|
|
|
|
|
|
+ private static EmbeddedDatabase db;
|
|
|
private static JWKSource<SecurityContext> jwkSource;
|
|
|
private static NimbusJwsEncoder jwtEncoder;
|
|
|
private static ProviderSettings providerSettings;
|
|
@@ -124,6 +134,9 @@ public class OAuth2AuthorizationCodeGrantTests {
|
|
|
@Autowired
|
|
|
private MockMvc mvc;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private JdbcOperations jdbcOperations;
|
|
|
+
|
|
|
@Autowired
|
|
|
private RegisteredClientRepository registeredClientRepository;
|
|
|
|
|
@@ -141,6 +154,26 @@ public class OAuth2AuthorizationCodeGrantTests {
|
|
|
providerSettings = new ProviderSettings()
|
|
|
.authorizationEndpoint("/test/authorize")
|
|
|
.tokenEndpoint("/test/token");
|
|
|
+ db = new EmbeddedDatabaseBuilder()
|
|
|
+ .generateUniqueName(true)
|
|
|
+ .setType(EmbeddedDatabaseType.HSQL)
|
|
|
+ .setScriptEncoding("UTF-8")
|
|
|
+ .addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql")
|
|
|
+ .addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql")
|
|
|
+ .addScript("org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql")
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @After
|
|
|
+ public void tearDown() {
|
|
|
+ jdbcOperations.update("truncate table oauth2_authorization");
|
|
|
+ jdbcOperations.update("truncate table oauth2_authorization_consent");
|
|
|
+ jdbcOperations.update("truncate table oauth2_registered_client");
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterClass
|
|
|
+ public static void destroy() {
|
|
|
+ db.shutdown();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -485,25 +518,26 @@ public class OAuth2AuthorizationCodeGrantTests {
|
|
|
static class AuthorizationServerConfiguration {
|
|
|
|
|
|
@Bean
|
|
|
- OAuth2AuthorizationService authorizationService() {
|
|
|
- return new InMemoryOAuth2AuthorizationService();
|
|
|
+ OAuth2AuthorizationService authorizationService(JdbcOperations jdbcOperations, RegisteredClientRepository registeredClientRepository) {
|
|
|
+ JdbcOAuth2AuthorizationService authorizationService = new JdbcOAuth2AuthorizationService(jdbcOperations, registeredClientRepository);
|
|
|
+ authorizationService.setAuthorizationRowMapper(new RowMapper(registeredClientRepository));
|
|
|
+ authorizationService.setAuthorizationParametersMapper(new ParametersMapper());
|
|
|
+ return authorizationService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ OAuth2AuthorizationConsentService authorizationConsentService(JdbcOperations jdbcOperations, RegisteredClientRepository registeredClientRepository) {
|
|
|
+ return new JdbcOAuth2AuthorizationConsentService(jdbcOperations, registeredClientRepository);
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
- OAuth2AuthorizationConsentService authorizationConsentService() {
|
|
|
- return new InMemoryOAuth2AuthorizationConsentService();
|
|
|
+ RegisteredClientRepository registeredClientRepository(JdbcOperations jdbcOperations) {
|
|
|
+ return new JdbcRegisteredClientRepository(jdbcOperations);
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
- RegisteredClientRepository registeredClientRepository() {
|
|
|
- // @formatter:off
|
|
|
- RegisteredClient dummyClient = TestRegisteredClients.registeredClient()
|
|
|
- .id("dummy-client")
|
|
|
- .clientId("dummy-client")
|
|
|
- .clientSecret("dummy-secret")
|
|
|
- .build();
|
|
|
- // @formatter:on
|
|
|
- return new InMemoryRegisteredClientRepository(dummyClient);
|
|
|
+ JdbcOperations jdbcOperations() {
|
|
|
+ return new JdbcTemplate(db);
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
@@ -530,6 +564,24 @@ public class OAuth2AuthorizationCodeGrantTests {
|
|
|
return NoOpPasswordEncoder.getInstance();
|
|
|
}
|
|
|
|
|
|
+ static class RowMapper extends JdbcOAuth2AuthorizationService.OAuth2AuthorizationRowMapper {
|
|
|
+
|
|
|
+ RowMapper(RegisteredClientRepository registeredClientRepository) {
|
|
|
+ super(registeredClientRepository);
|
|
|
+ getObjectMapper().addMixIn(TestingAuthenticationToken.class, TestingAuthenticationTokenMixin.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ static class ParametersMapper extends JdbcOAuth2AuthorizationService.OAuth2AuthorizationParametersMapper {
|
|
|
+
|
|
|
+ ParametersMapper() {
|
|
|
+ super();
|
|
|
+ getObjectMapper().addMixIn(TestingAuthenticationToken.class, TestingAuthenticationTokenMixin.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@EnableWebSecurity
|