Skip to content

Commit 99d86d9

Browse files
author
Steve Riesenberg
committed
1 parent 85e2467 commit 99d86d9

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

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

+29-39
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,23 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization;
1717

18+
import java.nio.charset.StandardCharsets;
19+
import java.sql.PreparedStatement;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
import java.sql.Timestamp;
23+
import java.sql.Types;
24+
import java.time.Instant;
25+
import java.util.ArrayList;
26+
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.Set;
30+
import java.util.function.Function;
31+
1832
import com.fasterxml.jackson.core.JsonProcessingException;
1933
import com.fasterxml.jackson.databind.ObjectMapper;
34+
2035
import org.springframework.dao.DataRetrievalFailureException;
2136
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
2237
import org.springframework.jdbc.core.JdbcOperations;
@@ -41,20 +56,6 @@
4156
import org.springframework.util.CollectionUtils;
4257
import org.springframework.util.StringUtils;
4358

44-
import java.nio.charset.StandardCharsets;
45-
import java.sql.PreparedStatement;
46-
import java.sql.ResultSet;
47-
import java.sql.SQLException;
48-
import java.sql.Timestamp;
49-
import java.sql.Types;
50-
import java.time.Instant;
51-
import java.util.ArrayList;
52-
import java.util.Collections;
53-
import java.util.List;
54-
import java.util.Map;
55-
import java.util.Set;
56-
import java.util.function.Function;
57-
5859
/**
5960
* A JDBC implementation of an {@link OAuth2AuthorizationService} that uses a
6061
* <p>
@@ -188,7 +189,6 @@ public JdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
188189
this.authorizationParametersMapper = new OAuth2AuthorizationParametersMapper(objectMapper);
189190
}
190191

191-
192192
@Override
193193
public void save(OAuth2Authorization authorization) {
194194
Assert.notNull(authorization, "authorization cannot be null");
@@ -310,7 +310,6 @@ public static class OAuth2AuthorizationRowMapper implements RowMapper<OAuth2Auth
310310
private final ObjectMapper objectMapper;
311311
private LobHandler lobHandler = new DefaultLobHandler();
312312

313-
314313
public OAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository, ObjectMapper objectMapper) {
315314
Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null");
316315
Assert.notNull(objectMapper, "objectMapper cannot be null");
@@ -323,8 +322,7 @@ public OAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientR
323322
public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException {
324323
try {
325324
String registeredClientId = rs.getString("registered_client_id");
326-
RegisteredClient registeredClient = this.registeredClientRepository
327-
.findById(registeredClientId);
325+
RegisteredClient registeredClient = this.registeredClientRepository.findById(registeredClientId);
328326
if (registeredClient == null) {
329327
throw new DataRetrievalFailureException(
330328
"The RegisteredClient with id '" + registeredClientId + "' it was not found in the RegisteredClientRepository.");
@@ -339,7 +337,7 @@ public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException
339337
builder.id(id)
340338
.principalName(principalName)
341339
.authorizationGrantType(new AuthorizationGrantType(authorizationGrantType))
342-
.attributes(attrs -> attrs.putAll(attributes));
340+
.attributes((attrs) -> attrs.putAll(attributes));
343341

344342
String state = rs.getString("state");
345343
if (StringUtils.hasText(state)) {
@@ -352,22 +350,19 @@ public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException
352350
byte[] authorizationCodeValue = this.lobHandler.getBlobAsBytes(rs, "authorization_code_value");
353351

354352
if (authorizationCodeValue != null) {
355-
tokenValue = new String(authorizationCodeValue,
356-
StandardCharsets.UTF_8);
353+
tokenValue = new String(authorizationCodeValue, StandardCharsets.UTF_8);
357354
tokenIssuedAt = rs.getTimestamp("authorization_code_issued_at").toInstant();
358355
tokenExpiresAt = rs.getTimestamp("authorization_code_expires_at").toInstant();
359356
Map<String, Object> authorizationCodeMetadata = this.objectMapper.readValue(rs.getString("authorization_code_metadata"), Map.class);
360357

361358
OAuth2AuthorizationCode authorizationCode = new OAuth2AuthorizationCode(
362359
tokenValue, tokenIssuedAt, tokenExpiresAt);
363-
builder
364-
.token(authorizationCode, (metadata) -> metadata.putAll(authorizationCodeMetadata));
360+
builder.token(authorizationCode, (metadata) -> metadata.putAll(authorizationCodeMetadata));
365361
}
366362

367363
byte[] accessTokenValue = this.lobHandler.getBlobAsBytes(rs, "access_token_value");
368364
if (accessTokenValue != null) {
369-
tokenValue = new String(accessTokenValue,
370-
StandardCharsets.UTF_8);
365+
tokenValue = new String(accessTokenValue, StandardCharsets.UTF_8);
371366
tokenIssuedAt = rs.getTimestamp("access_token_issued_at").toInstant();
372367
tokenExpiresAt = rs.getTimestamp("access_token_expires_at").toInstant();
373368
Map<String, Object> accessTokenMetadata = this.objectMapper.readValue(rs.getString("access_token_metadata"), Map.class);
@@ -382,29 +377,24 @@ public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException
382377
scopes = StringUtils.commaDelimitedListToSet(accessTokenScopes);
383378
}
384379
OAuth2AccessToken accessToken = new OAuth2AccessToken(tokenType, tokenValue, tokenIssuedAt, tokenExpiresAt, scopes);
385-
builder
386-
.token(accessToken, (metadata) -> metadata.putAll(accessTokenMetadata));
380+
builder.token(accessToken, (metadata) -> metadata.putAll(accessTokenMetadata));
387381
}
388382

389383
byte[] oidcIdTokenValue = this.lobHandler.getBlobAsBytes(rs, "oidc_id_token_value");
390-
391384
if (oidcIdTokenValue != null) {
392-
tokenValue = new String(oidcIdTokenValue,
393-
StandardCharsets.UTF_8);
385+
tokenValue = new String(oidcIdTokenValue, StandardCharsets.UTF_8);
394386
tokenIssuedAt = rs.getTimestamp("oidc_id_token_issued_at").toInstant();
395387
tokenExpiresAt = rs.getTimestamp("oidc_id_token_expires_at").toInstant();
396388
Map<String, Object> oidcTokenMetadata = this.objectMapper.readValue(rs.getString("oidc_id_token_metadata"), Map.class);
397389

398390
OidcIdToken oidcToken = new OidcIdToken(
399391
tokenValue, tokenIssuedAt, tokenExpiresAt, (Map<String, Object>) oidcTokenMetadata.get(OAuth2Authorization.Token.CLAIMS_METADATA_NAME));
400-
builder
401-
.token(oidcToken, (metadata) -> metadata.putAll(oidcTokenMetadata));
392+
builder.token(oidcToken, (metadata) -> metadata.putAll(oidcTokenMetadata));
402393
}
403394

404395
byte[] refreshTokenValue = this.lobHandler.getBlobAsBytes(rs, "refresh_token_value");
405396
if (refreshTokenValue != null) {
406-
tokenValue = new String(refreshTokenValue,
407-
StandardCharsets.UTF_8);
397+
tokenValue = new String(refreshTokenValue, StandardCharsets.UTF_8);
408398
tokenIssuedAt = rs.getTimestamp("refresh_token_issued_at").toInstant();
409399
tokenExpiresAt = null;
410400
Timestamp refreshTokenExpiresAt = rs.getTimestamp("refresh_token_expires_at");
@@ -415,8 +405,7 @@ public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException
415405

416406
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken2(
417407
tokenValue, tokenIssuedAt, tokenExpiresAt);
418-
builder
419-
.token(refreshToken, (metadata) -> metadata.putAll(refreshTokenMetadata));
408+
builder.token(refreshToken, (metadata) -> metadata.putAll(refreshTokenMetadata));
420409
}
421410
return builder.build();
422411
} catch (JsonProcessingException e) {
@@ -428,13 +417,15 @@ public final void setLobHandler(LobHandler lobHandler) {
428417
Assert.notNull(lobHandler, "lobHandler cannot be null");
429418
this.lobHandler = lobHandler;
430419
}
420+
431421
}
432422

433423
/**
434424
* The default {@code Function} that maps {@link OAuth2Authorization} to a
435425
* {@code List} of {@link SqlParameterValue}.
436426
*/
437427
public static class OAuth2AuthorizationParametersMapper implements Function<OAuth2Authorization, List<SqlParameterValue>> {
428+
438429
private final ObjectMapper objectMapper;
439430

440431
public OAuth2AuthorizationParametersMapper(ObjectMapper objectMapper) {
@@ -444,7 +435,6 @@ public OAuth2AuthorizationParametersMapper(ObjectMapper objectMapper) {
444435

445436
@Override
446437
public List<SqlParameterValue> apply(OAuth2Authorization authorization) {
447-
448438
try {
449439
List<SqlParameterValue> parameters = new ArrayList<>();
450440
parameters.add(new SqlParameterValue(Types.VARCHAR, authorization.getId()));
@@ -495,7 +485,6 @@ public List<SqlParameterValue> apply(OAuth2Authorization authorization) {
495485
} catch (JsonProcessingException e) {
496486
throw new IllegalArgumentException(e.getMessage(), e);
497487
}
498-
499488
}
500489

501490
private <T extends AbstractOAuth2Token> List<SqlParameterValue> toSqlParameterList(OAuth2Authorization.Token<T> token) throws JsonProcessingException {
@@ -505,7 +494,6 @@ private <T extends AbstractOAuth2Token> List<SqlParameterValue> toSqlParameterLi
505494
Timestamp tokenExpiresAt = null;
506495
String codeMetadata = null;
507496
if (token != null) {
508-
509497
tokenValue = token.getToken().getTokenValue().getBytes(StandardCharsets.UTF_8);
510498
if (token.getToken().getIssuedAt() != null) {
511499
tokenIssuedAt = Timestamp.from(token.getToken().getIssuedAt());
@@ -522,6 +510,7 @@ private <T extends AbstractOAuth2Token> List<SqlParameterValue> toSqlParameterLi
522510
parameters.add(new SqlParameterValue(Types.VARCHAR, codeMetadata));
523511
return parameters;
524512
}
513+
525514
}
526515

527516
private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter {
@@ -551,4 +540,5 @@ protected void doSetValue(PreparedStatement ps, int parameterPosition, Object ar
551540
}
552541

553542
}
543+
554544
}

0 commit comments

Comments
 (0)