Polish gh-313

This commit is contained in:
Steve Riesenberg
2021-06-16 10:13:36 -05:00
parent 552751bd93
commit 7f095e0a6f

View File

@@ -15,8 +15,23 @@
*/ */
package org.springframework.security.oauth2.server.authorization; package org.springframework.security.oauth2.server.authorization;
import java.nio.charset.StandardCharsets;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter; import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.JdbcOperations;
@@ -41,20 +56,6 @@ import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.nio.charset.StandardCharsets;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
/** /**
* A JDBC implementation of an {@link OAuth2AuthorizationService} that uses a * A JDBC implementation of an {@link OAuth2AuthorizationService} that uses a
* <p> * <p>
@@ -188,7 +189,6 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
this.authorizationParametersMapper = new OAuth2AuthorizationParametersMapper(objectMapper); this.authorizationParametersMapper = new OAuth2AuthorizationParametersMapper(objectMapper);
} }
@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");
@@ -310,7 +310,6 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
private LobHandler lobHandler = new DefaultLobHandler(); private LobHandler lobHandler = new DefaultLobHandler();
public OAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository, ObjectMapper objectMapper) { public OAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository, ObjectMapper objectMapper) {
Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null"); Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null");
Assert.notNull(objectMapper, "objectMapper cannot be null"); Assert.notNull(objectMapper, "objectMapper cannot be null");
@@ -323,8 +322,7 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException { public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException {
try { try {
String registeredClientId = rs.getString("registered_client_id"); String registeredClientId = rs.getString("registered_client_id");
RegisteredClient registeredClient = this.registeredClientRepository RegisteredClient registeredClient = this.registeredClientRepository.findById(registeredClientId);
.findById(registeredClientId);
if (registeredClient == null) { if (registeredClient == null) {
throw new DataRetrievalFailureException( throw new DataRetrievalFailureException(
"The RegisteredClient with id '" + registeredClientId + "' it was not found in the RegisteredClientRepository."); "The RegisteredClient with id '" + registeredClientId + "' it was not found in the RegisteredClientRepository.");
@@ -339,7 +337,7 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
builder.id(id) builder.id(id)
.principalName(principalName) .principalName(principalName)
.authorizationGrantType(new AuthorizationGrantType(authorizationGrantType)) .authorizationGrantType(new AuthorizationGrantType(authorizationGrantType))
.attributes(attrs -> attrs.putAll(attributes)); .attributes((attrs) -> attrs.putAll(attributes));
String state = rs.getString("state"); String state = rs.getString("state");
if (StringUtils.hasText(state)) { if (StringUtils.hasText(state)) {
@@ -352,22 +350,19 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
byte[] authorizationCodeValue = this.lobHandler.getBlobAsBytes(rs, "authorization_code_value"); byte[] authorizationCodeValue = this.lobHandler.getBlobAsBytes(rs, "authorization_code_value");
if (authorizationCodeValue != null) { if (authorizationCodeValue != null) {
tokenValue = new String(authorizationCodeValue, tokenValue = new String(authorizationCodeValue, StandardCharsets.UTF_8);
StandardCharsets.UTF_8);
tokenIssuedAt = rs.getTimestamp("authorization_code_issued_at").toInstant(); tokenIssuedAt = rs.getTimestamp("authorization_code_issued_at").toInstant();
tokenExpiresAt = rs.getTimestamp("authorization_code_expires_at").toInstant(); tokenExpiresAt = rs.getTimestamp("authorization_code_expires_at").toInstant();
Map<String, Object> authorizationCodeMetadata = this.objectMapper.readValue(rs.getString("authorization_code_metadata"), Map.class); Map<String, Object> authorizationCodeMetadata = this.objectMapper.readValue(rs.getString("authorization_code_metadata"), Map.class);
OAuth2AuthorizationCode authorizationCode = new OAuth2AuthorizationCode( OAuth2AuthorizationCode authorizationCode = new OAuth2AuthorizationCode(
tokenValue, tokenIssuedAt, tokenExpiresAt); tokenValue, tokenIssuedAt, tokenExpiresAt);
builder builder.token(authorizationCode, (metadata) -> metadata.putAll(authorizationCodeMetadata));
.token(authorizationCode, (metadata) -> metadata.putAll(authorizationCodeMetadata));
} }
byte[] accessTokenValue = this.lobHandler.getBlobAsBytes(rs, "access_token_value"); byte[] accessTokenValue = this.lobHandler.getBlobAsBytes(rs, "access_token_value");
if (accessTokenValue != null) { if (accessTokenValue != null) {
tokenValue = new String(accessTokenValue, tokenValue = new String(accessTokenValue, StandardCharsets.UTF_8);
StandardCharsets.UTF_8);
tokenIssuedAt = rs.getTimestamp("access_token_issued_at").toInstant(); tokenIssuedAt = rs.getTimestamp("access_token_issued_at").toInstant();
tokenExpiresAt = rs.getTimestamp("access_token_expires_at").toInstant(); tokenExpiresAt = rs.getTimestamp("access_token_expires_at").toInstant();
Map<String, Object> accessTokenMetadata = this.objectMapper.readValue(rs.getString("access_token_metadata"), Map.class); Map<String, Object> accessTokenMetadata = this.objectMapper.readValue(rs.getString("access_token_metadata"), Map.class);
@@ -382,29 +377,24 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
scopes = StringUtils.commaDelimitedListToSet(accessTokenScopes); scopes = StringUtils.commaDelimitedListToSet(accessTokenScopes);
} }
OAuth2AccessToken accessToken = new OAuth2AccessToken(tokenType, tokenValue, tokenIssuedAt, tokenExpiresAt, scopes); OAuth2AccessToken accessToken = new OAuth2AccessToken(tokenType, tokenValue, tokenIssuedAt, tokenExpiresAt, scopes);
builder builder.token(accessToken, (metadata) -> metadata.putAll(accessTokenMetadata));
.token(accessToken, (metadata) -> metadata.putAll(accessTokenMetadata));
} }
byte[] oidcIdTokenValue = this.lobHandler.getBlobAsBytes(rs, "oidc_id_token_value"); byte[] oidcIdTokenValue = this.lobHandler.getBlobAsBytes(rs, "oidc_id_token_value");
if (oidcIdTokenValue != null) { if (oidcIdTokenValue != null) {
tokenValue = new String(oidcIdTokenValue, tokenValue = new String(oidcIdTokenValue, StandardCharsets.UTF_8);
StandardCharsets.UTF_8);
tokenIssuedAt = rs.getTimestamp("oidc_id_token_issued_at").toInstant(); tokenIssuedAt = rs.getTimestamp("oidc_id_token_issued_at").toInstant();
tokenExpiresAt = rs.getTimestamp("oidc_id_token_expires_at").toInstant(); tokenExpiresAt = rs.getTimestamp("oidc_id_token_expires_at").toInstant();
Map<String, Object> oidcTokenMetadata = this.objectMapper.readValue(rs.getString("oidc_id_token_metadata"), Map.class); Map<String, Object> oidcTokenMetadata = this.objectMapper.readValue(rs.getString("oidc_id_token_metadata"), Map.class);
OidcIdToken oidcToken = new OidcIdToken( OidcIdToken oidcToken = new OidcIdToken(
tokenValue, tokenIssuedAt, tokenExpiresAt, (Map<String, Object>) oidcTokenMetadata.get(OAuth2Authorization.Token.CLAIMS_METADATA_NAME)); tokenValue, tokenIssuedAt, tokenExpiresAt, (Map<String, Object>) oidcTokenMetadata.get(OAuth2Authorization.Token.CLAIMS_METADATA_NAME));
builder builder.token(oidcToken, (metadata) -> metadata.putAll(oidcTokenMetadata));
.token(oidcToken, (metadata) -> metadata.putAll(oidcTokenMetadata));
} }
byte[] refreshTokenValue = this.lobHandler.getBlobAsBytes(rs, "refresh_token_value"); byte[] refreshTokenValue = this.lobHandler.getBlobAsBytes(rs, "refresh_token_value");
if (refreshTokenValue != null) { if (refreshTokenValue != null) {
tokenValue = new String(refreshTokenValue, tokenValue = new String(refreshTokenValue, StandardCharsets.UTF_8);
StandardCharsets.UTF_8);
tokenIssuedAt = rs.getTimestamp("refresh_token_issued_at").toInstant(); tokenIssuedAt = rs.getTimestamp("refresh_token_issued_at").toInstant();
tokenExpiresAt = null; tokenExpiresAt = null;
Timestamp refreshTokenExpiresAt = rs.getTimestamp("refresh_token_expires_at"); Timestamp refreshTokenExpiresAt = rs.getTimestamp("refresh_token_expires_at");
@@ -415,8 +405,7 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken2( OAuth2RefreshToken refreshToken = new OAuth2RefreshToken2(
tokenValue, tokenIssuedAt, tokenExpiresAt); tokenValue, tokenIssuedAt, tokenExpiresAt);
builder builder.token(refreshToken, (metadata) -> metadata.putAll(refreshTokenMetadata));
.token(refreshToken, (metadata) -> metadata.putAll(refreshTokenMetadata));
} }
return builder.build(); return builder.build();
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
@@ -428,6 +417,7 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
Assert.notNull(lobHandler, "lobHandler cannot be null"); Assert.notNull(lobHandler, "lobHandler cannot be null");
this.lobHandler = lobHandler; this.lobHandler = lobHandler;
} }
} }
/** /**
@@ -435,6 +425,7 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
* {@code List} of {@link SqlParameterValue}. * {@code List} of {@link SqlParameterValue}.
*/ */
public static class OAuth2AuthorizationParametersMapper implements Function<OAuth2Authorization, List<SqlParameterValue>> { public static class OAuth2AuthorizationParametersMapper implements Function<OAuth2Authorization, List<SqlParameterValue>> {
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
public OAuth2AuthorizationParametersMapper(ObjectMapper objectMapper) { public OAuth2AuthorizationParametersMapper(ObjectMapper objectMapper) {
@@ -444,7 +435,6 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
@Override @Override
public List<SqlParameterValue> apply(OAuth2Authorization authorization) { public List<SqlParameterValue> apply(OAuth2Authorization authorization) {
try { try {
List<SqlParameterValue> parameters = new ArrayList<>(); List<SqlParameterValue> parameters = new ArrayList<>();
parameters.add(new SqlParameterValue(Types.VARCHAR, authorization.getId())); parameters.add(new SqlParameterValue(Types.VARCHAR, authorization.getId()));
@@ -495,7 +485,6 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new IllegalArgumentException(e.getMessage(), e); throw new IllegalArgumentException(e.getMessage(), e);
} }
} }
private <T extends AbstractOAuth2Token> List<SqlParameterValue> toSqlParameterList(OAuth2Authorization.Token<T> token) throws JsonProcessingException { private <T extends AbstractOAuth2Token> List<SqlParameterValue> toSqlParameterList(OAuth2Authorization.Token<T> token) throws JsonProcessingException {
@@ -505,7 +494,6 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
Timestamp tokenExpiresAt = null; Timestamp tokenExpiresAt = null;
String codeMetadata = null; String codeMetadata = null;
if (token != null) { if (token != null) {
tokenValue = token.getToken().getTokenValue().getBytes(StandardCharsets.UTF_8); tokenValue = token.getToken().getTokenValue().getBytes(StandardCharsets.UTF_8);
if (token.getToken().getIssuedAt() != null) { if (token.getToken().getIssuedAt() != null) {
tokenIssuedAt = Timestamp.from(token.getToken().getIssuedAt()); tokenIssuedAt = Timestamp.from(token.getToken().getIssuedAt());
@@ -522,6 +510,7 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
parameters.add(new SqlParameterValue(Types.VARCHAR, codeMetadata)); parameters.add(new SqlParameterValue(Types.VARCHAR, codeMetadata));
return parameters; return parameters;
} }
} }
private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter { private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter {
@@ -551,4 +540,5 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization
} }
} }
} }