15
15
*/
16
16
package org .springframework .security .oauth2 .server .authorization ;
17
17
18
+ import java .sql .ResultSet ;
19
+ import java .sql .SQLException ;
20
+ import java .sql .Types ;
21
+ import java .util .List ;
22
+
18
23
import org .junit .After ;
19
24
import org .junit .Before ;
20
25
import org .junit .Test ;
26
+
27
+ import org .springframework .dao .DataRetrievalFailureException ;
28
+ import org .springframework .jdbc .core .ArgumentPreparedStatementSetter ;
21
29
import org .springframework .jdbc .core .JdbcOperations ;
22
30
import org .springframework .jdbc .core .JdbcTemplate ;
31
+ import org .springframework .jdbc .core .PreparedStatementSetter ;
32
+ import org .springframework .jdbc .core .SqlParameterValue ;
23
33
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabase ;
24
34
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
25
35
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseType ;
26
36
import org .springframework .security .core .authority .SimpleGrantedAuthority ;
27
37
import org .springframework .security .oauth2 .server .authorization .client .RegisteredClient ;
28
38
import org .springframework .security .oauth2 .server .authorization .client .RegisteredClientRepository ;
29
39
import org .springframework .security .oauth2 .server .authorization .client .TestRegisteredClients ;
40
+ import org .springframework .util .StringUtils ;
30
41
31
42
import static org .assertj .core .api .Assertions .assertThat ;
32
43
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
47
58
public class JdbcOAuth2AuthorizationConsentServiceTests {
48
59
49
60
private static final String OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql" ;
61
+ private static final String CUSTOM_OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/custom-oauth2-authorization-consent-schema.sql" ;
50
62
private static final String PRINCIPAL_NAME = "principal-name" ;
51
63
private static final RegisteredClient REGISTERED_CLIENT = TestRegisteredClients .registeredClient ().build ();
52
64
@@ -200,6 +212,23 @@ public void findByIdWhenAuthorizationConsentDoesNotExistThenNull() {
200
212
assertThat (this .authorizationConsentService .findById (REGISTERED_CLIENT .getId (), "unknown-user" )).isNull ();
201
213
}
202
214
215
+ @ Test
216
+ public void tableDefinitionWhenCustomThenAbleToOverride () {
217
+ when (this .registeredClientRepository .findById (eq (REGISTERED_CLIENT .getId ())))
218
+ .thenReturn (REGISTERED_CLIENT );
219
+
220
+ EmbeddedDatabase db = createDb (CUSTOM_OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE );
221
+ OAuth2AuthorizationConsentService authorizationConsentService =
222
+ new CustomJdbcOAuth2AuthorizationConsentService (new JdbcTemplate (db ), this .registeredClientRepository );
223
+ authorizationConsentService .save (AUTHORIZATION_CONSENT );
224
+ OAuth2AuthorizationConsent foundAuthorizationConsent1 = authorizationConsentService .findById (AUTHORIZATION_CONSENT .getRegisteredClientId (), AUTHORIZATION_CONSENT .getPrincipalName ());
225
+ assertThat (foundAuthorizationConsent1 ).isEqualTo (AUTHORIZATION_CONSENT );
226
+ authorizationConsentService .remove (AUTHORIZATION_CONSENT );
227
+ OAuth2AuthorizationConsent foundAuthorizationConsent2 = authorizationConsentService .findById (REGISTERED_CLIENT .getClientId (), AUTHORIZATION_CONSENT .getPrincipalName ());
228
+ assertThat (foundAuthorizationConsent2 ).isNull ();
229
+ db .shutdown ();
230
+ }
231
+
203
232
@ Before
204
233
public void setUp () {
205
234
this .db = createDb ();
@@ -216,6 +245,7 @@ public void tearDown() {
216
245
private static EmbeddedDatabase createDb () {
217
246
return createDb (OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE );
218
247
}
248
+
219
249
private static EmbeddedDatabase createDb (String schema ) {
220
250
// @formatter:off
221
251
return new EmbeddedDatabaseBuilder ()
@@ -226,4 +256,96 @@ private static EmbeddedDatabase createDb(String schema) {
226
256
.build ();
227
257
// @formatter:on
228
258
}
259
+
260
+ private static final class CustomJdbcOAuth2AuthorizationConsentService extends JdbcOAuth2AuthorizationConsentService {
261
+
262
+ // @formatter:off
263
+ private static final String COLUMN_NAMES = "registeredClientId, "
264
+ + "principalName, "
265
+ + "authorities" ;
266
+ // @formatter:on
267
+
268
+ private static final String TABLE_NAME = "oauth2AuthorizationConsent" ;
269
+
270
+ private static final String PK_FILTER = "registeredClientId = ? AND principalName = ?" ;
271
+
272
+ // @formatter:off
273
+ private static final String LOAD_AUTHORIZATION_CONSENT_SQL = "SELECT " + COLUMN_NAMES
274
+ + " FROM " + TABLE_NAME
275
+ + " WHERE " + PK_FILTER ;
276
+ // @formatter:on
277
+
278
+ // @formatter:off
279
+ private static final String SAVE_AUTHORIZATION_CONSENT_SQL = "INSERT INTO " + TABLE_NAME
280
+ + " (" + COLUMN_NAMES + ") VALUES (?, ?, ?)" ;
281
+ // @formatter:on
282
+
283
+ private static final String REMOVE_AUTHORIZATION_CONSENT_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + PK_FILTER ;
284
+
285
+ CustomJdbcOAuth2AuthorizationConsentService (JdbcOperations jdbcOperations , RegisteredClientRepository registeredClientRepository ) {
286
+ super (jdbcOperations , registeredClientRepository );
287
+ setAuthorizationConsentRowMapper (new CustomOAuth2AuthorizationConsentRowMapper (registeredClientRepository ));
288
+ }
289
+
290
+ @ Override
291
+ public void save (OAuth2AuthorizationConsent authorizationConsent ) {
292
+ List <SqlParameterValue > parameters = getAuthorizationConsentParametersMapper ().apply (authorizationConsent );
293
+ PreparedStatementSetter pss = new ArgumentPreparedStatementSetter (parameters .toArray ());
294
+ getJdbcOperations ().update (SAVE_AUTHORIZATION_CONSENT_SQL , pss );
295
+ }
296
+
297
+ @ Override
298
+ public void remove (OAuth2AuthorizationConsent authorizationConsent ) {
299
+ SqlParameterValue [] parameters = new SqlParameterValue [] {
300
+ new SqlParameterValue (Types .VARCHAR , authorizationConsent .getRegisteredClientId ()),
301
+ new SqlParameterValue (Types .VARCHAR , authorizationConsent .getPrincipalName ())
302
+ };
303
+ PreparedStatementSetter pss = new ArgumentPreparedStatementSetter (parameters );
304
+ getJdbcOperations ().update (REMOVE_AUTHORIZATION_CONSENT_SQL , pss );
305
+ }
306
+
307
+ @ Override
308
+ public OAuth2AuthorizationConsent findById (String registeredClientId , String principalName ) {
309
+ SqlParameterValue [] parameters = new SqlParameterValue [] {
310
+ new SqlParameterValue (Types .VARCHAR , registeredClientId ),
311
+ new SqlParameterValue (Types .VARCHAR , principalName )};
312
+ PreparedStatementSetter pss = new ArgumentPreparedStatementSetter (parameters );
313
+ List <OAuth2AuthorizationConsent > result = getJdbcOperations ().query (LOAD_AUTHORIZATION_CONSENT_SQL , pss ,
314
+ getAuthorizationConsentRowMapper ());
315
+ return !result .isEmpty () ? result .get (0 ) : null ;
316
+ }
317
+
318
+ private static final class CustomOAuth2AuthorizationConsentRowMapper extends JdbcOAuth2AuthorizationConsentService .OAuth2AuthorizationConsentRowMapper {
319
+
320
+ CustomOAuth2AuthorizationConsentRowMapper (RegisteredClientRepository registeredClientRepository ) {
321
+ super (registeredClientRepository );
322
+ }
323
+
324
+ @ Override
325
+ public OAuth2AuthorizationConsent mapRow (ResultSet rs , int rowNum ) throws SQLException {
326
+ String registeredClientId = rs .getString ("registeredClientId" );
327
+
328
+ RegisteredClient registeredClient = getRegisteredClientRepository ()
329
+ .findById (registeredClientId );
330
+ if (registeredClient == null ) {
331
+ throw new DataRetrievalFailureException (
332
+ "The RegisteredClient with id '" + registeredClientId + "' was not found in the RegisteredClientRepository." );
333
+ }
334
+
335
+ String principalName = rs .getString ("principalName" );
336
+
337
+ OAuth2AuthorizationConsent .Builder builder = OAuth2AuthorizationConsent .withId (registeredClientId , principalName );
338
+ String authorizationConsentAuthorities = rs .getString ("authorities" );
339
+ if (authorizationConsentAuthorities != null ) {
340
+ for (String authority : StringUtils .commaDelimitedListToSet (authorizationConsentAuthorities )) {
341
+ builder .authority (new SimpleGrantedAuthority (authority ));
342
+ }
343
+ }
344
+ return builder .build ();
345
+ }
346
+
347
+ }
348
+
349
+ }
350
+
229
351
}
0 commit comments