diff --git a/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationService.java b/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationService.java index ced7b887..2a474516 100644 --- a/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationService.java +++ b/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationService.java @@ -15,8 +15,23 @@ */ 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.databind.ObjectMapper; + import org.springframework.dao.DataRetrievalFailureException; import org.springframework.jdbc.core.ArgumentPreparedStatementSetter; import org.springframework.jdbc.core.JdbcOperations; @@ -41,20 +56,6 @@ import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; 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 *

@@ -188,7 +189,6 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization this.authorizationParametersMapper = new OAuth2AuthorizationParametersMapper(objectMapper); } - @Override public void save(OAuth2Authorization authorization) { Assert.notNull(authorization, "authorization cannot be null"); @@ -310,7 +310,6 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization private final ObjectMapper objectMapper; private LobHandler lobHandler = new DefaultLobHandler(); - public OAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository, ObjectMapper objectMapper) { Assert.notNull(registeredClientRepository, "registeredClientRepository 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 { try { String registeredClientId = rs.getString("registered_client_id"); - RegisteredClient registeredClient = this.registeredClientRepository - .findById(registeredClientId); + RegisteredClient registeredClient = this.registeredClientRepository.findById(registeredClientId); if (registeredClient == null) { throw new DataRetrievalFailureException( "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) .principalName(principalName) .authorizationGrantType(new AuthorizationGrantType(authorizationGrantType)) - .attributes(attrs -> attrs.putAll(attributes)); + .attributes((attrs) -> attrs.putAll(attributes)); String state = rs.getString("state"); if (StringUtils.hasText(state)) { @@ -352,22 +350,19 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization byte[] authorizationCodeValue = this.lobHandler.getBlobAsBytes(rs, "authorization_code_value"); if (authorizationCodeValue != null) { - tokenValue = new String(authorizationCodeValue, - StandardCharsets.UTF_8); + tokenValue = new String(authorizationCodeValue, StandardCharsets.UTF_8); tokenIssuedAt = rs.getTimestamp("authorization_code_issued_at").toInstant(); tokenExpiresAt = rs.getTimestamp("authorization_code_expires_at").toInstant(); Map authorizationCodeMetadata = this.objectMapper.readValue(rs.getString("authorization_code_metadata"), Map.class); OAuth2AuthorizationCode authorizationCode = new OAuth2AuthorizationCode( tokenValue, tokenIssuedAt, tokenExpiresAt); - builder - .token(authorizationCode, (metadata) -> metadata.putAll(authorizationCodeMetadata)); + builder.token(authorizationCode, (metadata) -> metadata.putAll(authorizationCodeMetadata)); } byte[] accessTokenValue = this.lobHandler.getBlobAsBytes(rs, "access_token_value"); if (accessTokenValue != null) { - tokenValue = new String(accessTokenValue, - StandardCharsets.UTF_8); + tokenValue = new String(accessTokenValue, StandardCharsets.UTF_8); tokenIssuedAt = rs.getTimestamp("access_token_issued_at").toInstant(); tokenExpiresAt = rs.getTimestamp("access_token_expires_at").toInstant(); Map 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); } OAuth2AccessToken accessToken = new OAuth2AccessToken(tokenType, tokenValue, tokenIssuedAt, tokenExpiresAt, scopes); - builder - .token(accessToken, (metadata) -> metadata.putAll(accessTokenMetadata)); + builder.token(accessToken, (metadata) -> metadata.putAll(accessTokenMetadata)); } byte[] oidcIdTokenValue = this.lobHandler.getBlobAsBytes(rs, "oidc_id_token_value"); - if (oidcIdTokenValue != null) { - tokenValue = new String(oidcIdTokenValue, - StandardCharsets.UTF_8); + tokenValue = new String(oidcIdTokenValue, StandardCharsets.UTF_8); tokenIssuedAt = rs.getTimestamp("oidc_id_token_issued_at").toInstant(); tokenExpiresAt = rs.getTimestamp("oidc_id_token_expires_at").toInstant(); Map oidcTokenMetadata = this.objectMapper.readValue(rs.getString("oidc_id_token_metadata"), Map.class); OidcIdToken oidcToken = new OidcIdToken( tokenValue, tokenIssuedAt, tokenExpiresAt, (Map) oidcTokenMetadata.get(OAuth2Authorization.Token.CLAIMS_METADATA_NAME)); - builder - .token(oidcToken, (metadata) -> metadata.putAll(oidcTokenMetadata)); + builder.token(oidcToken, (metadata) -> metadata.putAll(oidcTokenMetadata)); } byte[] refreshTokenValue = this.lobHandler.getBlobAsBytes(rs, "refresh_token_value"); if (refreshTokenValue != null) { - tokenValue = new String(refreshTokenValue, - StandardCharsets.UTF_8); + tokenValue = new String(refreshTokenValue, StandardCharsets.UTF_8); tokenIssuedAt = rs.getTimestamp("refresh_token_issued_at").toInstant(); tokenExpiresAt = null; Timestamp refreshTokenExpiresAt = rs.getTimestamp("refresh_token_expires_at"); @@ -415,8 +405,7 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization OAuth2RefreshToken refreshToken = new OAuth2RefreshToken2( tokenValue, tokenIssuedAt, tokenExpiresAt); - builder - .token(refreshToken, (metadata) -> metadata.putAll(refreshTokenMetadata)); + builder.token(refreshToken, (metadata) -> metadata.putAll(refreshTokenMetadata)); } return builder.build(); } catch (JsonProcessingException e) { @@ -428,6 +417,7 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization Assert.notNull(lobHandler, "lobHandler cannot be null"); this.lobHandler = lobHandler; } + } /** @@ -435,6 +425,7 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization * {@code List} of {@link SqlParameterValue}. */ public static class OAuth2AuthorizationParametersMapper implements Function> { + private final ObjectMapper objectMapper; public OAuth2AuthorizationParametersMapper(ObjectMapper objectMapper) { @@ -444,7 +435,6 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization @Override public List apply(OAuth2Authorization authorization) { - try { List parameters = new ArrayList<>(); parameters.add(new SqlParameterValue(Types.VARCHAR, authorization.getId())); @@ -495,7 +485,6 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization } catch (JsonProcessingException e) { throw new IllegalArgumentException(e.getMessage(), e); } - } private List toSqlParameterList(OAuth2Authorization.Token token) throws JsonProcessingException { @@ -505,7 +494,6 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization Timestamp tokenExpiresAt = null; String codeMetadata = null; if (token != null) { - tokenValue = token.getToken().getTokenValue().getBytes(StandardCharsets.UTF_8); if (token.getToken().getIssuedAt() != null) { tokenIssuedAt = Timestamp.from(token.getToken().getIssuedAt()); @@ -522,6 +510,7 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization parameters.add(new SqlParameterValue(Types.VARCHAR, codeMetadata)); return parameters; } + } private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter { @@ -551,4 +540,5 @@ public final class JdbcOAuth2AuthorizationService implements OAuth2Authorization } } + }