Skip to content

Commit a949998

Browse files
Steve Riesenbergsjohnr
Steve Riesenberg
authored andcommitted
Add test to override schema for JdbcOAuth2AuthorizationConsentService
1 parent aa208a2 commit a949998

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationConsentServiceTests.java

+122
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,29 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization;
1717

18+
import java.sql.ResultSet;
19+
import java.sql.SQLException;
20+
import java.sql.Types;
21+
import java.util.List;
22+
1823
import org.junit.After;
1924
import org.junit.Before;
2025
import org.junit.Test;
26+
27+
import org.springframework.dao.DataRetrievalFailureException;
28+
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
2129
import org.springframework.jdbc.core.JdbcOperations;
2230
import org.springframework.jdbc.core.JdbcTemplate;
31+
import org.springframework.jdbc.core.PreparedStatementSetter;
32+
import org.springframework.jdbc.core.SqlParameterValue;
2333
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
2434
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2535
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
2636
import org.springframework.security.core.authority.SimpleGrantedAuthority;
2737
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
2838
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
2939
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
40+
import org.springframework.util.StringUtils;
3041

3142
import static org.assertj.core.api.Assertions.assertThat;
3243
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -47,6 +58,7 @@
4758
public class JdbcOAuth2AuthorizationConsentServiceTests {
4859

4960
private static final String OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql";
61+
private static final String CUSTOM_OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/custom-oauth2-authorization-consent-schema.sql";
5062
private static final String PRINCIPAL_NAME = "principal-name";
5163
private static final RegisteredClient REGISTERED_CLIENT = TestRegisteredClients.registeredClient().build();
5264

@@ -200,6 +212,23 @@ public void findByIdWhenAuthorizationConsentDoesNotExistThenNull() {
200212
assertThat(this.authorizationConsentService.findById(REGISTERED_CLIENT.getId(), "unknown-user")).isNull();
201213
}
202214

215+
@Test
216+
public void tableDefinitionWhenCustomThenAbleToOverride() {
217+
when(this.registeredClientRepository.findById(eq(REGISTERED_CLIENT.getId())))
218+
.thenReturn(REGISTERED_CLIENT);
219+
220+
EmbeddedDatabase db = createDb(CUSTOM_OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE);
221+
OAuth2AuthorizationConsentService authorizationConsentService =
222+
new CustomJdbcOAuth2AuthorizationConsentService(new JdbcTemplate(db), this.registeredClientRepository);
223+
authorizationConsentService.save(AUTHORIZATION_CONSENT);
224+
OAuth2AuthorizationConsent foundAuthorizationConsent1 = authorizationConsentService.findById(AUTHORIZATION_CONSENT.getRegisteredClientId(), AUTHORIZATION_CONSENT.getPrincipalName());
225+
assertThat(foundAuthorizationConsent1).isEqualTo(AUTHORIZATION_CONSENT);
226+
authorizationConsentService.remove(AUTHORIZATION_CONSENT);
227+
OAuth2AuthorizationConsent foundAuthorizationConsent2 = authorizationConsentService.findById(REGISTERED_CLIENT.getClientId(), AUTHORIZATION_CONSENT.getPrincipalName());
228+
assertThat(foundAuthorizationConsent2).isNull();
229+
db.shutdown();
230+
}
231+
203232
@Before
204233
public void setUp() {
205234
this.db = createDb();
@@ -216,6 +245,7 @@ public void tearDown() {
216245
private static EmbeddedDatabase createDb() {
217246
return createDb(OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE);
218247
}
248+
219249
private static EmbeddedDatabase createDb(String schema) {
220250
// @formatter:off
221251
return new EmbeddedDatabaseBuilder()
@@ -226,4 +256,96 @@ private static EmbeddedDatabase createDb(String schema) {
226256
.build();
227257
// @formatter:on
228258
}
259+
260+
private static final class CustomJdbcOAuth2AuthorizationConsentService extends JdbcOAuth2AuthorizationConsentService {
261+
262+
// @formatter:off
263+
private static final String COLUMN_NAMES = "registeredClientId, "
264+
+ "principalName, "
265+
+ "authorities";
266+
// @formatter:on
267+
268+
private static final String TABLE_NAME = "oauth2AuthorizationConsent";
269+
270+
private static final String PK_FILTER = "registeredClientId = ? AND principalName = ?";
271+
272+
// @formatter:off
273+
private static final String LOAD_AUTHORIZATION_CONSENT_SQL = "SELECT " + COLUMN_NAMES
274+
+ " FROM " + TABLE_NAME
275+
+ " WHERE " + PK_FILTER;
276+
// @formatter:on
277+
278+
// @formatter:off
279+
private static final String SAVE_AUTHORIZATION_CONSENT_SQL = "INSERT INTO " + TABLE_NAME
280+
+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?)";
281+
// @formatter:on
282+
283+
private static final String REMOVE_AUTHORIZATION_CONSENT_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + PK_FILTER;
284+
285+
CustomJdbcOAuth2AuthorizationConsentService(JdbcOperations jdbcOperations, RegisteredClientRepository registeredClientRepository) {
286+
super(jdbcOperations, registeredClientRepository);
287+
setAuthorizationConsentRowMapper(new CustomOAuth2AuthorizationConsentRowMapper(registeredClientRepository));
288+
}
289+
290+
@Override
291+
public void save(OAuth2AuthorizationConsent authorizationConsent) {
292+
List<SqlParameterValue> parameters = getAuthorizationConsentParametersMapper().apply(authorizationConsent);
293+
PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters.toArray());
294+
getJdbcOperations().update(SAVE_AUTHORIZATION_CONSENT_SQL, pss);
295+
}
296+
297+
@Override
298+
public void remove(OAuth2AuthorizationConsent authorizationConsent) {
299+
SqlParameterValue[] parameters = new SqlParameterValue[] {
300+
new SqlParameterValue(Types.VARCHAR, authorizationConsent.getRegisteredClientId()),
301+
new SqlParameterValue(Types.VARCHAR, authorizationConsent.getPrincipalName())
302+
};
303+
PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters);
304+
getJdbcOperations().update(REMOVE_AUTHORIZATION_CONSENT_SQL, pss);
305+
}
306+
307+
@Override
308+
public OAuth2AuthorizationConsent findById(String registeredClientId, String principalName) {
309+
SqlParameterValue[] parameters = new SqlParameterValue[] {
310+
new SqlParameterValue(Types.VARCHAR, registeredClientId),
311+
new SqlParameterValue(Types.VARCHAR, principalName)};
312+
PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters);
313+
List<OAuth2AuthorizationConsent> result = getJdbcOperations().query(LOAD_AUTHORIZATION_CONSENT_SQL, pss,
314+
getAuthorizationConsentRowMapper());
315+
return !result.isEmpty() ? result.get(0) : null;
316+
}
317+
318+
private static final class CustomOAuth2AuthorizationConsentRowMapper extends JdbcOAuth2AuthorizationConsentService.OAuth2AuthorizationConsentRowMapper {
319+
320+
CustomOAuth2AuthorizationConsentRowMapper(RegisteredClientRepository registeredClientRepository) {
321+
super(registeredClientRepository);
322+
}
323+
324+
@Override
325+
public OAuth2AuthorizationConsent mapRow(ResultSet rs, int rowNum) throws SQLException {
326+
String registeredClientId = rs.getString("registeredClientId");
327+
328+
RegisteredClient registeredClient = getRegisteredClientRepository()
329+
.findById(registeredClientId);
330+
if (registeredClient == null) {
331+
throw new DataRetrievalFailureException(
332+
"The RegisteredClient with id '" + registeredClientId + "' was not found in the RegisteredClientRepository.");
333+
}
334+
335+
String principalName = rs.getString("principalName");
336+
337+
OAuth2AuthorizationConsent.Builder builder = OAuth2AuthorizationConsent.withId(registeredClientId, principalName);
338+
String authorizationConsentAuthorities = rs.getString("authorities");
339+
if (authorizationConsentAuthorities != null) {
340+
for (String authority : StringUtils.commaDelimitedListToSet(authorizationConsentAuthorities)) {
341+
builder.authority(new SimpleGrantedAuthority(authority));
342+
}
343+
}
344+
return builder.build();
345+
}
346+
347+
}
348+
349+
}
350+
229351
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE oauth2AuthorizationConsent (
2+
registeredClientId varchar(100) NOT NULL,
3+
principalName varchar(200) NOT NULL,
4+
authorities varchar(1000) NOT NULL,
5+
PRIMARY KEY (registeredClientId, principalName)
6+
);

0 commit comments

Comments
 (0)