47
47
import org .springframework .jdbc .support .lob .LobHandler ;
48
48
import org .springframework .lang .Nullable ;
49
49
import org .springframework .security .jackson2 .SecurityJackson2Modules ;
50
- import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
51
- import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
52
- import org .springframework .security .oauth2 .core .OAuth2RefreshToken ;
53
- import org .springframework .security .oauth2 .core .OAuth2Token ;
50
+ import org .springframework .security .oauth2 .core .*;
54
51
import org .springframework .security .oauth2 .core .endpoint .OAuth2ParameterNames ;
55
52
import org .springframework .security .oauth2 .core .oidc .OidcIdToken ;
56
53
import org .springframework .security .oauth2 .core .oidc .endpoint .OidcParameterNames ;
@@ -106,20 +103,31 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
106
103
+ "refresh_token_value,"
107
104
+ "refresh_token_issued_at,"
108
105
+ "refresh_token_expires_at,"
109
- + "refresh_token_metadata" ;
106
+ + "refresh_token_metadata,"
107
+ + "user_code_value,"
108
+ + "user_code_issued_at,"
109
+ + "user_code_expires_at,"
110
+ + "user_code_metadata,"
111
+ + "device_code_value,"
112
+ + "device_code_issued_at,"
113
+ + "device_code_expires_at,"
114
+ + "device_code_metadata" ;
110
115
// @formatter:on
111
116
112
117
private static final String TABLE_NAME = "oauth2_authorization" ;
113
118
114
119
private static final String PK_FILTER = "id = ?" ;
115
- private static final String UNKNOWN_TOKEN_TYPE_FILTER = "state = ? OR authorization_code_value = ? OR " +
116
- "access_token_value = ? OR oidc_id_token_value = ? OR refresh_token_value = ?" ;
120
+ private static final String UNKNOWN_TOKEN_TYPE_FILTER = "state = ? OR authorization_code_value = ? OR "
121
+ + "access_token_value = ? OR oidc_id_token_value = ? OR refresh_token_value = ? OR "
122
+ + "user_code_value = ? OR device_code_value = ?" ;
117
123
118
124
private static final String STATE_FILTER = "state = ?" ;
119
125
private static final String AUTHORIZATION_CODE_FILTER = "authorization_code_value = ?" ;
120
126
private static final String ACCESS_TOKEN_FILTER = "access_token_value = ?" ;
121
127
private static final String ID_TOKEN_FILTER = "oidc_id_token_value = ?" ;
122
128
private static final String REFRESH_TOKEN_FILTER = "refresh_token_value = ?" ;
129
+ private static final String USER_CODE_FILTER = "user_code_value = ?" ;
130
+ private static final String DEVICE_CODE_FILTER = "device_code_value = ?" ;
123
131
124
132
// @formatter:off
125
133
private static final String LOAD_AUTHORIZATION_SQL = "SELECT " + COLUMN_NAMES
@@ -129,7 +137,7 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
129
137
130
138
// @formatter:off
131
139
private static final String SAVE_AUTHORIZATION_SQL = "INSERT INTO " + TABLE_NAME
132
- + " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ;
140
+ + " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" ;
133
141
// @formatter:on
134
142
135
143
// @formatter:off
@@ -138,7 +146,9 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
138
146
+ " authorization_code_value = ?, authorization_code_issued_at = ?, authorization_code_expires_at = ?, authorization_code_metadata = ?,"
139
147
+ " access_token_value = ?, access_token_issued_at = ?, access_token_expires_at = ?, access_token_metadata = ?, access_token_type = ?, access_token_scopes = ?,"
140
148
+ " oidc_id_token_value = ?, oidc_id_token_issued_at = ?, oidc_id_token_expires_at = ?, oidc_id_token_metadata = ?,"
141
- + " refresh_token_value = ?, refresh_token_issued_at = ?, refresh_token_expires_at = ?, refresh_token_metadata = ?"
149
+ + " refresh_token_value = ?, refresh_token_issued_at = ?, refresh_token_expires_at = ?, refresh_token_metadata = ?,"
150
+ + " user_code_value = ?, user_code_issued_at = ?, user_code_expires_at = ?, user_code_metadata = ?,"
151
+ + " device_code_value = ?, device_code_issued_at = ?, device_code_expires_at = ?, device_code_metadata = ?"
142
152
+ " WHERE " + PK_FILTER ;
143
153
// @formatter:on
144
154
@@ -244,6 +254,8 @@ public OAuth2Authorization findByToken(String token, @Nullable OAuth2TokenType t
244
254
parameters .add (mapToSqlParameter ("access_token_value" , token ));
245
255
parameters .add (mapToSqlParameter ("oidc_id_token_value" , token ));
246
256
parameters .add (mapToSqlParameter ("refresh_token_value" , token ));
257
+ parameters .add (mapToSqlParameter ("user_code_value" , token ));
258
+ parameters .add (mapToSqlParameter ("device_code_value" , token ));
247
259
return findBy (UNKNOWN_TOKEN_TYPE_FILTER , parameters );
248
260
} else if (OAuth2ParameterNames .STATE .equals (tokenType .getValue ())) {
249
261
parameters .add (new SqlParameterValue (Types .VARCHAR , token ));
@@ -260,6 +272,12 @@ public OAuth2Authorization findByToken(String token, @Nullable OAuth2TokenType t
260
272
} else if (OAuth2TokenType .REFRESH_TOKEN .equals (tokenType )) {
261
273
parameters .add (mapToSqlParameter ("refresh_token_value" , token ));
262
274
return findBy (REFRESH_TOKEN_FILTER , parameters );
275
+ } else if (OAuth2TokenType .USER_CODE .equals (tokenType )) {
276
+ parameters .add (mapToSqlParameter ("user_code_value" , token ));
277
+ return findBy (USER_CODE_FILTER , parameters );
278
+ } else if (OAuth2TokenType .DEVICE_CODE .equals (tokenType )) {
279
+ parameters .add (mapToSqlParameter ("device_code_value" , token ));
280
+ return findBy (DEVICE_CODE_FILTER , parameters );
263
281
}
264
282
return null ;
265
283
}
@@ -425,6 +443,35 @@ public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException
425
443
refreshTokenValue , tokenIssuedAt , tokenExpiresAt );
426
444
builder .token (refreshToken , (metadata ) -> metadata .putAll (refreshTokenMetadata ));
427
445
}
446
+
447
+ String userCodeValue = getLobValue (rs , "user_code_value" );
448
+ if (StringUtils .hasText (userCodeValue )) {
449
+ tokenIssuedAt = rs .getTimestamp ("user_code_issued_at" ).toInstant ();
450
+ tokenExpiresAt = null ;
451
+ Timestamp userCodeExpiresAt = rs .getTimestamp ("user_code_expires_at" );
452
+ if (userCodeExpiresAt != null ) {
453
+ tokenExpiresAt = userCodeExpiresAt .toInstant ();
454
+ }
455
+ Map <String , Object > userCodeMetadata = parseMap (getLobValue (rs , "user_code_metadata" ));
456
+
457
+ OAuth2UserCode userCode = new OAuth2UserCode (userCodeValue , tokenIssuedAt , tokenExpiresAt );
458
+ builder .token (userCode , (metadata ) -> metadata .putAll (userCodeMetadata ));
459
+ }
460
+
461
+ String deviceCodeValue = getLobValue (rs , "device_code_value" );
462
+ if (StringUtils .hasText (deviceCodeValue )) {
463
+ tokenIssuedAt = rs .getTimestamp ("device_code_issued_at" ).toInstant ();
464
+ tokenExpiresAt = null ;
465
+ Timestamp deviceCodeExpiresAt = rs .getTimestamp ("device_code_expires_at" );
466
+ if (deviceCodeExpiresAt != null ) {
467
+ tokenExpiresAt = deviceCodeExpiresAt .toInstant ();
468
+ }
469
+ Map <String , Object > deviceCodeMetadata = parseMap (getLobValue (rs , "device_code_metadata" ));
470
+
471
+ OAuth2DeviceCode deviceCode = new OAuth2DeviceCode (deviceCodeValue , tokenIssuedAt , tokenExpiresAt );
472
+ builder .token (deviceCode , (metadata ) -> metadata .putAll (deviceCodeMetadata ));
473
+ }
474
+
428
475
return builder .build ();
429
476
}
430
477
@@ -545,6 +592,17 @@ public List<SqlParameterValue> apply(OAuth2Authorization authorization) {
545
592
List <SqlParameterValue > refreshTokenSqlParameters = toSqlParameterList (
546
593
"refresh_token_value" , "refresh_token_metadata" , refreshToken );
547
594
parameters .addAll (refreshTokenSqlParameters );
595
+
596
+ OAuth2Authorization .Token <OAuth2UserCode > userCode = authorization .getToken (OAuth2UserCode .class );
597
+ List <SqlParameterValue > userCodeSqlParameters = toSqlParameterList (
598
+ "user_code_value" , "user_code_metadata" , userCode );
599
+ parameters .addAll (userCodeSqlParameters );
600
+
601
+ OAuth2Authorization .Token <OAuth2DeviceCode > deviceCode = authorization .getToken (OAuth2DeviceCode .class );
602
+ List <SqlParameterValue > deviceCodeSqlParameters = toSqlParameterList (
603
+ "device_code_value" , "device_code_metadata" , deviceCode );
604
+ parameters .addAll (deviceCodeSqlParameters );
605
+
548
606
return parameters ;
549
607
}
550
608
@@ -670,6 +728,14 @@ private static void initColumnMetadata(JdbcOperations jdbcOperations) {
670
728
columnMetadataMap .put (columnMetadata .getColumnName (), columnMetadata );
671
729
columnMetadata = getColumnMetadata (jdbcOperations , "refresh_token_metadata" , Types .BLOB );
672
730
columnMetadataMap .put (columnMetadata .getColumnName (), columnMetadata );
731
+ columnMetadata = getColumnMetadata (jdbcOperations , "user_code_value" , Types .BLOB );
732
+ columnMetadataMap .put (columnMetadata .getColumnName (), columnMetadata );
733
+ columnMetadata = getColumnMetadata (jdbcOperations , "user_code_metadata" , Types .BLOB );
734
+ columnMetadataMap .put (columnMetadata .getColumnName (), columnMetadata );
735
+ columnMetadata = getColumnMetadata (jdbcOperations , "device_code_value" , Types .BLOB );
736
+ columnMetadataMap .put (columnMetadata .getColumnName (), columnMetadata );
737
+ columnMetadata = getColumnMetadata (jdbcOperations , "device_code_metadata" , Types .BLOB );
738
+ columnMetadataMap .put (columnMetadata .getColumnName (), columnMetadata );
673
739
}
674
740
675
741
private static ColumnMetadata getColumnMetadata (JdbcOperations jdbcOperations , String columnName , int defaultDataType ) {
0 commit comments