29
29
import com .nimbusds .jose .jwk .JWKSet ;
30
30
import com .nimbusds .jose .jwk .source .JWKSource ;
31
31
import com .nimbusds .jose .proc .SecurityContext ;
32
+ import org .junit .After ;
33
+ import org .junit .AfterClass ;
32
34
import org .junit .BeforeClass ;
33
35
import org .junit .Rule ;
34
36
import org .junit .Test ;
39
41
import org .springframework .http .HttpHeaders ;
40
42
import org .springframework .http .HttpStatus ;
41
43
import org .springframework .http .converter .HttpMessageConverter ;
44
+ import org .springframework .jdbc .core .JdbcOperations ;
45
+ import org .springframework .jdbc .core .JdbcTemplate ;
46
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabase ;
47
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
48
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseType ;
42
49
import org .springframework .mock .http .client .MockClientHttpResponse ;
43
50
import org .springframework .mock .web .MockHttpServletResponse ;
51
+ import org .springframework .security .authentication .TestingAuthenticationToken ;
44
52
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
45
53
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
46
54
import org .springframework .security .config .annotation .web .configuration .OAuth2AuthorizationServerConfiguration ;
61
69
import org .springframework .security .oauth2 .jwt .JwtDecoder ;
62
70
import org .springframework .security .oauth2 .jwt .JwtEncoder ;
63
71
import org .springframework .security .oauth2 .jwt .NimbusJwsEncoder ;
64
- import org .springframework .security .oauth2 .server .authorization .InMemoryOAuth2AuthorizationConsentService ;
65
- import org .springframework .security .oauth2 .server .authorization .InMemoryOAuth2AuthorizationService ;
72
+ import org .springframework .security .oauth2 .server .authorization .JdbcOAuth2AuthorizationConsentService ;
73
+ import org .springframework .security .oauth2 .server .authorization .JdbcOAuth2AuthorizationService ;
66
74
import org .springframework .security .oauth2 .server .authorization .JwtEncodingContext ;
67
75
import org .springframework .security .oauth2 .server .authorization .OAuth2Authorization ;
68
76
import org .springframework .security .oauth2 .server .authorization .OAuth2AuthorizationCode ;
69
77
import org .springframework .security .oauth2 .server .authorization .OAuth2AuthorizationConsentService ;
70
78
import org .springframework .security .oauth2 .server .authorization .OAuth2AuthorizationService ;
71
79
import org .springframework .security .oauth2 .server .authorization .OAuth2TokenCustomizer ;
72
80
import org .springframework .security .oauth2 .server .authorization .TestOAuth2Authorizations ;
73
- import org .springframework .security .oauth2 .server .authorization .client .InMemoryRegisteredClientRepository ;
81
+ import org .springframework .security .oauth2 .server .authorization .client .JdbcRegisteredClientRepository ;
74
82
import org .springframework .security .oauth2 .server .authorization .client .RegisteredClient ;
75
83
import org .springframework .security .oauth2 .server .authorization .client .RegisteredClientRepository ;
76
84
import org .springframework .security .oauth2 .server .authorization .client .TestRegisteredClients ;
77
85
import org .springframework .security .oauth2 .server .authorization .config .ProviderSettings ;
86
+ import org .springframework .security .oauth2 .server .authorization .jackson2 .TestingAuthenticationTokenMixin ;
78
87
import org .springframework .security .oauth2 .server .authorization .web .OAuth2AuthorizationEndpointFilter ;
79
88
import org .springframework .security .oauth2 .server .authorization .web .OAuth2TokenEndpointFilter ;
80
89
import org .springframework .security .web .SecurityFilterChain ;
@@ -111,6 +120,7 @@ public class OAuth2AuthorizationCodeGrantTests {
111
120
private static final OAuth2TokenType AUTHORIZATION_CODE_TOKEN_TYPE = new OAuth2TokenType (OAuth2ParameterNames .CODE );
112
121
private static final OAuth2TokenType STATE_TOKEN_TYPE = new OAuth2TokenType (OAuth2ParameterNames .STATE );
113
122
123
+ private static EmbeddedDatabase db ;
114
124
private static JWKSource <SecurityContext > jwkSource ;
115
125
private static NimbusJwsEncoder jwtEncoder ;
116
126
private static ProviderSettings providerSettings ;
@@ -124,6 +134,9 @@ public class OAuth2AuthorizationCodeGrantTests {
124
134
@ Autowired
125
135
private MockMvc mvc ;
126
136
137
+ @ Autowired
138
+ private JdbcOperations jdbcOperations ;
139
+
127
140
@ Autowired
128
141
private RegisteredClientRepository registeredClientRepository ;
129
142
@@ -141,6 +154,26 @@ public static void init() {
141
154
providerSettings = new ProviderSettings ()
142
155
.authorizationEndpoint ("/test/authorize" )
143
156
.tokenEndpoint ("/test/token" );
157
+ db = new EmbeddedDatabaseBuilder ()
158
+ .generateUniqueName (true )
159
+ .setType (EmbeddedDatabaseType .HSQL )
160
+ .setScriptEncoding ("UTF-8" )
161
+ .addScript ("org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql" )
162
+ .addScript ("org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql" )
163
+ .addScript ("org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql" )
164
+ .build ();
165
+ }
166
+
167
+ @ After
168
+ public void tearDown () {
169
+ jdbcOperations .update ("truncate table oauth2_authorization" );
170
+ jdbcOperations .update ("truncate table oauth2_authorization_consent" );
171
+ jdbcOperations .update ("truncate table oauth2_registered_client" );
172
+ }
173
+
174
+ @ AfterClass
175
+ public static void destroy () {
176
+ db .shutdown ();
144
177
}
145
178
146
179
@ Test
@@ -485,25 +518,26 @@ private String extractParameterFromRedirectUri(String redirectUri, String param)
485
518
static class AuthorizationServerConfiguration {
486
519
487
520
@ Bean
488
- OAuth2AuthorizationService authorizationService () {
489
- return new InMemoryOAuth2AuthorizationService ();
521
+ OAuth2AuthorizationService authorizationService (JdbcOperations jdbcOperations , RegisteredClientRepository registeredClientRepository ) {
522
+ JdbcOAuth2AuthorizationService authorizationService = new JdbcOAuth2AuthorizationService (jdbcOperations , registeredClientRepository );
523
+ authorizationService .setAuthorizationRowMapper (new RowMapper (registeredClientRepository ));
524
+ authorizationService .setAuthorizationParametersMapper (new ParametersMapper ());
525
+ return authorizationService ;
526
+ }
527
+
528
+ @ Bean
529
+ OAuth2AuthorizationConsentService authorizationConsentService (JdbcOperations jdbcOperations , RegisteredClientRepository registeredClientRepository ) {
530
+ return new JdbcOAuth2AuthorizationConsentService (jdbcOperations , registeredClientRepository );
490
531
}
491
532
492
533
@ Bean
493
- OAuth2AuthorizationConsentService authorizationConsentService ( ) {
494
- return new InMemoryOAuth2AuthorizationConsentService ( );
534
+ RegisteredClientRepository registeredClientRepository ( JdbcOperations jdbcOperations ) {
535
+ return new JdbcRegisteredClientRepository ( jdbcOperations );
495
536
}
496
537
497
538
@ Bean
498
- RegisteredClientRepository registeredClientRepository () {
499
- // @formatter:off
500
- RegisteredClient dummyClient = TestRegisteredClients .registeredClient ()
501
- .id ("dummy-client" )
502
- .clientId ("dummy-client" )
503
- .clientSecret ("dummy-secret" )
504
- .build ();
505
- // @formatter:on
506
- return new InMemoryRegisteredClientRepository (dummyClient );
539
+ JdbcOperations jdbcOperations () {
540
+ return new JdbcTemplate (db );
507
541
}
508
542
509
543
@ Bean
@@ -530,6 +564,24 @@ PasswordEncoder passwordEncoder() {
530
564
return NoOpPasswordEncoder .getInstance ();
531
565
}
532
566
567
+ static class RowMapper extends JdbcOAuth2AuthorizationService .OAuth2AuthorizationRowMapper {
568
+
569
+ RowMapper (RegisteredClientRepository registeredClientRepository ) {
570
+ super (registeredClientRepository );
571
+ getObjectMapper ().addMixIn (TestingAuthenticationToken .class , TestingAuthenticationTokenMixin .class );
572
+ }
573
+
574
+ }
575
+
576
+ static class ParametersMapper extends JdbcOAuth2AuthorizationService .OAuth2AuthorizationParametersMapper {
577
+
578
+ ParametersMapper () {
579
+ super ();
580
+ getObjectMapper ().addMixIn (TestingAuthenticationToken .class , TestingAuthenticationTokenMixin .class );
581
+ }
582
+
583
+ }
584
+
533
585
}
534
586
535
587
@ EnableWebSecurity
0 commit comments