Переглянути джерело

Polish JdbcOAuth2AuthorizationService

Issue gh-304
Joe Grandja 4 роки тому
батько
коміт
9787794ea1

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

@@ -61,7 +61,6 @@ import org.springframework.util.StringUtils;
 
 
 /**
 /**
  * A JDBC implementation of an {@link OAuth2AuthorizationService} that uses a
  * A JDBC implementation of an {@link OAuth2AuthorizationService} that uses a
- * <p>
  * {@link JdbcOperations} for {@link OAuth2Authorization} persistence.
  * {@link JdbcOperations} for {@link OAuth2Authorization} persistence.
  *
  *
  * <p>
  * <p>
@@ -71,11 +70,11 @@ import org.springframework.util.StringUtils;
  * therefore MUST be defined in the database schema.
  * therefore MUST be defined in the database schema.
  *
  *
  * @author Ovidiu Popa
  * @author Ovidiu Popa
+ * @since 0.1.2
  * @see OAuth2AuthorizationService
  * @see OAuth2AuthorizationService
  * @see OAuth2Authorization
  * @see OAuth2Authorization
  * @see JdbcOperations
  * @see JdbcOperations
  * @see RowMapper
  * @see RowMapper
- * @since 0.1.2
  */
  */
 public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationService {
 public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationService {
 
 
@@ -110,8 +109,7 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
 
 
 	private static final String PK_FILTER = "id = ?";
 	private static final String PK_FILTER = "id = ?";
 	private static final String UNKNOWN_TOKEN_TYPE_FILTER = "state = ? OR authorization_code_value = ? OR " +
 	private static final String UNKNOWN_TOKEN_TYPE_FILTER = "state = ? OR authorization_code_value = ? OR " +
-			"access_token_value = ? OR " +
-			"refresh_token_value = ?";
+			"access_token_value = ? OR refresh_token_value = ?";
 
 
 	private static final String STATE_FILTER = "state = ?";
 	private static final String STATE_FILTER = "state = ?";
 	private static final String AUTHORIZATION_CODE_FILTER = "authorization_code_value = ?";
 	private static final String AUTHORIZATION_CODE_FILTER = "authorization_code_value = ?";
@@ -126,7 +124,7 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
 
 
 	// @formatter:off
 	// @formatter:off
 	private static final String SAVE_AUTHORIZATION_SQL = "INSERT INTO " + TABLE_NAME
 	private static final String SAVE_AUTHORIZATION_SQL = "INSERT INTO " + TABLE_NAME
-			+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?)";
+			+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
 	// @formatter:on
 	// @formatter:on
 
 
 	// @formatter:off
 	// @formatter:off
@@ -180,7 +178,6 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
 	@Override
 	@Override
 	public void save(OAuth2Authorization authorization) {
 	public void save(OAuth2Authorization authorization) {
 		Assert.notNull(authorization, "authorization cannot be null");
 		Assert.notNull(authorization, "authorization cannot be null");
-
 		OAuth2Authorization existingAuthorization = findById(authorization.getId());
 		OAuth2Authorization existingAuthorization = findById(authorization.getId());
 		if (existingAuthorization == null) {
 		if (existingAuthorization == null) {
 			insertAuthorization(authorization);
 			insertAuthorization(authorization);
@@ -529,7 +526,6 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
 				if (token.getToken().getIssuedAt() != null) {
 				if (token.getToken().getIssuedAt() != null) {
 					tokenIssuedAt = Timestamp.from(token.getToken().getIssuedAt());
 					tokenIssuedAt = Timestamp.from(token.getToken().getIssuedAt());
 				}
 				}
-
 				if (token.getToken().getExpiresAt() != null) {
 				if (token.getToken().getExpiresAt() != null) {
 					tokenExpiresAt = Timestamp.from(token.getToken().getExpiresAt());
 					tokenExpiresAt = Timestamp.from(token.getToken().getExpiresAt());
 				}
 				}
@@ -553,7 +549,6 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
 	}
 	}
 
 
 	private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter {
 	private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter {
-
 		private final LobCreator lobCreator;
 		private final LobCreator lobCreator;
 
 
 		private LobCreatorArgumentPreparedStatementSetter(LobCreator lobCreator, Object[] args) {
 		private LobCreatorArgumentPreparedStatementSetter(LobCreator lobCreator, Object[] args) {

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

@@ -4,24 +4,24 @@ CREATE TABLE oauth2_authorization (
     principal_name varchar(200) NOT NULL,
     principal_name varchar(200) NOT NULL,
     authorization_grant_type varchar(100) NOT NULL,
     authorization_grant_type varchar(100) NOT NULL,
     attributes varchar(4000) DEFAULT NULL,
     attributes varchar(4000) DEFAULT NULL,
-    state varchar(1000) DEFAULT NULL,
+    state varchar(500) DEFAULT NULL,
     authorization_code_value blob DEFAULT NULL,
     authorization_code_value blob DEFAULT NULL,
     authorization_code_issued_at timestamp DEFAULT NULL,
     authorization_code_issued_at timestamp DEFAULT NULL,
     authorization_code_expires_at timestamp DEFAULT NULL,
     authorization_code_expires_at timestamp DEFAULT NULL,
-    authorization_code_metadata varchar(1000) DEFAULT NULL,
+    authorization_code_metadata varchar(2000) DEFAULT NULL,
     access_token_value blob DEFAULT NULL,
     access_token_value blob DEFAULT NULL,
     access_token_issued_at timestamp DEFAULT NULL,
     access_token_issued_at timestamp DEFAULT NULL,
     access_token_expires_at timestamp DEFAULT NULL,
     access_token_expires_at timestamp DEFAULT NULL,
-    access_token_metadata varchar(1000) DEFAULT NULL,
+    access_token_metadata varchar(2000) DEFAULT NULL,
     access_token_type varchar(100) DEFAULT NULL,
     access_token_type varchar(100) DEFAULT NULL,
     access_token_scopes varchar(1000) DEFAULT NULL,
     access_token_scopes varchar(1000) DEFAULT NULL,
     oidc_id_token_value blob DEFAULT NULL,
     oidc_id_token_value blob DEFAULT NULL,
     oidc_id_token_issued_at timestamp DEFAULT NULL,
     oidc_id_token_issued_at timestamp DEFAULT NULL,
     oidc_id_token_expires_at timestamp DEFAULT NULL,
     oidc_id_token_expires_at timestamp DEFAULT NULL,
-    oidc_id_token_metadata varchar(1000) DEFAULT NULL,
+    oidc_id_token_metadata varchar(2000) DEFAULT NULL,
     refresh_token_value blob DEFAULT NULL,
     refresh_token_value blob DEFAULT NULL,
     refresh_token_issued_at timestamp DEFAULT NULL,
     refresh_token_issued_at timestamp DEFAULT NULL,
     refresh_token_expires_at timestamp DEFAULT NULL,
     refresh_token_expires_at timestamp DEFAULT NULL,
-    refresh_token_metadata varchar(1000) DEFAULT NULL,
+    refresh_token_metadata varchar(2000) DEFAULT NULL,
     PRIMARY KEY (id)
     PRIMARY KEY (id)
 );
 );

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

@@ -29,7 +29,6 @@ import java.util.Set;
 import java.util.function.Function;
 import java.util.function.Function;
 
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import org.junit.After;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.Test;
@@ -210,7 +209,7 @@ public class JdbcOAuth2AuthorizationServiceTests {
 				.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
 				.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
 				.token(AUTHORIZATION_CODE)
 				.token(AUTHORIZATION_CODE)
 				.build();
 				.build();
-		ObjectMapper objectMapper = new ObjectMapper();
+
 		RowMapper<OAuth2Authorization> authorizationRowMapper = spy(
 		RowMapper<OAuth2Authorization> authorizationRowMapper = spy(
 				new JdbcOAuth2AuthorizationService.OAuth2AuthorizationRowMapper(
 				new JdbcOAuth2AuthorizationService.OAuth2AuthorizationRowMapper(
 						this.registeredClientRepository));
 						this.registeredClientRepository));
@@ -252,7 +251,7 @@ public class JdbcOAuth2AuthorizationServiceTests {
 				AUTHORIZATION_CODE.getTokenValue(), AUTHORIZATION_CODE_TOKEN_TYPE);
 				AUTHORIZATION_CODE.getTokenValue(), AUTHORIZATION_CODE_TOKEN_TYPE);
 		assertThat(authorization).isEqualTo(expectedAuthorization);
 		assertThat(authorization).isEqualTo(expectedAuthorization);
 
 
-		this.authorizationService.remove(expectedAuthorization);
+		this.authorizationService.remove(authorization);
 		authorization = this.authorizationService.findByToken(
 		authorization = this.authorizationService.findByToken(
 				AUTHORIZATION_CODE.getTokenValue(), AUTHORIZATION_CODE_TOKEN_TYPE);
 				AUTHORIZATION_CODE.getTokenValue(), AUTHORIZATION_CODE_TOKEN_TYPE);
 		assertThat(authorization).isNull();
 		assertThat(authorization).isNull();
@@ -463,8 +462,7 @@ public class JdbcOAuth2AuthorizationServiceTests {
 
 
 		private static final String PK_FILTER = "id = ?";
 		private static final String PK_FILTER = "id = ?";
 		private static final String UNKNOWN_TOKEN_TYPE_FILTER = "state = ? OR authorizationCodeValue = ? OR " +
 		private static final String UNKNOWN_TOKEN_TYPE_FILTER = "state = ? OR authorizationCodeValue = ? OR " +
-				"accessTokenValue = ? OR " +
-				"refreshTokenValue = ?";
+				"accessTokenValue = ? OR refreshTokenValue = ?";
 
 
 		// @formatter:off
 		// @formatter:off
 		private static final String LOAD_AUTHORIZATION_SQL = "SELECT " + COLUMN_NAMES
 		private static final String LOAD_AUTHORIZATION_SQL = "SELECT " + COLUMN_NAMES
@@ -474,12 +472,12 @@ public class JdbcOAuth2AuthorizationServiceTests {
 
 
 		// @formatter:off
 		// @formatter:off
 		private static final String SAVE_AUTHORIZATION_SQL = "INSERT INTO " + TABLE_NAME
 		private static final String SAVE_AUTHORIZATION_SQL = "INSERT INTO " + TABLE_NAME
-				+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?)";
+				+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
 		// @formatter:on
 		// @formatter:on
 
 
 		private static final String REMOVE_AUTHORIZATION_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + PK_FILTER;
 		private static final String REMOVE_AUTHORIZATION_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + PK_FILTER;
 
 
-		CustomJdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
+		private CustomJdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
 				RegisteredClientRepository registeredClientRepository) {
 				RegisteredClientRepository registeredClientRepository) {
 			super(jdbcOperations, registeredClientRepository);
 			super(jdbcOperations, registeredClientRepository);
 			setAuthorizationRowMapper(new CustomOAuth2AuthorizationRowMapper(registeredClientRepository));
 			setAuthorizationRowMapper(new CustomOAuth2AuthorizationRowMapper(registeredClientRepository));
@@ -520,7 +518,7 @@ public class JdbcOAuth2AuthorizationServiceTests {
 
 
 		private static final class CustomOAuth2AuthorizationRowMapper extends JdbcOAuth2AuthorizationService.OAuth2AuthorizationRowMapper {
 		private static final class CustomOAuth2AuthorizationRowMapper extends JdbcOAuth2AuthorizationService.OAuth2AuthorizationRowMapper {
 
 
-			CustomOAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository) {
+			private CustomOAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository) {
 				super(registeredClientRepository);
 				super(registeredClientRepository);
 			}
 			}
 
 
@@ -682,7 +680,6 @@ public class JdbcOAuth2AuthorizationServiceTests {
 					if (token.getToken().getIssuedAt() != null) {
 					if (token.getToken().getIssuedAt() != null) {
 						tokenIssuedAt = Timestamp.from(token.getToken().getIssuedAt());
 						tokenIssuedAt = Timestamp.from(token.getToken().getIssuedAt());
 					}
 					}
-
 					if (token.getToken().getExpiresAt() != null) {
 					if (token.getToken().getExpiresAt() != null) {
 						tokenExpiresAt = Timestamp.from(token.getToken().getExpiresAt());
 						tokenExpiresAt = Timestamp.from(token.getToken().getExpiresAt());
 					}
 					}

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

@@ -4,24 +4,24 @@ CREATE TABLE oauth2Authorization (
     principalName varchar(200) NOT NULL,
     principalName varchar(200) NOT NULL,
     authorizationGrantType varchar(100) NOT NULL,
     authorizationGrantType varchar(100) NOT NULL,
     attributes varchar(4000) DEFAULT NULL,
     attributes varchar(4000) DEFAULT NULL,
-    state varchar(1000) DEFAULT NULL,
+    state varchar(500) DEFAULT NULL,
     authorizationCodeValue varchar(1000) DEFAULT NULL,
     authorizationCodeValue varchar(1000) DEFAULT NULL,
     authorizationCodeIssuedAt timestamp DEFAULT NULL,
     authorizationCodeIssuedAt timestamp DEFAULT NULL,
     authorizationCodeExpiresAt timestamp DEFAULT NULL,
     authorizationCodeExpiresAt timestamp DEFAULT NULL,
-    authorizationCodeMetadata varchar(1000) DEFAULT NULL,
+    authorizationCodeMetadata varchar(2000) DEFAULT NULL,
     accessTokenValue varchar(1000) DEFAULT NULL,
     accessTokenValue varchar(1000) DEFAULT NULL,
     accessTokenIssuedAt timestamp DEFAULT NULL,
     accessTokenIssuedAt timestamp DEFAULT NULL,
     accessTokenExpiresAt timestamp DEFAULT NULL,
     accessTokenExpiresAt timestamp DEFAULT NULL,
-    accessTokenMetadata varchar(1000) DEFAULT NULL,
+    accessTokenMetadata varchar(2000) DEFAULT NULL,
     accessTokenType varchar(100) DEFAULT NULL,
     accessTokenType varchar(100) DEFAULT NULL,
     accessTokenScopes varchar(1000) DEFAULT NULL,
     accessTokenScopes varchar(1000) DEFAULT NULL,
     oidcIdTokenValue varchar(1000) DEFAULT NULL,
     oidcIdTokenValue varchar(1000) DEFAULT NULL,
     oidcIdTokenIssuedAt timestamp DEFAULT NULL,
     oidcIdTokenIssuedAt timestamp DEFAULT NULL,
     oidcIdTokenExpiresAt timestamp DEFAULT NULL,
     oidcIdTokenExpiresAt timestamp DEFAULT NULL,
-    oidcIdTokenMetadata varchar(1000) DEFAULT NULL,
+    oidcIdTokenMetadata varchar(2000) DEFAULT NULL,
     refreshTokenValue varchar(1000) DEFAULT NULL,
     refreshTokenValue varchar(1000) DEFAULT NULL,
     refreshTokenIssuedAt timestamp DEFAULT NULL,
     refreshTokenIssuedAt timestamp DEFAULT NULL,
     refreshTokenExpiresAt timestamp DEFAULT NULL,
     refreshTokenExpiresAt timestamp DEFAULT NULL,
-    refreshTokenMetadata varchar(1000) DEFAULT NULL,
+    refreshTokenMetadata varchar(2000) DEFAULT NULL,
     PRIMARY KEY (id)
     PRIMARY KEY (id)
 );
 );