15
15
*/
16
16
package org .springframework .security .oauth2 .server .authorization ;
17
17
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
+
18
32
import com .fasterxml .jackson .core .JsonProcessingException ;
19
33
import com .fasterxml .jackson .databind .ObjectMapper ;
34
+
20
35
import org .springframework .dao .DataRetrievalFailureException ;
21
36
import org .springframework .jdbc .core .ArgumentPreparedStatementSetter ;
22
37
import org .springframework .jdbc .core .JdbcOperations ;
41
56
import org .springframework .util .CollectionUtils ;
42
57
import org .springframework .util .StringUtils ;
43
58
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
-
58
59
/**
59
60
* A JDBC implementation of an {@link OAuth2AuthorizationService} that uses a
60
61
* <p>
@@ -188,7 +189,6 @@ public JdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
188
189
this .authorizationParametersMapper = new OAuth2AuthorizationParametersMapper (objectMapper );
189
190
}
190
191
191
-
192
192
@ Override
193
193
public void save (OAuth2Authorization authorization ) {
194
194
Assert .notNull (authorization , "authorization cannot be null" );
@@ -310,7 +310,6 @@ public static class OAuth2AuthorizationRowMapper implements RowMapper<OAuth2Auth
310
310
private final ObjectMapper objectMapper ;
311
311
private LobHandler lobHandler = new DefaultLobHandler ();
312
312
313
-
314
313
public OAuth2AuthorizationRowMapper (RegisteredClientRepository registeredClientRepository , ObjectMapper objectMapper ) {
315
314
Assert .notNull (registeredClientRepository , "registeredClientRepository cannot be null" );
316
315
Assert .notNull (objectMapper , "objectMapper cannot be null" );
@@ -323,8 +322,7 @@ public OAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientR
323
322
public OAuth2Authorization mapRow (ResultSet rs , int rowNum ) throws SQLException {
324
323
try {
325
324
String registeredClientId = rs .getString ("registered_client_id" );
326
- RegisteredClient registeredClient = this .registeredClientRepository
327
- .findById (registeredClientId );
325
+ RegisteredClient registeredClient = this .registeredClientRepository .findById (registeredClientId );
328
326
if (registeredClient == null ) {
329
327
throw new DataRetrievalFailureException (
330
328
"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
339
337
builder .id (id )
340
338
.principalName (principalName )
341
339
.authorizationGrantType (new AuthorizationGrantType (authorizationGrantType ))
342
- .attributes (attrs -> attrs .putAll (attributes ));
340
+ .attributes (( attrs ) -> attrs .putAll (attributes ));
343
341
344
342
String state = rs .getString ("state" );
345
343
if (StringUtils .hasText (state )) {
@@ -352,22 +350,19 @@ public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException
352
350
byte [] authorizationCodeValue = this .lobHandler .getBlobAsBytes (rs , "authorization_code_value" );
353
351
354
352
if (authorizationCodeValue != null ) {
355
- tokenValue = new String (authorizationCodeValue ,
356
- StandardCharsets .UTF_8 );
353
+ tokenValue = new String (authorizationCodeValue , StandardCharsets .UTF_8 );
357
354
tokenIssuedAt = rs .getTimestamp ("authorization_code_issued_at" ).toInstant ();
358
355
tokenExpiresAt = rs .getTimestamp ("authorization_code_expires_at" ).toInstant ();
359
356
Map <String , Object > authorizationCodeMetadata = this .objectMapper .readValue (rs .getString ("authorization_code_metadata" ), Map .class );
360
357
361
358
OAuth2AuthorizationCode authorizationCode = new OAuth2AuthorizationCode (
362
359
tokenValue , tokenIssuedAt , tokenExpiresAt );
363
- builder
364
- .token (authorizationCode , (metadata ) -> metadata .putAll (authorizationCodeMetadata ));
360
+ builder .token (authorizationCode , (metadata ) -> metadata .putAll (authorizationCodeMetadata ));
365
361
}
366
362
367
363
byte [] accessTokenValue = this .lobHandler .getBlobAsBytes (rs , "access_token_value" );
368
364
if (accessTokenValue != null ) {
369
- tokenValue = new String (accessTokenValue ,
370
- StandardCharsets .UTF_8 );
365
+ tokenValue = new String (accessTokenValue , StandardCharsets .UTF_8 );
371
366
tokenIssuedAt = rs .getTimestamp ("access_token_issued_at" ).toInstant ();
372
367
tokenExpiresAt = rs .getTimestamp ("access_token_expires_at" ).toInstant ();
373
368
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
382
377
scopes = StringUtils .commaDelimitedListToSet (accessTokenScopes );
383
378
}
384
379
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 ));
387
381
}
388
382
389
383
byte [] oidcIdTokenValue = this .lobHandler .getBlobAsBytes (rs , "oidc_id_token_value" );
390
-
391
384
if (oidcIdTokenValue != null ) {
392
- tokenValue = new String (oidcIdTokenValue ,
393
- StandardCharsets .UTF_8 );
385
+ tokenValue = new String (oidcIdTokenValue , StandardCharsets .UTF_8 );
394
386
tokenIssuedAt = rs .getTimestamp ("oidc_id_token_issued_at" ).toInstant ();
395
387
tokenExpiresAt = rs .getTimestamp ("oidc_id_token_expires_at" ).toInstant ();
396
388
Map <String , Object > oidcTokenMetadata = this .objectMapper .readValue (rs .getString ("oidc_id_token_metadata" ), Map .class );
397
389
398
390
OidcIdToken oidcToken = new OidcIdToken (
399
391
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 ));
402
393
}
403
394
404
395
byte [] refreshTokenValue = this .lobHandler .getBlobAsBytes (rs , "refresh_token_value" );
405
396
if (refreshTokenValue != null ) {
406
- tokenValue = new String (refreshTokenValue ,
407
- StandardCharsets .UTF_8 );
397
+ tokenValue = new String (refreshTokenValue , StandardCharsets .UTF_8 );
408
398
tokenIssuedAt = rs .getTimestamp ("refresh_token_issued_at" ).toInstant ();
409
399
tokenExpiresAt = null ;
410
400
Timestamp refreshTokenExpiresAt = rs .getTimestamp ("refresh_token_expires_at" );
@@ -415,8 +405,7 @@ public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException
415
405
416
406
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken2 (
417
407
tokenValue , tokenIssuedAt , tokenExpiresAt );
418
- builder
419
- .token (refreshToken , (metadata ) -> metadata .putAll (refreshTokenMetadata ));
408
+ builder .token (refreshToken , (metadata ) -> metadata .putAll (refreshTokenMetadata ));
420
409
}
421
410
return builder .build ();
422
411
} catch (JsonProcessingException e ) {
@@ -428,13 +417,15 @@ public final void setLobHandler(LobHandler lobHandler) {
428
417
Assert .notNull (lobHandler , "lobHandler cannot be null" );
429
418
this .lobHandler = lobHandler ;
430
419
}
420
+
431
421
}
432
422
433
423
/**
434
424
* The default {@code Function} that maps {@link OAuth2Authorization} to a
435
425
* {@code List} of {@link SqlParameterValue}.
436
426
*/
437
427
public static class OAuth2AuthorizationParametersMapper implements Function <OAuth2Authorization , List <SqlParameterValue >> {
428
+
438
429
private final ObjectMapper objectMapper ;
439
430
440
431
public OAuth2AuthorizationParametersMapper (ObjectMapper objectMapper ) {
@@ -444,7 +435,6 @@ public OAuth2AuthorizationParametersMapper(ObjectMapper objectMapper) {
444
435
445
436
@ Override
446
437
public List <SqlParameterValue > apply (OAuth2Authorization authorization ) {
447
-
448
438
try {
449
439
List <SqlParameterValue > parameters = new ArrayList <>();
450
440
parameters .add (new SqlParameterValue (Types .VARCHAR , authorization .getId ()));
@@ -495,7 +485,6 @@ public List<SqlParameterValue> apply(OAuth2Authorization authorization) {
495
485
} catch (JsonProcessingException e ) {
496
486
throw new IllegalArgumentException (e .getMessage (), e );
497
487
}
498
-
499
488
}
500
489
501
490
private <T extends AbstractOAuth2Token > List <SqlParameterValue > toSqlParameterList (OAuth2Authorization .Token <T > token ) throws JsonProcessingException {
@@ -505,7 +494,6 @@ private <T extends AbstractOAuth2Token> List<SqlParameterValue> toSqlParameterLi
505
494
Timestamp tokenExpiresAt = null ;
506
495
String codeMetadata = null ;
507
496
if (token != null ) {
508
-
509
497
tokenValue = token .getToken ().getTokenValue ().getBytes (StandardCharsets .UTF_8 );
510
498
if (token .getToken ().getIssuedAt () != null ) {
511
499
tokenIssuedAt = Timestamp .from (token .getToken ().getIssuedAt ());
@@ -522,6 +510,7 @@ private <T extends AbstractOAuth2Token> List<SqlParameterValue> toSqlParameterLi
522
510
parameters .add (new SqlParameterValue (Types .VARCHAR , codeMetadata ));
523
511
return parameters ;
524
512
}
513
+
525
514
}
526
515
527
516
private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter {
@@ -551,4 +540,5 @@ protected void doSetValue(PreparedStatement ps, int parameterPosition, Object ar
551
540
}
552
541
553
542
}
543
+
554
544
}
0 commit comments