Skip to content

Commit 9787794

Browse files
committed
Polish JdbcOAuth2AuthorizationService
Issue gh-304
1 parent 41dd689 commit 9787794

File tree

4 files changed

+19
-27
lines changed

4 files changed

+19
-27
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161

6262
/**
6363
* A JDBC implementation of an {@link OAuth2AuthorizationService} that uses a
64-
* <p>
6564
* {@link JdbcOperations} for {@link OAuth2Authorization} persistence.
6665
*
6766
* <p>
@@ -71,11 +70,11 @@
7170
* therefore MUST be defined in the database schema.
7271
*
7372
* @author Ovidiu Popa
73+
* @since 0.1.2
7474
* @see OAuth2AuthorizationService
7575
* @see OAuth2Authorization
7676
* @see JdbcOperations
7777
* @see RowMapper
78-
* @since 0.1.2
7978
*/
8079
public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationService {
8180

@@ -110,8 +109,7 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
110109

111110
private static final String PK_FILTER = "id = ?";
112111
private static final String UNKNOWN_TOKEN_TYPE_FILTER = "state = ? OR authorization_code_value = ? OR " +
113-
"access_token_value = ? OR " +
114-
"refresh_token_value = ?";
112+
"access_token_value = ? OR refresh_token_value = ?";
115113

116114
private static final String STATE_FILTER = "state = ?";
117115
private static final String AUTHORIZATION_CODE_FILTER = "authorization_code_value = ?";
@@ -126,7 +124,7 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
126124

127125
// @formatter:off
128126
private static final String SAVE_AUTHORIZATION_SQL = "INSERT INTO " + TABLE_NAME
129-
+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?)";
127+
+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
130128
// @formatter:on
131129

132130
// @formatter:off
@@ -180,7 +178,6 @@ public JdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
180178
@Override
181179
public void save(OAuth2Authorization authorization) {
182180
Assert.notNull(authorization, "authorization cannot be null");
183-
184181
OAuth2Authorization existingAuthorization = findById(authorization.getId());
185182
if (existingAuthorization == null) {
186183
insertAuthorization(authorization);
@@ -529,7 +526,6 @@ private <T extends AbstractOAuth2Token> List<SqlParameterValue> toSqlParameterLi
529526
if (token.getToken().getIssuedAt() != null) {
530527
tokenIssuedAt = Timestamp.from(token.getToken().getIssuedAt());
531528
}
532-
533529
if (token.getToken().getExpiresAt() != null) {
534530
tokenExpiresAt = Timestamp.from(token.getToken().getExpiresAt());
535531
}
@@ -553,7 +549,6 @@ private String writeMap(Map<String, Object> data) {
553549
}
554550

555551
private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter {
556-
557552
private final LobCreator lobCreator;
558553

559554
private LobCreatorArgumentPreparedStatementSetter(LobCreator lobCreator, Object[] args) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ CREATE TABLE oauth2_authorization (
44
principal_name varchar(200) NOT NULL,
55
authorization_grant_type varchar(100) NOT NULL,
66
attributes varchar(4000) DEFAULT NULL,
7-
state varchar(1000) DEFAULT NULL,
7+
state varchar(500) DEFAULT NULL,
88
authorization_code_value blob DEFAULT NULL,
99
authorization_code_issued_at timestamp DEFAULT NULL,
1010
authorization_code_expires_at timestamp DEFAULT NULL,
11-
authorization_code_metadata varchar(1000) DEFAULT NULL,
11+
authorization_code_metadata varchar(2000) DEFAULT NULL,
1212
access_token_value blob DEFAULT NULL,
1313
access_token_issued_at timestamp DEFAULT NULL,
1414
access_token_expires_at timestamp DEFAULT NULL,
15-
access_token_metadata varchar(1000) DEFAULT NULL,
15+
access_token_metadata varchar(2000) DEFAULT NULL,
1616
access_token_type varchar(100) DEFAULT NULL,
1717
access_token_scopes varchar(1000) DEFAULT NULL,
1818
oidc_id_token_value blob DEFAULT NULL,
1919
oidc_id_token_issued_at timestamp DEFAULT NULL,
2020
oidc_id_token_expires_at timestamp DEFAULT NULL,
21-
oidc_id_token_metadata varchar(1000) DEFAULT NULL,
21+
oidc_id_token_metadata varchar(2000) DEFAULT NULL,
2222
refresh_token_value blob DEFAULT NULL,
2323
refresh_token_issued_at timestamp DEFAULT NULL,
2424
refresh_token_expires_at timestamp DEFAULT NULL,
25-
refresh_token_metadata varchar(1000) DEFAULT NULL,
25+
refresh_token_metadata varchar(2000) DEFAULT NULL,
2626
PRIMARY KEY (id)
2727
);

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.function.Function;
3030

3131
import com.fasterxml.jackson.core.type.TypeReference;
32-
import com.fasterxml.jackson.databind.ObjectMapper;
3332
import org.junit.After;
3433
import org.junit.Before;
3534
import org.junit.Test;
@@ -210,7 +209,7 @@ public void saveLoadAuthorizationWhenCustomStrategiesSetThenCalled() throws Exce
210209
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
211210
.token(AUTHORIZATION_CODE)
212211
.build();
213-
ObjectMapper objectMapper = new ObjectMapper();
212+
214213
RowMapper<OAuth2Authorization> authorizationRowMapper = spy(
215214
new JdbcOAuth2AuthorizationService.OAuth2AuthorizationRowMapper(
216215
this.registeredClientRepository));
@@ -252,7 +251,7 @@ public void removeWhenAuthorizationProvidedThenRemoved() {
252251
AUTHORIZATION_CODE.getTokenValue(), AUTHORIZATION_CODE_TOKEN_TYPE);
253252
assertThat(authorization).isEqualTo(expectedAuthorization);
254253

255-
this.authorizationService.remove(expectedAuthorization);
254+
this.authorizationService.remove(authorization);
256255
authorization = this.authorizationService.findByToken(
257256
AUTHORIZATION_CODE.getTokenValue(), AUTHORIZATION_CODE_TOKEN_TYPE);
258257
assertThat(authorization).isNull();
@@ -463,8 +462,7 @@ private static final class CustomJdbcOAuth2AuthorizationService extends JdbcOAut
463462

464463
private static final String PK_FILTER = "id = ?";
465464
private static final String UNKNOWN_TOKEN_TYPE_FILTER = "state = ? OR authorizationCodeValue = ? OR " +
466-
"accessTokenValue = ? OR " +
467-
"refreshTokenValue = ?";
465+
"accessTokenValue = ? OR refreshTokenValue = ?";
468466

469467
// @formatter:off
470468
private static final String LOAD_AUTHORIZATION_SQL = "SELECT " + COLUMN_NAMES
@@ -474,12 +472,12 @@ private static final class CustomJdbcOAuth2AuthorizationService extends JdbcOAut
474472

475473
// @formatter:off
476474
private static final String SAVE_AUTHORIZATION_SQL = "INSERT INTO " + TABLE_NAME
477-
+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?)";
475+
+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
478476
// @formatter:on
479477

480478
private static final String REMOVE_AUTHORIZATION_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + PK_FILTER;
481479

482-
CustomJdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
480+
private CustomJdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
483481
RegisteredClientRepository registeredClientRepository) {
484482
super(jdbcOperations, registeredClientRepository);
485483
setAuthorizationRowMapper(new CustomOAuth2AuthorizationRowMapper(registeredClientRepository));
@@ -520,7 +518,7 @@ private OAuth2Authorization findBy(String filter, Object... args) {
520518

521519
private static final class CustomOAuth2AuthorizationRowMapper extends JdbcOAuth2AuthorizationService.OAuth2AuthorizationRowMapper {
522520

523-
CustomOAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository) {
521+
private CustomOAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository) {
524522
super(registeredClientRepository);
525523
}
526524

@@ -682,7 +680,6 @@ private <T extends AbstractOAuth2Token> List<SqlParameterValue> toSqlParameterLi
682680
if (token.getToken().getIssuedAt() != null) {
683681
tokenIssuedAt = Timestamp.from(token.getToken().getIssuedAt());
684682
}
685-
686683
if (token.getToken().getExpiresAt() != null) {
687684
tokenExpiresAt = Timestamp.from(token.getToken().getExpiresAt());
688685
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ CREATE TABLE oauth2Authorization (
44
principalName varchar(200) NOT NULL,
55
authorizationGrantType varchar(100) NOT NULL,
66
attributes varchar(4000) DEFAULT NULL,
7-
state varchar(1000) DEFAULT NULL,
7+
state varchar(500) DEFAULT NULL,
88
authorizationCodeValue varchar(1000) DEFAULT NULL,
99
authorizationCodeIssuedAt timestamp DEFAULT NULL,
1010
authorizationCodeExpiresAt timestamp DEFAULT NULL,
11-
authorizationCodeMetadata varchar(1000) DEFAULT NULL,
11+
authorizationCodeMetadata varchar(2000) DEFAULT NULL,
1212
accessTokenValue varchar(1000) DEFAULT NULL,
1313
accessTokenIssuedAt timestamp DEFAULT NULL,
1414
accessTokenExpiresAt timestamp DEFAULT NULL,
15-
accessTokenMetadata varchar(1000) DEFAULT NULL,
15+
accessTokenMetadata varchar(2000) DEFAULT NULL,
1616
accessTokenType varchar(100) DEFAULT NULL,
1717
accessTokenScopes varchar(1000) DEFAULT NULL,
1818
oidcIdTokenValue varchar(1000) DEFAULT NULL,
1919
oidcIdTokenIssuedAt timestamp DEFAULT NULL,
2020
oidcIdTokenExpiresAt timestamp DEFAULT NULL,
21-
oidcIdTokenMetadata varchar(1000) DEFAULT NULL,
21+
oidcIdTokenMetadata varchar(2000) DEFAULT NULL,
2222
refreshTokenValue varchar(1000) DEFAULT NULL,
2323
refreshTokenIssuedAt timestamp DEFAULT NULL,
2424
refreshTokenExpiresAt timestamp DEFAULT NULL,
25-
refreshTokenMetadata varchar(1000) DEFAULT NULL,
25+
refreshTokenMetadata varchar(2000) DEFAULT NULL,
2626
PRIMARY KEY (id)
2727
);

0 commit comments

Comments
 (0)