Skip to content

Commit b14f2d8

Browse files
committed
1 parent 96a1b77 commit b14f2d8

File tree

5 files changed

+52
-109
lines changed

5 files changed

+52
-109
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationService.java

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -141,9 +141,10 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
141141

142142
private static final String REMOVE_AUTHORIZATION_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + PK_FILTER;
143143

144+
private static int tokenColumnDataType;
145+
144146
private final JdbcOperations jdbcOperations;
145147
private final LobHandler lobHandler;
146-
private static int tokenColumnType;
147148
private RowMapper<OAuth2Authorization> authorizationRowMapper;
148149
private Function<OAuth2Authorization, List<SqlParameterValue>> authorizationParametersMapper;
149150

@@ -172,15 +173,13 @@ public JdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
172173
Assert.notNull(lobHandler, "lobHandler cannot be null");
173174
this.jdbcOperations = jdbcOperations;
174175
this.lobHandler = lobHandler;
175-
tokenColumnType = getColumnDataType(jdbcOperations, "access_token_value");
176176
OAuth2AuthorizationRowMapper authorizationRowMapper = new OAuth2AuthorizationRowMapper(registeredClientRepository);
177177
authorizationRowMapper.setLobHandler(lobHandler);
178178
this.authorizationRowMapper = authorizationRowMapper;
179-
OAuth2AuthorizationParametersMapper authorizationParametersMapper = new OAuth2AuthorizationParametersMapper();
180-
this.authorizationParametersMapper = authorizationParametersMapper;
179+
this.authorizationParametersMapper = new OAuth2AuthorizationParametersMapper();
180+
tokenColumnDataType = getColumnDataType(jdbcOperations, "access_token_value", Types.BLOB);
181181
}
182182

183-
184183
@Override
185184
public void save(OAuth2Authorization authorization) {
186185
Assert.notNull(authorization, "authorization cannot be null");
@@ -259,10 +258,9 @@ public OAuth2Authorization findByToken(String token, @Nullable OAuth2TokenType t
259258
}
260259

261260
private SqlParameterValue mapTokenToSqlParameter(String token) {
262-
if (Types.BLOB == tokenColumnType) {
263-
return new SqlParameterValue(Types.BLOB, token.getBytes(StandardCharsets.UTF_8));
264-
}
265-
return new SqlParameterValue(tokenColumnType, token);
261+
return Types.BLOB == tokenColumnDataType ?
262+
new SqlParameterValue(Types.BLOB, token.getBytes(StandardCharsets.UTF_8)) :
263+
new SqlParameterValue(tokenColumnDataType, token);
266264
}
267265

268266
private OAuth2Authorization findBy(String filter, List<SqlParameterValue> parameters) {
@@ -425,18 +423,16 @@ public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException
425423

426424
private String getTokenValue(ResultSet rs, String tokenColumn) throws SQLException {
427425
String tokenValue = null;
428-
if (Types.CLOB == tokenColumnType) {
426+
if (Types.BLOB == tokenColumnDataType) {
427+
byte[] tokenValueBytes = this.lobHandler.getBlobAsBytes(rs, tokenColumn);
428+
if (tokenValueBytes != null) {
429+
tokenValue = new String(tokenValueBytes, StandardCharsets.UTF_8);
430+
}
431+
} else if (Types.CLOB == tokenColumnDataType) {
429432
tokenValue = this.lobHandler.getClobAsString(rs, tokenColumn);
430-
}
431-
if (Types.VARCHAR == tokenColumnType) {
433+
} else {
432434
tokenValue = rs.getString(tokenColumn);
433435
}
434-
if (Types.BLOB == tokenColumnType) {
435-
byte[] tokenValueByte = this.lobHandler.getBlobAsBytes(rs, tokenColumn);
436-
if (tokenValueByte != null) {
437-
tokenValue = new String(tokenValueByte, StandardCharsets.UTF_8);
438-
}
439-
}
440436
return tokenValue;
441437
}
442438

@@ -559,13 +555,12 @@ private <T extends AbstractOAuth2Token> List<SqlParameterValue> toSqlParameterLi
559555
}
560556
metadata = writeMap(token.getMetadata());
561557
}
562-
if (Types.BLOB == tokenColumnType && StringUtils.hasText(tokenValue)) {
563-
byte[] tokenValueAsBytes = tokenValue.getBytes(StandardCharsets.UTF_8);
564-
parameters.add(new SqlParameterValue(tokenColumnType, tokenValueAsBytes));
558+
if (Types.BLOB == tokenColumnDataType && StringUtils.hasText(tokenValue)) {
559+
byte[] tokenValueBytes = tokenValue.getBytes(StandardCharsets.UTF_8);
560+
parameters.add(new SqlParameterValue(Types.BLOB, tokenValueBytes));
565561
} else {
566-
parameters.add(new SqlParameterValue(tokenColumnType, tokenValue));
562+
parameters.add(new SqlParameterValue(tokenColumnDataType, tokenValue));
567563
}
568-
569564
parameters.add(new SqlParameterValue(Types.TIMESTAMP, tokenIssuedAt));
570565
parameters.add(new SqlParameterValue(Types.TIMESTAMP, tokenExpiresAt));
571566
parameters.add(new SqlParameterValue(Types.VARCHAR, metadata));
@@ -582,22 +577,25 @@ private String writeMap(Map<String, Object> data) {
582577

583578
}
584579

585-
private static int getColumnDataType(JdbcOperations jdbcOperations, String columnName){
586-
return jdbcOperations.execute((ConnectionCallback<Integer>) con -> {
587-
DatabaseMetaData databaseMetaData = con.getMetaData();
588-
ResultSet rs = databaseMetaData.getColumns(null, null, TABLE_NAME, columnName);
589-
if (rs.next()) {
590-
return rs.getInt("DATA_TYPE");
591-
}
592-
// NOTE: When using HSQL: When a database object is created with one of the CREATE statements if the name is enclosed in double quotes, the exact name is used as the case-normal form.
593-
// But if it is not enclosed in double quotes, the name is converted to uppercase and this uppercase version is stored in the database as the case-normal form
594-
rs = databaseMetaData.getColumns(null, null, TABLE_NAME.toUpperCase(), columnName.toUpperCase());
595-
if (rs.next()) {
596-
return rs.getInt("DATA_TYPE");
597-
}
598-
return Types.NULL;
599-
});
600-
}
580+
private static Integer getColumnDataType(JdbcOperations jdbcOperations, String columnName, int defaultDataType) {
581+
return jdbcOperations.execute((ConnectionCallback<Integer>) conn -> {
582+
DatabaseMetaData databaseMetaData = conn.getMetaData();
583+
ResultSet rs = databaseMetaData.getColumns(null, null, TABLE_NAME, columnName);
584+
if (rs.next()) {
585+
return rs.getInt("DATA_TYPE");
586+
}
587+
// NOTE: (Applies to HSQL)
588+
// When a database object is created with one of the CREATE statements or renamed with the ALTER statement,
589+
// if the name is enclosed in double quotes, the exact name is used as the case-normal form.
590+
// But if it is not enclosed in double quotes,
591+
// the name is converted to uppercase and this uppercase version is stored in the database as the case-normal form.
592+
rs = databaseMetaData.getColumns(null, null, TABLE_NAME.toUpperCase(), columnName.toUpperCase());
593+
if (rs.next()) {
594+
return rs.getInt("DATA_TYPE");
595+
}
596+
return defaultDataType;
597+
});
598+
}
601599

602600
private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter {
603601
private final LobCreator lobCreator;

oauth2-authorization-server/src/main/resources/org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema-postgres.sql

Lines changed: 0 additions & 42 deletions
This file was deleted.

oauth2-authorization-server/src/main/resources/org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
IMPORTANT:
3+
If using PostgreSQL, update ALL columns defined with 'blob' to 'text',
4+
as PostgreSQL does not support the 'blob' data type.
5+
*/
16
CREATE TABLE oauth2_authorization (
27
id varchar(100) NOT NULL,
38
registered_client_id varchar(100) NOT NULL,

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,6 @@
4343
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
4444
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4545
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
46-
import org.springframework.jdbc.support.lob.DefaultLobHandler;
4746
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
4847
import org.springframework.security.oauth2.core.AuthorizationGrantType;
4948
import org.springframework.security.oauth2.core.OAuth2AccessToken;
@@ -76,7 +75,7 @@
7675
public class JdbcOAuth2AuthorizationServiceTests {
7776
private static final String OAUTH2_AUTHORIZATION_SCHEMA_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql";
7877
private static final String CUSTOM_OAUTH2_AUTHORIZATION_SCHEMA_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/custom-oauth2-authorization-schema.sql";
79-
private static final String OAUTH2_AUTHORIZATION_SCHEMA_CLOB_COLUMN_TYPE_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/custom-oauth2-authorization-schema-clob-data-type.sql";
78+
private static final String OAUTH2_AUTHORIZATION_SCHEMA_CLOB_DATA_TYPE_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/custom-oauth2-authorization-schema-clob-data-type.sql";
8079
private static final OAuth2TokenType AUTHORIZATION_CODE_TOKEN_TYPE = new OAuth2TokenType(OAuth2ParameterNames.CODE);
8180
private static final OAuth2TokenType STATE_TOKEN_TYPE = new OAuth2TokenType(OAuth2ParameterNames.STATE);
8281
private static final String ID = "id";
@@ -417,12 +416,13 @@ public void tableDefinitionWhenCustomThenAbleToOverride() {
417416
}
418417

419418
@Test
420-
public void tableDefinitionWhenClobSqlTypeThenUpdateAuthorization() {
421-
EmbeddedDatabase db = createDb(OAUTH2_AUTHORIZATION_SCHEMA_CLOB_COLUMN_TYPE_SQL_RESOURCE);
422-
OAuth2AuthorizationService authorizationService =
423-
new JdbcOAuth2AuthorizationService(new JdbcTemplate(db), this.registeredClientRepository);
419+
public void tableDefinitionWhenClobSqlTypeThenAuthorizationUpdated() {
424420
when(this.registeredClientRepository.findById(eq(REGISTERED_CLIENT.getId())))
425421
.thenReturn(REGISTERED_CLIENT);
422+
423+
EmbeddedDatabase db = createDb(OAUTH2_AUTHORIZATION_SCHEMA_CLOB_DATA_TYPE_SQL_RESOURCE);
424+
OAuth2AuthorizationService authorizationService =
425+
new JdbcOAuth2AuthorizationService(new JdbcTemplate(db), this.registeredClientRepository);
426426
OAuth2Authorization originalAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
427427
.id(ID)
428428
.principalName(PRINCIPAL_NAME)
@@ -512,14 +512,11 @@ private static final class CustomJdbcOAuth2AuthorizationService extends JdbcOAut
512512

513513
private CustomJdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
514514
RegisteredClientRepository registeredClientRepository) {
515-
super(jdbcOperations, registeredClientRepository, new DefaultLobHandler());
515+
super(jdbcOperations, registeredClientRepository);
516516
setAuthorizationRowMapper(new CustomOAuth2AuthorizationRowMapper(registeredClientRepository));
517517
setAuthorizationParametersMapper(new CustomOAuth2AuthorizationParametersMapper());
518-
519518
}
520519

521-
522-
523520
@Override
524521
public void save(OAuth2Authorization authorization) {
525522
List<SqlParameterValue> parameters = getAuthorizationParametersMapper().apply(authorization);

oauth2-authorization-server/src/test/resources/org/springframework/security/oauth2/server/authorization/custom-oauth2-authorization-schema-clob-data-type.sql

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
1-
/*
2-
* Copyright 2020-2022 the original author or authors.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
CREATE TABLE oauth2_authorization (
172
id varchar(100) NOT NULL,
183
registered_client_id varchar(100) NOT NULL,
194
principal_name varchar(200) NOT NULL,
205
authorization_grant_type varchar(100) NOT NULL,
21-
attributes varchar(15000) DEFAULT NULL,
6+
attributes varchar(4000) DEFAULT NULL,
227
state varchar(500) DEFAULT NULL,
238
authorization_code_value clob DEFAULT NULL,
249
authorization_code_issued_at timestamp DEFAULT NULL,

0 commit comments

Comments
 (0)