15
15
*/
16
16
package org .springframework .security .oauth2 .server .authorization .client ;
17
17
18
+ import com .fasterxml .jackson .core .JsonProcessingException ;
19
+ import com .fasterxml .jackson .databind .ObjectMapper ;
18
20
import org .springframework .jdbc .core .*;
19
21
import org .springframework .jdbc .support .lob .DefaultLobHandler ;
20
22
import org .springframework .jdbc .support .lob .LobCreator ;
@@ -54,18 +56,15 @@ public class JdbcRegisteredClientRepository implements RegisteredClientRepositor
54
56
+ "authorization_grant_types, "
55
57
+ "redirect_uris, "
56
58
+ "scopes, "
57
- + "require_proof_key, "
58
- + "require_user_consent, "
59
- + "access_token_ttl, "
60
- + "reuse_refresh_tokens, "
61
- + "refresh_token_ttl" ;
59
+ + "client_settings,"
60
+ + "token_settings" ;
62
61
63
62
private static final String TABLE_NAME = "oauth2_registered_client" ;
64
63
65
64
private static final String LOAD_REGISTERED_CLIENT_SQL = "SELECT " + COLUMN_NAMES + " FROM " + TABLE_NAME + " WHERE " ;
66
65
67
66
private static final String INSERT_REGISTERED_CLIENT_SQL = "INSERT INTO " + TABLE_NAME
68
- + "(" + COLUMN_NAMES + ") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" ;
67
+ + "(" + COLUMN_NAMES + ") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ;
69
68
70
69
private RowMapper <RegisteredClient > registeredClientRowMapper ;
71
70
@@ -75,9 +74,13 @@ public class JdbcRegisteredClientRepository implements RegisteredClientRepositor
75
74
76
75
private final LobHandler lobHandler = new DefaultLobHandler ();
77
76
78
- public JdbcRegisteredClientRepository (JdbcOperations jdbcOperations ) {
77
+ private final ObjectMapper objectMapper ;
78
+
79
+ public JdbcRegisteredClientRepository (JdbcOperations jdbcOperations , ObjectMapper objectMapper ) {
79
80
Assert .notNull (jdbcOperations , "jdbcOperations cannot be null" );
81
+ Assert .notNull (objectMapper , "objectMapper cannot be null" );
80
82
this .jdbcOperations = jdbcOperations ;
83
+ this .objectMapper = objectMapper ;
81
84
this .registeredClientRowMapper = new DefaultRegisteredClientRowMapper ();
82
85
this .registeredClientParametersMapper = new DefaultRegisteredClientParametersMapper ();
83
86
}
@@ -145,7 +148,7 @@ private RegisteredClient findBy(String condStr, Object...args) {
145
148
return !lst .isEmpty () ? lst .get (0 ) : null ;
146
149
}
147
150
148
- private static class DefaultRegisteredClientRowMapper implements RowMapper <RegisteredClient > {
151
+ private class DefaultRegisteredClientRowMapper implements RowMapper <RegisteredClient > {
149
152
150
153
private final LobHandler lobHandler = new DefaultLobHandler ();
151
154
@@ -154,6 +157,7 @@ private Collection<String> parseList(String s) {
154
157
}
155
158
156
159
@ Override
160
+ @ SuppressWarnings ("unchecked" )
157
161
public RegisteredClient mapRow (ResultSet rs , int rowNum ) throws SQLException {
158
162
Collection <String > scopes = parseList (rs .getString ("scopes" ));
159
163
List <AuthorizationGrantType > authGrantTypes = parseList (rs .getString ("authorization_grant_types" ))
@@ -180,54 +184,102 @@ public RegisteredClient mapRow(ResultSet rs, int rowNum) throws SQLException {
180
184
RegisteredClient rc = builder .build ();
181
185
182
186
TokenSettings ts = rc .getTokenSettings ();
183
- ts .accessTokenTimeToLive (Duration .ofMillis (rs .getLong ("access_token_ttl" )));
184
- ts .refreshTokenTimeToLive (Duration .ofMillis (rs .getLong ("refresh_token_ttl" )));
185
- ts .reuseRefreshTokens (rs .getBoolean ("reuse_refresh_tokens" ));
186
-
187
187
ClientSettings cs = rc .getClientSettings ();
188
- cs .requireProofKey (rs .getBoolean ("require_proof_key" ));
189
- cs .requireUserConsent (rs .getBoolean ("require_user_consent" ));
188
+
189
+ try {
190
+ String tokenSettingsJson = rs .getString ("token_settings" );
191
+ if (tokenSettingsJson != null ) {
192
+
193
+ Map <String , Object > m = JdbcRegisteredClientRepository .this .objectMapper .readValue (tokenSettingsJson , Map .class );
194
+
195
+ Number accessTokenTTL = (Number )m .get ("access_token_ttl" );
196
+ if (accessTokenTTL != null ) {
197
+ ts .accessTokenTimeToLive (Duration .ofMillis (accessTokenTTL .longValue ()));
198
+ }
199
+
200
+ Number refreshTokenTTL = (Number )m .get ("refresh_token_ttl" );
201
+ if (refreshTokenTTL != null ) {
202
+ ts .refreshTokenTimeToLive (Duration .ofMillis (refreshTokenTTL .longValue ()));
203
+ }
204
+
205
+ Boolean reuseRefreshTokens = (Boolean )m .get ("reuse_refresh_tokens" );
206
+ if (reuseRefreshTokens != null ) {
207
+ ts .reuseRefreshTokens (reuseRefreshTokens );
208
+ }
209
+ }
210
+
211
+ String clientSettingsJson = rs .getString ("client_settings" );
212
+ if (clientSettingsJson != null ) {
213
+
214
+ Map <String , Object > m = JdbcRegisteredClientRepository .this .objectMapper .readValue (clientSettingsJson , Map .class );
215
+
216
+ Boolean requireProofKey = (Boolean )m .get ("require_proof_key" );
217
+ if (requireProofKey != null ) {
218
+ cs .requireProofKey (requireProofKey );
219
+ }
220
+
221
+ Boolean requireUserConsent = (Boolean )m .get ("require_user_consent" );
222
+ if (requireUserConsent != null ) {
223
+ cs .requireUserConsent (requireUserConsent );
224
+ }
225
+ }
226
+
227
+
228
+ } catch (JsonProcessingException e ) {
229
+ throw new IllegalArgumentException (e .getMessage (), e );
230
+ }
190
231
191
232
return rc ;
192
233
}
193
234
}
194
235
195
- private static class DefaultRegisteredClientParametersMapper implements Function <RegisteredClient , List <SqlParameterValue >> {
236
+ private class DefaultRegisteredClientParametersMapper implements Function <RegisteredClient , List <SqlParameterValue >> {
196
237
@ Override
197
238
public List <SqlParameterValue > apply (RegisteredClient registeredClient ) {
239
+ try {
240
+ List <String > clientAuthenticationMethodNames = new ArrayList <>(registeredClient .getClientAuthenticationMethods ().size ());
241
+ for (ClientAuthenticationMethod clientAuthenticationMethod : registeredClient .getClientAuthenticationMethods ()) {
242
+ clientAuthenticationMethodNames .add (clientAuthenticationMethod .getValue ());
243
+ }
198
244
199
- List <String > clientAuthenticationMethodNames = new ArrayList <>(registeredClient .getClientAuthenticationMethods ().size ());
200
- for (ClientAuthenticationMethod clientAuthenticationMethod : registeredClient .getClientAuthenticationMethods ()) {
201
- clientAuthenticationMethodNames .add (clientAuthenticationMethod .getValue ());
202
- }
245
+ List <String > authorizationGrantTypeNames = new ArrayList <>(registeredClient .getAuthorizationGrantTypes ().size ());
246
+ for (AuthorizationGrantType authorizationGrantType : registeredClient .getAuthorizationGrantTypes ()) {
247
+ authorizationGrantTypeNames .add (authorizationGrantType .getValue ());
248
+ }
203
249
204
- List <String > authorizationGrantTypeNames = new ArrayList <>(registeredClient .getAuthorizationGrantTypes ().size ());
205
- for (AuthorizationGrantType authorizationGrantType : registeredClient .getAuthorizationGrantTypes ()) {
206
- authorizationGrantTypeNames .add (authorizationGrantType .getValue ());
250
+ Instant issuedAt = registeredClient .getClientIdIssuedAt () != null ?
251
+ registeredClient .getClientIdIssuedAt () : Instant .now ();
252
+
253
+ Timestamp clientSecretExpiresAt = registeredClient .getClientSecretExpiresAt () != null ?
254
+ Timestamp .from (registeredClient .getClientSecretExpiresAt ()) : null ;
255
+
256
+ Map <String ,Object > clientSettings = new HashMap <>();
257
+ clientSettings .put ("require_proof_key" , registeredClient .getClientSettings ().requireProofKey ());
258
+ clientSettings .put ("require_user_consent" , registeredClient .getClientSettings ().requireUserConsent ());
259
+ String clientSettingsJson = JdbcRegisteredClientRepository .this .objectMapper .writeValueAsString (clientSettings );
260
+
261
+ Map <String ,Object > tokenSettings = new HashMap <>();
262
+ tokenSettings .put ("access_token_ttl" , registeredClient .getTokenSettings ().accessTokenTimeToLive ().toMillis ());
263
+ tokenSettings .put ("reuse_refresh_tokens" , registeredClient .getTokenSettings ().reuseRefreshTokens ());
264
+ tokenSettings .put ("refresh_token_ttl" , registeredClient .getTokenSettings ().refreshTokenTimeToLive ().toMillis ());
265
+ String tokenSettingsJson = JdbcRegisteredClientRepository .this .objectMapper .writeValueAsString (tokenSettings );
266
+
267
+ return Arrays .asList (
268
+ new SqlParameterValue (Types .VARCHAR , registeredClient .getId ()),
269
+ new SqlParameterValue (Types .VARCHAR , registeredClient .getClientId ()),
270
+ new SqlParameterValue (Types .TIMESTAMP , Timestamp .from (issuedAt )),
271
+ new SqlParameterValue (Types .BLOB , registeredClient .getClientSecret ().getBytes (StandardCharsets .UTF_8 )),
272
+ new SqlParameterValue (Types .TIMESTAMP , clientSecretExpiresAt ),
273
+ new SqlParameterValue (Types .VARCHAR , registeredClient .getClientName ()),
274
+ new SqlParameterValue (Types .VARCHAR , String .join ("|" , clientAuthenticationMethodNames )),
275
+ new SqlParameterValue (Types .VARCHAR , String .join ("|" , authorizationGrantTypeNames )),
276
+ new SqlParameterValue (Types .VARCHAR , String .join ("|" , registeredClient .getRedirectUris ())),
277
+ new SqlParameterValue (Types .VARCHAR , String .join ("|" , registeredClient .getScopes ())),
278
+ new SqlParameterValue (Types .VARCHAR , clientSettingsJson ),
279
+ new SqlParameterValue (Types .VARCHAR , tokenSettingsJson ));
280
+ } catch (JsonProcessingException e ) {
281
+ throw new IllegalArgumentException (e .getMessage (), e );
207
282
}
208
-
209
- Instant issuedAt = registeredClient .getClientIdIssuedAt () != null ?
210
- registeredClient .getClientIdIssuedAt () : Instant .now ();
211
-
212
- Timestamp clientSecretExpiresAt = registeredClient .getClientSecretExpiresAt () != null ?
213
- Timestamp .from (registeredClient .getClientSecretExpiresAt ()) : null ;
214
-
215
- return Arrays .asList (
216
- new SqlParameterValue (Types .VARCHAR , registeredClient .getId ()),
217
- new SqlParameterValue (Types .VARCHAR , registeredClient .getClientId ()),
218
- new SqlParameterValue (Types .TIMESTAMP , Timestamp .from (issuedAt )),
219
- new SqlParameterValue (Types .BLOB , registeredClient .getClientSecret ().getBytes (StandardCharsets .UTF_8 )),
220
- new SqlParameterValue (Types .TIMESTAMP , clientSecretExpiresAt ),
221
- new SqlParameterValue (Types .VARCHAR , registeredClient .getClientName ()),
222
- new SqlParameterValue (Types .VARCHAR , String .join ("|" , clientAuthenticationMethodNames )),
223
- new SqlParameterValue (Types .VARCHAR , String .join ("|" , authorizationGrantTypeNames )),
224
- new SqlParameterValue (Types .VARCHAR , String .join ("|" , registeredClient .getRedirectUris ())),
225
- new SqlParameterValue (Types .VARCHAR , String .join ("|" , registeredClient .getScopes ())),
226
- new SqlParameterValue (Types .BOOLEAN , registeredClient .getClientSettings ().requireProofKey ()),
227
- new SqlParameterValue (Types .BOOLEAN , registeredClient .getClientSettings ().requireUserConsent ()),
228
- new SqlParameterValue (Types .NUMERIC , registeredClient .getTokenSettings ().accessTokenTimeToLive ().toMillis ()),
229
- new SqlParameterValue (Types .BOOLEAN , registeredClient .getTokenSettings ().reuseRefreshTokens ()),
230
- new SqlParameterValue (Types .NUMERIC , registeredClient .getTokenSettings ().refreshTokenTimeToLive ().toMillis ()));
231
283
}
232
284
}
233
285
0 commit comments