Skip to content

Commit 5161712

Browse files
author
Steve Riesenberg
committed
Polish gh-13976
Closes gh-13757
1 parent a6b872d commit 5161712

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/ReactiveRemoteJWKSource.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,23 @@ class ReactiveRemoteJWKSource implements ReactiveJWKSource {
4444
private final AtomicReference<Mono<JWKSet>> cachedJWKSet = new AtomicReference<>(Mono.empty());
4545

4646
/**
47-
* cached url for jwk set.
47+
* The cached JWK set URL.
4848
*/
4949
private final AtomicReference<String> cachedJwkSetUrl = new AtomicReference<>();
5050

5151
private WebClient webClient = WebClient.create();
5252

53-
private Mono<String> jwkSetURLProvider;
53+
private final Mono<String> jwkSetUrlProvider;
5454

5555
ReactiveRemoteJWKSource(String jwkSetURL) {
5656
Assert.hasText(jwkSetURL, "jwkSetURL cannot be empty");
57-
this.cachedJwkSetUrl.set(jwkSetURL);
57+
this.jwkSetUrlProvider = Mono.just(jwkSetURL);
5858
}
5959

60-
ReactiveRemoteJWKSource(Mono<String> jwkSetURLProvider) {
61-
Assert.notNull(jwkSetURLProvider, "jwkSetURLProvider cannot be null");
62-
this.jwkSetURLProvider = jwkSetURLProvider;
60+
ReactiveRemoteJWKSource(Mono<String> jwkSetUrlProvider) {
61+
Assert.notNull(jwkSetUrlProvider, "jwkSetUrlProvider cannot be null");
62+
this.jwkSetUrlProvider = Mono.fromCallable(this.cachedJwkSetUrl::get)
63+
.switchIfEmpty(Mono.defer(() -> jwkSetUrlProvider.doOnNext(this.cachedJwkSetUrl::set)));
6364
}
6465

6566
@Override
@@ -105,10 +106,7 @@ private Mono<List<JWK>> get(JWKSelector jwkSelector, JWKSet jwkSet) {
105106
*/
106107
private Mono<JWKSet> getJWKSet() {
107108
// @formatter:off
108-
return Mono.justOrEmpty(this.cachedJwkSetUrl.get())
109-
.switchIfEmpty(Mono.defer(() -> this.jwkSetURLProvider
110-
.doOnNext(this.cachedJwkSetUrl::set))
111-
)
109+
return this.jwkSetUrlProvider
112110
.flatMap((jwkSetURL) -> this.webClient.get()
113111
.uri(jwkSetURL)
114112
.retrieve()

oauth2/oauth2-jose/src/test/java/org/springframework/security/oauth2/jwt/ReactiveRemoteJWKSourceTests.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,15 +32,16 @@
3232
import org.junit.jupiter.api.extension.ExtendWith;
3333
import org.mockito.Mock;
3434
import org.mockito.junit.jupiter.MockitoExtension;
35-
import org.springframework.web.reactive.function.client.WebClientResponseException;
3635
import reactor.core.publisher.Mono;
3736

37+
import org.springframework.web.reactive.function.client.WebClientResponseException;
38+
3839
import static org.assertj.core.api.Assertions.assertThat;
39-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
40+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4041
import static org.mockito.ArgumentMatchers.any;
4142
import static org.mockito.BDDMockito.given;
42-
import static org.mockito.Mockito.doReturn;
43-
import static org.mockito.Mockito.doThrow;
43+
import static org.mockito.BDDMockito.willReturn;
44+
import static org.mockito.BDDMockito.willThrow;
4445

4546
/**
4647
* @author Rob Winch
@@ -168,14 +169,14 @@ public void getWhenNoMatchAndKeyIdMatchThenEmpty() {
168169
@Test
169170
public void getShouldRecoverAndReturnKeysAfterErrorCase() {
170171
given(this.matcher.matches(any())).willReturn(true);
171-
this.source = new ReactiveRemoteJWKSource(Mono.fromSupplier(mockStringSupplier));
172-
doThrow(WebClientResponseException.ServiceUnavailable.class).when(this.mockStringSupplier).get();
172+
this.source = new ReactiveRemoteJWKSource(Mono.fromSupplier(this.mockStringSupplier));
173+
willThrow(WebClientResponseException.ServiceUnavailable.class).given(this.mockStringSupplier).get();
173174
// first case: id provider has error state
174-
assertThatThrownBy(() -> this.source.get(this.selector).block())
175-
.isExactlyInstanceOf(WebClientResponseException.ServiceUnavailable.class);
175+
assertThatExceptionOfType(WebClientResponseException.ServiceUnavailable.class)
176+
.isThrownBy(() -> this.source.get(this.selector).block());
176177
// second case: id provider is healthy again
177-
doReturn(this.server.url("/").toString()).when(this.mockStringSupplier).get();
178-
var actual = this.source.get(this.selector).block();
178+
willReturn(this.server.url("/").toString()).given(this.mockStringSupplier).get();
179+
List<JWK> actual = this.source.get(this.selector).block();
179180
assertThat(actual).isNotEmpty();
180181
}
181182

0 commit comments

Comments
 (0)