30
30
import java .util .Set ;
31
31
import java .util .function .Function ;
32
32
33
- import com .fasterxml .jackson .core .JsonProcessingException ;
33
+ import com .fasterxml .jackson .core .type . TypeReference ;
34
34
import com .fasterxml .jackson .databind .ObjectMapper ;
35
35
36
36
import org .springframework .jdbc .core .ArgumentPreparedStatementSetter ;
@@ -88,29 +88,18 @@ public class JdbcRegisteredClientRepository implements RegisteredClientRepositor
88
88
* @param jdbcOperations the JDBC operations
89
89
*/
90
90
public JdbcRegisteredClientRepository (JdbcOperations jdbcOperations ) {
91
- this (jdbcOperations , new ObjectMapper ());
92
- }
93
-
94
- /**
95
- * Constructs a {@code JdbcRegisteredClientRepository} using the provided parameters.
96
- *
97
- * @param jdbcOperations the JDBC operations
98
- * @param objectMapper the object mapper
99
- */
100
- public JdbcRegisteredClientRepository (JdbcOperations jdbcOperations , ObjectMapper objectMapper ) {
101
91
Assert .notNull (jdbcOperations , "jdbcOperations cannot be null" );
102
- Assert .notNull (objectMapper , "objectMapper cannot be null" );
103
92
this .jdbcOperations = jdbcOperations ;
104
- this .registeredClientRowMapper = new DefaultRegisteredClientRowMapper (objectMapper );
105
- this .registeredClientParametersMapper = new DefaultRegisteredClientParametersMapper (objectMapper );
93
+ this .registeredClientRowMapper = new DefaultRegisteredClientRowMapper ();
94
+ this .registeredClientParametersMapper = new DefaultRegisteredClientParametersMapper ();
106
95
}
107
96
108
97
/**
109
98
* Allows changing of {@link RegisteredClient} row mapper implementation
110
99
*
111
100
* @param registeredClientRowMapper mapper implementation
112
101
*/
113
- public void setRegisteredClientRowMapper (RowMapper <RegisteredClient > registeredClientRowMapper ) {
102
+ public final void setRegisteredClientRowMapper (RowMapper <RegisteredClient > registeredClientRowMapper ) {
114
103
Assert .notNull (registeredClientRowMapper , "registeredClientRowMapper cannot be null" );
115
104
this .registeredClientRowMapper = registeredClientRowMapper ;
116
105
}
@@ -120,18 +109,30 @@ public void setRegisteredClientRowMapper(RowMapper<RegisteredClient> registeredC
120
109
*
121
110
* @param registeredClientParametersMapper mapper implementation
122
111
*/
123
- public void setRegisteredClientParametersMapper (Function <RegisteredClient , List <SqlParameterValue >> registeredClientParametersMapper ) {
112
+ public final void setRegisteredClientParametersMapper (Function <RegisteredClient , List <SqlParameterValue >> registeredClientParametersMapper ) {
124
113
Assert .notNull (registeredClientParametersMapper , "registeredClientParameterMapper cannot be null" );
125
114
this .registeredClientParametersMapper = registeredClientParametersMapper ;
126
115
}
127
116
117
+ protected final JdbcOperations getJdbcOperations () {
118
+ return this .jdbcOperations ;
119
+ }
120
+
121
+ protected final RowMapper <RegisteredClient > getRegisteredClientRowMapper () {
122
+ return this .registeredClientRowMapper ;
123
+ }
124
+
125
+ protected final Function <RegisteredClient , List <SqlParameterValue >> getRegisteredClientParametersMapper () {
126
+ return this .registeredClientParametersMapper ;
127
+ }
128
+
128
129
@ Override
129
130
public void save (RegisteredClient registeredClient ) {
130
131
Assert .notNull (registeredClient , "registeredClient cannot be null" );
131
- RegisteredClient foundClient = this . findBy ("id = ? OR client_id = ?" ,
132
+ RegisteredClient foundClient = findBy ("id = ? OR client_id = ?" ,
132
133
registeredClient .getId (), registeredClient .getClientId ());
133
134
134
- if (null != foundClient ) {
135
+ if (foundClient != null ) {
135
136
Assert .isTrue (!foundClient .getId ().equals (registeredClient .getId ()),
136
137
"Registered client must be unique. Found duplicate identifier: " + registeredClient .getId ());
137
138
Assert .isTrue (!foundClient .getClientId ().equals (registeredClient .getClientId ()),
@@ -155,29 +156,20 @@ public RegisteredClient findByClientId(String clientId) {
155
156
return findBy ("client_id = ?" , clientId );
156
157
}
157
158
158
- private RegisteredClient findBy (String condStr , Object ...args ) {
159
- List <RegisteredClient > lst = this .jdbcOperations .query (
159
+ private RegisteredClient findBy (String condStr , Object ... args ) {
160
+ List <RegisteredClient > result = this .jdbcOperations .query (
160
161
LOAD_REGISTERED_CLIENT_SQL + condStr ,
161
- registeredClientRowMapper , args );
162
- return !lst .isEmpty () ? lst .get (0 ) : null ;
162
+ this . registeredClientRowMapper , args );
163
+ return !result .isEmpty () ? result .get (0 ) : null ;
163
164
}
164
165
165
166
public static class DefaultRegisteredClientRowMapper implements RowMapper <RegisteredClient > {
166
167
167
- private final ObjectMapper objectMapper ;
168
-
169
- public DefaultRegisteredClientRowMapper (ObjectMapper objectMapper ) {
170
- this .objectMapper = objectMapper ;
171
- }
172
-
173
- private Set <String > parseList (String s ) {
174
- return s != null ? StringUtils .commaDelimitedListToSet (s ) : Collections .emptySet ();
175
- }
168
+ private ObjectMapper objectMapper = new ObjectMapper ();
176
169
177
170
@ Override
178
- @ SuppressWarnings ("unchecked" )
179
171
public RegisteredClient mapRow (ResultSet rs , int rowNum ) throws SQLException {
180
- Set <String > scopes = parseList (rs .getString ("scopes" ));
172
+ Set <String > clientScopes = parseList (rs .getString ("scopes" ));
181
173
Set <String > authGrantTypes = parseList (rs .getString ("authorization_grant_types" ));
182
174
Set <String > clientAuthMethods = parseList (rs .getString ("client_authentication_methods" ));
183
175
Set <String > redirectUris = parseList (rs .getString ("redirect_uris" ));
@@ -191,115 +183,140 @@ public RegisteredClient mapRow(ResultSet rs, int rowNum) throws SQLException {
191
183
.clientSecret (clientSecret )
192
184
.clientSecretExpiresAt (clientSecretExpiresAt != null ? clientSecretExpiresAt .toInstant () : null )
193
185
.clientName (rs .getString ("client_name" ))
194
- .authorizationGrantTypes (coll -> authGrantTypes .forEach (authGrantType ->
195
- coll .add (AUTHORIZATION_GRANT_TYPE_MAP .get (authGrantType ))))
196
- .clientAuthenticationMethods (coll -> clientAuthMethods .forEach (clientAuthMethod ->
197
- coll .add (CLIENT_AUTHENTICATION_METHOD_MAP .get (clientAuthMethod ))))
198
- .redirectUris (coll -> coll .addAll (redirectUris ))
199
- .scopes (coll -> coll .addAll (scopes ));
200
-
201
- RegisteredClient rc = builder .build ();
186
+ .authorizationGrantTypes ((grantTypes ) -> authGrantTypes .forEach (authGrantType ->
187
+ grantTypes .add (AUTHORIZATION_GRANT_TYPE_MAP .get (authGrantType ))))
188
+ .clientAuthenticationMethods ((authenticationMethods ) -> clientAuthMethods .forEach (clientAuthMethod ->
189
+ authenticationMethods .add (CLIENT_AUTHENTICATION_METHOD_MAP .get (clientAuthMethod ))))
190
+ .redirectUris ((uris ) -> uris .addAll (redirectUris ))
191
+ .scopes ((scopes ) -> scopes .addAll (clientScopes ));
192
+
193
+ RegisteredClient registeredClient = builder .build ();
194
+
195
+ String tokenSettingsJson = rs .getString ("token_settings" );
196
+ if (tokenSettingsJson != null ) {
197
+ Map <String , Object > settings = parseMap (tokenSettingsJson );
198
+ TokenSettings tokenSettings = registeredClient .getTokenSettings ();
199
+
200
+ Number accessTokenTTL = (Number ) settings .get ("access_token_ttl" );
201
+ if (accessTokenTTL != null ) {
202
+ tokenSettings .accessTokenTimeToLive (Duration .ofMillis (accessTokenTTL .longValue ()));
203
+ }
202
204
203
- TokenSettings ts = rc .getTokenSettings ();
204
- ClientSettings cs = rc .getClientSettings ();
205
+ Number refreshTokenTTL = (Number ) settings .get ("refresh_token_ttl" );
206
+ if (refreshTokenTTL != null ) {
207
+ tokenSettings .refreshTokenTimeToLive (Duration .ofMillis (refreshTokenTTL .longValue ()));
208
+ }
205
209
206
- try {
207
- String tokenSettingsJson = rs .getString ("token_settings" );
208
- if (tokenSettingsJson != null ) {
209
- Map <String , Object > m = this .objectMapper .readValue (tokenSettingsJson , Map .class );
210
-
211
- Number accessTokenTTL = (Number ) m .get ("access_token_ttl" );
212
- if (accessTokenTTL != null ) {
213
- ts .accessTokenTimeToLive (Duration .ofMillis (accessTokenTTL .longValue ()));
214
- }
215
-
216
- Number refreshTokenTTL = (Number ) m .get ("refresh_token_ttl" );
217
- if (refreshTokenTTL != null ) {
218
- ts .refreshTokenTimeToLive (Duration .ofMillis (refreshTokenTTL .longValue ()));
219
- }
220
-
221
- Boolean reuseRefreshTokens = (Boolean ) m .get ("reuse_refresh_tokens" );
222
- if (reuseRefreshTokens != null ) {
223
- ts .reuseRefreshTokens (reuseRefreshTokens );
224
- }
210
+ Boolean reuseRefreshTokens = (Boolean ) settings .get ("reuse_refresh_tokens" );
211
+ if (reuseRefreshTokens != null ) {
212
+ tokenSettings .reuseRefreshTokens (reuseRefreshTokens );
225
213
}
214
+ }
226
215
227
- String clientSettingsJson = rs .getString ("client_settings" );
228
- if (clientSettingsJson != null ) {
229
- Map <String , Object > m = this .objectMapper .readValue (clientSettingsJson , Map .class );
216
+ String clientSettingsJson = rs .getString ("client_settings" );
217
+ if (clientSettingsJson != null ) {
218
+ Map <String , Object > settings = parseMap (clientSettingsJson );
219
+ ClientSettings clientSettings = registeredClient .getClientSettings ();
230
220
231
- Boolean requireProofKey = (Boolean ) m .get ("require_proof_key" );
232
- if (requireProofKey != null ) {
233
- cs .requireProofKey (requireProofKey );
234
- }
221
+ Boolean requireProofKey = (Boolean ) settings .get ("require_proof_key" );
222
+ if (requireProofKey != null ) {
223
+ clientSettings .requireProofKey (requireProofKey );
224
+ }
235
225
236
- Boolean requireUserConsent = (Boolean ) m .get ("require_user_consent" );
237
- if (requireUserConsent != null ) {
238
- cs .requireUserConsent (requireUserConsent );
239
- }
226
+ Boolean requireUserConsent = (Boolean ) settings .get ("require_user_consent" );
227
+ if (requireUserConsent != null ) {
228
+ clientSettings .requireUserConsent (requireUserConsent );
240
229
}
241
- } catch (JsonProcessingException e ) {
242
- throw new IllegalArgumentException (e .getMessage (), e );
243
230
}
244
231
245
- return rc ;
232
+ return registeredClient ;
246
233
}
247
234
248
- }
235
+ public final void setObjectMapper (ObjectMapper objectMapper ) {
236
+ Assert .notNull (objectMapper , "objectMapper cannot be null" );
237
+ this .objectMapper = objectMapper ;
238
+ }
249
239
250
- public static class DefaultRegisteredClientParametersMapper implements Function <RegisteredClient , List <SqlParameterValue >> {
240
+ protected final ObjectMapper getObjectMapper () {
241
+ return this .objectMapper ;
242
+ }
251
243
252
- private final ObjectMapper objectMapper ;
244
+ private Set <String > parseList (String s ) {
245
+ return s != null ? StringUtils .commaDelimitedListToSet (s ) : Collections .emptySet ();
246
+ }
253
247
254
- private DefaultRegisteredClientParametersMapper (ObjectMapper objectMapper ) {
255
- this .objectMapper = objectMapper ;
248
+ private Map <String , Object > parseMap (String data ) {
249
+ try {
250
+ return this .objectMapper .readValue (data , new TypeReference <Map <String , Object >>() {});
251
+ } catch (Exception ex ) {
252
+ throw new IllegalArgumentException (ex .getMessage (), ex );
253
+ }
256
254
}
257
255
256
+ }
257
+
258
+ public static class DefaultRegisteredClientParametersMapper implements Function <RegisteredClient , List <SqlParameterValue >> {
259
+
260
+ private ObjectMapper objectMapper = new ObjectMapper ();
261
+
258
262
@ Override
259
263
public List <SqlParameterValue > apply (RegisteredClient registeredClient ) {
260
- try {
261
- List <String > clientAuthenticationMethodNames = new ArrayList <>(registeredClient .getClientAuthenticationMethods ().size ());
262
- for (ClientAuthenticationMethod clientAuthenticationMethod : registeredClient .getClientAuthenticationMethods ()) {
263
- clientAuthenticationMethodNames .add (clientAuthenticationMethod .getValue ());
264
- }
264
+ List <String > clientAuthenticationMethodNames = new ArrayList <>(registeredClient .getClientAuthenticationMethods ().size ());
265
+ for (ClientAuthenticationMethod clientAuthenticationMethod : registeredClient .getClientAuthenticationMethods ()) {
266
+ clientAuthenticationMethodNames .add (clientAuthenticationMethod .getValue ());
267
+ }
265
268
266
- List <String > authorizationGrantTypeNames = new ArrayList <>(registeredClient .getAuthorizationGrantTypes ().size ());
267
- for (AuthorizationGrantType authorizationGrantType : registeredClient .getAuthorizationGrantTypes ()) {
268
- authorizationGrantTypeNames .add (authorizationGrantType .getValue ());
269
- }
269
+ List <String > authorizationGrantTypeNames = new ArrayList <>(registeredClient .getAuthorizationGrantTypes ().size ());
270
+ for (AuthorizationGrantType authorizationGrantType : registeredClient .getAuthorizationGrantTypes ()) {
271
+ authorizationGrantTypeNames .add (authorizationGrantType .getValue ());
272
+ }
273
+
274
+ Instant issuedAt = registeredClient .getClientIdIssuedAt () != null ?
275
+ registeredClient .getClientIdIssuedAt () : Instant .now ();
276
+
277
+ Timestamp clientSecretExpiresAt = registeredClient .getClientSecretExpiresAt () != null ?
278
+ Timestamp .from (registeredClient .getClientSecretExpiresAt ()) : null ;
279
+
280
+ Map <String , Object > clientSettings = new HashMap <>();
281
+ clientSettings .put ("require_proof_key" , registeredClient .getClientSettings ().requireProofKey ());
282
+ clientSettings .put ("require_user_consent" , registeredClient .getClientSettings ().requireUserConsent ());
283
+ String clientSettingsJson = writeMap (clientSettings );
284
+
285
+ Map <String , Object > tokenSettings = new HashMap <>();
286
+ tokenSettings .put ("access_token_ttl" , registeredClient .getTokenSettings ().accessTokenTimeToLive ().toMillis ());
287
+ tokenSettings .put ("reuse_refresh_tokens" , registeredClient .getTokenSettings ().reuseRefreshTokens ());
288
+ tokenSettings .put ("refresh_token_ttl" , registeredClient .getTokenSettings ().refreshTokenTimeToLive ().toMillis ());
289
+ String tokenSettingsJson = writeMap (tokenSettings );
290
+
291
+ return Arrays .asList (
292
+ new SqlParameterValue (Types .VARCHAR , registeredClient .getId ()),
293
+ new SqlParameterValue (Types .VARCHAR , registeredClient .getClientId ()),
294
+ new SqlParameterValue (Types .TIMESTAMP , Timestamp .from (issuedAt )),
295
+ new SqlParameterValue (Types .VARCHAR , registeredClient .getClientSecret ()),
296
+ new SqlParameterValue (Types .TIMESTAMP , clientSecretExpiresAt ),
297
+ new SqlParameterValue (Types .VARCHAR , registeredClient .getClientName ()),
298
+ new SqlParameterValue (Types .VARCHAR , StringUtils .collectionToCommaDelimitedString (clientAuthenticationMethodNames )),
299
+ new SqlParameterValue (Types .VARCHAR , StringUtils .collectionToCommaDelimitedString (authorizationGrantTypeNames )),
300
+ new SqlParameterValue (Types .VARCHAR , StringUtils .collectionToCommaDelimitedString (registeredClient .getRedirectUris ())),
301
+ new SqlParameterValue (Types .VARCHAR , StringUtils .collectionToCommaDelimitedString (registeredClient .getScopes ())),
302
+ new SqlParameterValue (Types .VARCHAR , clientSettingsJson ),
303
+ new SqlParameterValue (Types .VARCHAR , tokenSettingsJson ));
304
+ }
270
305
271
- Instant issuedAt = registeredClient .getClientIdIssuedAt () != null ?
272
- registeredClient .getClientIdIssuedAt () : Instant .now ();
273
-
274
- Timestamp clientSecretExpiresAt = registeredClient .getClientSecretExpiresAt () != null ?
275
- Timestamp .from (registeredClient .getClientSecretExpiresAt ()) : null ;
276
-
277
- Map <String , Object > clientSettings = new HashMap <>();
278
- clientSettings .put ("require_proof_key" , registeredClient .getClientSettings ().requireProofKey ());
279
- clientSettings .put ("require_user_consent" , registeredClient .getClientSettings ().requireUserConsent ());
280
- String clientSettingsJson = this .objectMapper .writeValueAsString (clientSettings );
281
-
282
- Map <String , Object > tokenSettings = new HashMap <>();
283
- tokenSettings .put ("access_token_ttl" , registeredClient .getTokenSettings ().accessTokenTimeToLive ().toMillis ());
284
- tokenSettings .put ("reuse_refresh_tokens" , registeredClient .getTokenSettings ().reuseRefreshTokens ());
285
- tokenSettings .put ("refresh_token_ttl" , registeredClient .getTokenSettings ().refreshTokenTimeToLive ().toMillis ());
286
- String tokenSettingsJson = this .objectMapper .writeValueAsString (tokenSettings );
287
-
288
- return Arrays .asList (
289
- new SqlParameterValue (Types .VARCHAR , registeredClient .getId ()),
290
- new SqlParameterValue (Types .VARCHAR , registeredClient .getClientId ()),
291
- new SqlParameterValue (Types .TIMESTAMP , Timestamp .from (issuedAt )),
292
- new SqlParameterValue (Types .VARCHAR , registeredClient .getClientSecret ()),
293
- new SqlParameterValue (Types .TIMESTAMP , clientSecretExpiresAt ),
294
- new SqlParameterValue (Types .VARCHAR , registeredClient .getClientName ()),
295
- new SqlParameterValue (Types .VARCHAR , StringUtils .collectionToCommaDelimitedString (clientAuthenticationMethodNames )),
296
- new SqlParameterValue (Types .VARCHAR , StringUtils .collectionToCommaDelimitedString (authorizationGrantTypeNames )),
297
- new SqlParameterValue (Types .VARCHAR , StringUtils .collectionToCommaDelimitedString (registeredClient .getRedirectUris ())),
298
- new SqlParameterValue (Types .VARCHAR , StringUtils .collectionToCommaDelimitedString (registeredClient .getScopes ())),
299
- new SqlParameterValue (Types .VARCHAR , clientSettingsJson ),
300
- new SqlParameterValue (Types .VARCHAR , tokenSettingsJson ));
301
- } catch (JsonProcessingException e ) {
302
- throw new IllegalArgumentException (e .getMessage (), e );
306
+ public final void setObjectMapper (ObjectMapper objectMapper ) {
307
+ Assert .notNull (objectMapper , "objectMapper cannot be null" );
308
+ this .objectMapper = objectMapper ;
309
+ }
310
+
311
+ protected final ObjectMapper getObjectMapper () {
312
+ return this .objectMapper ;
313
+ }
314
+
315
+ private String writeMap (Map <String , Object > data ) {
316
+ try {
317
+ return this .objectMapper .writeValueAsString (data );
318
+ } catch (Exception ex ) {
319
+ throw new IllegalArgumentException (ex .getMessage (), ex );
303
320
}
304
321
}
305
322
0 commit comments