37
37
import org .springframework .security .core .context .ReactiveSecurityContextHolder ;
38
38
import org .springframework .security .oauth2 .client .OAuth2AuthorizedClient ;
39
39
import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
40
+ import org .springframework .security .oauth2 .client .registration .ReactiveClientRegistrationRepository ;
40
41
import org .springframework .security .oauth2 .client .registration .TestClientRegistrations ;
41
42
import org .springframework .security .oauth2 .client .web .server .ServerOAuth2AuthorizedClientRepository ;
42
43
import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
71
72
@ RunWith (MockitoJUnitRunner .class )
72
73
public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
73
74
@ Mock
74
- private ServerOAuth2AuthorizedClientRepository auth2AuthorizedClientRepository ;
75
+ private ServerOAuth2AuthorizedClientRepository authorizedClientRepository ;
76
+
77
+ @ Mock
78
+ private ReactiveClientRegistrationRepository clientRegistrationRepository ;
75
79
76
80
private ServerOAuth2AuthorizedClientExchangeFilterFunction function = new ServerOAuth2AuthorizedClientExchangeFilterFunction ();
77
81
@@ -125,7 +129,7 @@ public void filterWhenExistingAuthorizationThenSingleAuthorizationHeader() {
125
129
126
130
@ Test
127
131
public void filterWhenRefreshRequiredThenRefresh () {
128
- when (this .auth2AuthorizedClientRepository .saveAuthorizedClient (any (), any (), any ())).thenReturn (Mono .empty ());
132
+ when (this .authorizedClientRepository .saveAuthorizedClient (any (), any (), any ())).thenReturn (Mono .empty ());
129
133
OAuth2AccessTokenResponse response = OAuth2AccessTokenResponse .withToken ("token-1" )
130
134
.tokenType (OAuth2AccessToken .TokenType .BEARER )
131
135
.expiresIn (3600 )
@@ -140,7 +144,7 @@ public void filterWhenRefreshRequiredThenRefresh() {
140
144
this .accessToken .getTokenValue (),
141
145
issuedAt ,
142
146
accessTokenExpiresAt );
143
- this .function = new ServerOAuth2AuthorizedClientExchangeFilterFunction (this .auth2AuthorizedClientRepository );
147
+ this .function = new ServerOAuth2AuthorizedClientExchangeFilterFunction (this .clientRegistrationRepository , this . authorizedClientRepository );
144
148
145
149
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken ("refresh-token" , issuedAt , refreshTokenExpiresAt );
146
150
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient (this .registration ,
@@ -154,7 +158,7 @@ public void filterWhenRefreshRequiredThenRefresh() {
154
158
.subscriberContext (ReactiveSecurityContextHolder .withAuthentication (authentication ))
155
159
.block ();
156
160
157
- verify (this .auth2AuthorizedClientRepository ).saveAuthorizedClient (any (), eq (authentication ), any ());
161
+ verify (this .authorizedClientRepository ).saveAuthorizedClient (any (), eq (authentication ), any ());
158
162
159
163
List <ClientRequest > requests = this .exchange .getRequests ();
160
164
assertThat (requests ).hasSize (2 );
@@ -174,7 +178,7 @@ public void filterWhenRefreshRequiredThenRefresh() {
174
178
175
179
@ Test
176
180
public void filterWhenRefreshRequiredAndEmptyReactiveSecurityContextThenSaved () {
177
- when (this .auth2AuthorizedClientRepository .saveAuthorizedClient (any (), any (), any ())).thenReturn (Mono .empty ());
181
+ when (this .authorizedClientRepository .saveAuthorizedClient (any (), any (), any ())).thenReturn (Mono .empty ());
178
182
OAuth2AccessTokenResponse response = OAuth2AccessTokenResponse .withToken ("token-1" )
179
183
.tokenType (OAuth2AccessToken .TokenType .BEARER )
180
184
.expiresIn (3600 )
@@ -189,7 +193,7 @@ public void filterWhenRefreshRequiredAndEmptyReactiveSecurityContextThenSaved()
189
193
this .accessToken .getTokenValue (),
190
194
issuedAt ,
191
195
accessTokenExpiresAt );
192
- this .function = new ServerOAuth2AuthorizedClientExchangeFilterFunction (this .auth2AuthorizedClientRepository );
196
+ this .function = new ServerOAuth2AuthorizedClientExchangeFilterFunction (this .clientRegistrationRepository , this . authorizedClientRepository );
193
197
194
198
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken ("refresh-token" , issuedAt , refreshTokenExpiresAt );
195
199
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient (this .registration ,
@@ -201,7 +205,7 @@ public void filterWhenRefreshRequiredAndEmptyReactiveSecurityContextThenSaved()
201
205
this .function .filter (request , this .exchange )
202
206
.block ();
203
207
204
- verify (this .auth2AuthorizedClientRepository ).saveAuthorizedClient (any (), any (), any ());
208
+ verify (this .authorizedClientRepository ).saveAuthorizedClient (any (), any (), any ());
205
209
206
210
List <ClientRequest > requests = this .exchange .getRequests ();
207
211
assertThat (requests ).hasSize (2 );
@@ -221,7 +225,7 @@ public void filterWhenRefreshRequiredAndEmptyReactiveSecurityContextThenSaved()
221
225
222
226
@ Test
223
227
public void filterWhenRefreshTokenNullThenShouldRefreshFalse () {
224
- this .function = new ServerOAuth2AuthorizedClientExchangeFilterFunction (this .auth2AuthorizedClientRepository );
228
+ this .function = new ServerOAuth2AuthorizedClientExchangeFilterFunction (this .clientRegistrationRepository , this . authorizedClientRepository );
225
229
226
230
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient (this .registration ,
227
231
"principalName" , this .accessToken );
@@ -243,7 +247,7 @@ public void filterWhenRefreshTokenNullThenShouldRefreshFalse() {
243
247
244
248
@ Test
245
249
public void filterWhenNotExpiredThenShouldRefreshFalse () {
246
- this .function = new ServerOAuth2AuthorizedClientExchangeFilterFunction (this .auth2AuthorizedClientRepository );
250
+ this .function = new ServerOAuth2AuthorizedClientExchangeFilterFunction (this .clientRegistrationRepository , this . authorizedClientRepository );
247
251
248
252
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken ("refresh-token" , this .accessToken .getIssuedAt (), this .accessToken .getExpiresAt ());
249
253
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient (this .registration ,
@@ -266,12 +270,13 @@ public void filterWhenNotExpiredThenShouldRefreshFalse() {
266
270
267
271
@ Test
268
272
public void filterWhenClientRegistrationIdThenAuthorizedClientResolved () {
269
- this .function = new ServerOAuth2AuthorizedClientExchangeFilterFunction (this .auth2AuthorizedClientRepository );
273
+ this .function = new ServerOAuth2AuthorizedClientExchangeFilterFunction (this .clientRegistrationRepository , this . authorizedClientRepository );
270
274
271
275
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken ("refresh-token" , this .accessToken .getIssuedAt (), this .accessToken .getExpiresAt ());
272
276
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient (this .registration ,
273
277
"principalName" , this .accessToken , refreshToken );
274
- when (this .auth2AuthorizedClientRepository .loadAuthorizedClient (any (), any (), any ())).thenReturn (Mono .just (authorizedClient ));
278
+ when (this .authorizedClientRepository .loadAuthorizedClient (any (), any (), any ())).thenReturn (Mono .just (authorizedClient ));
279
+ when (this .clientRegistrationRepository .findByRegistrationId (any ())).thenReturn (Mono .just (this .registration ));
275
280
ClientRequest request = ClientRequest .create (GET , URI .create ("https://example.com" ))
276
281
.attributes (clientRegistrationId (this .registration .getRegistrationId ()))
277
282
.build ();
0 commit comments