Skip to content

Commit 8584b12

Browse files
philsttrjgrandja
authored andcommitted
Make saveAuthorizedClient save the authorized client
Previously, saveAuthorizedClient never actually saved the authorized client, because it ignored the Mono<Void> returned from authorizedClientRepository.saveAuthorizedClient. Now, it does not ignore the Mono<Void> returned from authorizedClientRepository.saveAuthorizedClient, and includes it in the stream, and therefore it will properly save the authorized client. Fixes gh-7546
1 parent d26f40f commit 8584b12

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultReactiveOAuth2AuthorizedClientManager.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@ private Mono<OAuth2AuthorizedClient> loadAuthorizedClient(String clientRegistrat
105105
private Mono<OAuth2AuthorizedClient> saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal, ServerWebExchange serverWebExchange) {
106106
return Mono.justOrEmpty(serverWebExchange)
107107
.switchIfEmpty(Mono.defer(() -> currentServerWebExchange()))
108-
.map(exchange -> {
109-
this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, principal, exchange);
110-
return authorizedClient;
111-
})
108+
.flatMap(exchange -> this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, principal, exchange)
109+
.thenReturn(authorizedClient))
112110
.defaultIfEmpty(authorizedClient);
113111
}
114112

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/web/DefaultReactiveOAuth2AuthorizedClientManagerTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
3737
import org.springframework.web.server.ServerWebExchange;
3838
import reactor.core.publisher.Mono;
39+
import reactor.test.publisher.PublisherProbe;
3940
import reactor.util.context.Context;
4041

4142
import java.util.Collections;
@@ -64,6 +65,7 @@ public class DefaultReactiveOAuth2AuthorizedClientManagerTests {
6465
private MockServerWebExchange serverWebExchange;
6566
private Context context;
6667
private ArgumentCaptor<OAuth2AuthorizationContext> authorizationContextCaptor;
68+
private PublisherProbe<Void> saveAuthorizedClientProbe;
6769

6870
@SuppressWarnings("unchecked")
6971
@Before
@@ -74,8 +76,9 @@ public void setup() {
7476
this.authorizedClientRepository = mock(ServerOAuth2AuthorizedClientRepository.class);
7577
when(this.authorizedClientRepository.loadAuthorizedClient(
7678
anyString(), any(Authentication.class), any(ServerWebExchange.class))).thenReturn(Mono.empty());
79+
this.saveAuthorizedClientProbe = PublisherProbe.empty();
7780
when(this.authorizedClientRepository.saveAuthorizedClient(
78-
any(OAuth2AuthorizedClient.class), any(Authentication.class), any(ServerWebExchange.class))).thenReturn(Mono.empty());
81+
any(OAuth2AuthorizedClient.class), any(Authentication.class), any(ServerWebExchange.class))).thenReturn(this.saveAuthorizedClientProbe.mono());
7982
this.authorizedClientProvider = mock(ReactiveOAuth2AuthorizedClientProvider.class);
8083
when(this.authorizedClientProvider.authorize(any(OAuth2AuthorizationContext.class))).thenReturn(Mono.empty());
8184
this.contextAttributesMapper = mock(Function.class);
@@ -187,6 +190,7 @@ public void authorizeWhenNotAuthorizedAndSupportedProviderThenAuthorized() {
187190
assertThat(authorizedClient).isSameAs(this.authorizedClient);
188191
verify(this.authorizedClientRepository).saveAuthorizedClient(
189192
eq(this.authorizedClient), eq(this.principal), eq(this.serverWebExchange));
193+
this.saveAuthorizedClientProbe.assertWasSubscribed();
190194
}
191195

192196
@Test
@@ -245,6 +249,7 @@ public void authorizeWhenAuthorizedAndSupportedProviderThenReauthorized() {
245249
assertThat(authorizedClient).isSameAs(reauthorizedClient);
246250
verify(this.authorizedClientRepository).saveAuthorizedClient(
247251
eq(reauthorizedClient), eq(this.principal), eq(this.serverWebExchange));
252+
this.saveAuthorizedClientProbe.assertWasSubscribed();
248253
}
249254

250255
@Test
@@ -337,6 +342,7 @@ public void reauthorizeWhenSupportedProviderThenReauthorized() {
337342
assertThat(authorizedClient).isSameAs(reauthorizedClient);
338343
verify(this.authorizedClientRepository).saveAuthorizedClient(
339344
eq(reauthorizedClient), eq(this.principal), eq(this.serverWebExchange));
345+
this.saveAuthorizedClientProbe.assertWasSubscribed();
340346
}
341347

342348
@Test

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public void setup() {
140140
this.clientRegistrationRepository, this.authorizedClientRepository);
141141
this.authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
142142
this.function = new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
143+
when(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).thenReturn(Mono.empty());
143144
}
144145

145146
@Test

0 commit comments

Comments
 (0)