Skip to content

Commit 1a65abd

Browse files
committed
Add defaultOAuth2AuthorizedClient flag
Fixes: gh-5619
1 parent cecbc21 commit 1a65abd

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServletOAuth2AuthorizedClientExchangeFilterFunction.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,25 @@ public final class ServletOAuth2AuthorizedClientExchangeFilterFunction implement
109109

110110
private OAuth2AuthorizedClientRepository authorizedClientRepository;
111111

112+
private boolean defaultOAuth2AuthorizedClient;
113+
112114
public ServletOAuth2AuthorizedClientExchangeFilterFunction() {}
113115

114116
public ServletOAuth2AuthorizedClientExchangeFilterFunction(OAuth2AuthorizedClientRepository authorizedClientRepository) {
115117
this.authorizedClientRepository = authorizedClientRepository;
116118
}
117119

120+
/**
121+
* If true, a default {@link OAuth2AuthorizedClient} can be discovered from the current Authentication. It is
122+
* recommended to be cautious with this feature since all HTTP requests will receive the access token if it can be
123+
* resolved from the current Authentication.
124+
* @param defaultOAuth2AuthorizedClient true if a default {@link OAuth2AuthorizedClient} should be used, else false.
125+
* Default is false.
126+
*/
127+
public void setDefaultOAuth2AuthorizedClient(boolean defaultOAuth2AuthorizedClient) {
128+
this.defaultOAuth2AuthorizedClient = defaultOAuth2AuthorizedClient;
129+
}
130+
118131
/**
119132
* Configures the builder with {@link #defaultRequest()} and adds this as a {@link ExchangeFilterFunction}
120133
* @return the {@link Consumer} to configure the builder
@@ -251,13 +264,16 @@ private void populateDefaultAuthentication(Map<String, Object> attrs) {
251264
}
252265

253266
private void populateDefaultOAuth2AuthorizedClient(Map<String, Object> attrs) {
254-
if (this.authorizedClientRepository == null || attrs.containsKey(OAUTH2_AUTHORIZED_CLIENT_ATTR_NAME)) {
267+
if (this.authorizedClientRepository == null
268+
|| attrs.containsKey(OAUTH2_AUTHORIZED_CLIENT_ATTR_NAME)) {
255269
return;
256270
}
257271

258272
Authentication authentication = getAuthentication(attrs);
259273
String clientRegistrationId = getClientRegistrationId(attrs);
260-
if (clientRegistrationId == null && authentication instanceof OAuth2AuthenticationToken) {
274+
if (clientRegistrationId == null
275+
&& this.defaultOAuth2AuthorizedClient
276+
&& authentication instanceof OAuth2AuthenticationToken) {
261277
clientRegistrationId = ((OAuth2AuthenticationToken) authentication).getAuthorizedClientRegistrationId();
262278
}
263279
if (clientRegistrationId != null) {

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServletOAuth2AuthorizedClientExchangeFilterFunctionTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,9 @@ public void defaultRequestOAuth2AuthorizedClientWhenRepositoryNullThenOAuth2Auth
207207
}
208208

209209
@Test
210-
public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationAndClientRegistrationIdNullThenOAuth2AuthorizedClient() {
210+
public void defaultRequestOAuth2AuthorizedClientWhenDefaultTrueAndAuthenticationAndClientRegistrationIdNullThenOAuth2AuthorizedClient() {
211211
this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository);
212+
this.function.setDefaultOAuth2AuthorizedClient(true);
212213
OAuth2User user = mock(OAuth2User.class);
213214
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
214215
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(user, authorities, "id");
@@ -223,6 +224,19 @@ public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationAndClientRegis
223224
verify(this.authorizedClientRepository).loadAuthorizedClient(eq(token.getAuthorizedClientRegistrationId()), any(), any());
224225
}
225226

227+
@Test
228+
public void defaultRequestOAuth2AuthorizedClientWhenDefaultFalseAndAuthenticationAndClientRegistrationIdNullThenOAuth2AuthorizedClient() {
229+
this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository);
230+
OAuth2User user = mock(OAuth2User.class);
231+
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
232+
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(user, authorities, "id");
233+
authentication(token).accept(this.result);
234+
235+
Map<String, Object> attrs = getDefaultRequestAttributes();
236+
237+
assertThat(getOAuth2AuthorizedClient(attrs)).isNull();
238+
}
239+
226240
@Test
227241
public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationAndClientRegistrationIdThenIdIsExplicit() {
228242
this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository);

0 commit comments

Comments
 (0)