Skip to content

Commit 36ae1fe

Browse files
committed
Polish oauth2-resource-server format
Issue gh-8945
1 parent d5ae433 commit 36ae1fe

File tree

30 files changed

+674
-202
lines changed

30 files changed

+674
-202
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/BearerTokenError.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,38 @@ public String getScope() {
8787
}
8888

8989
private static boolean isDescriptionValid(String description) {
90-
return description == null || description.chars().allMatch((c) -> withinTheRangeOf(c, 0x20, 0x21)
91-
|| withinTheRangeOf(c, 0x23, 0x5B) || withinTheRangeOf(c, 0x5D, 0x7E));
90+
// @formatter:off
91+
return description == null || description.chars().allMatch((c) ->
92+
withinTheRangeOf(c, 0x20, 0x21) ||
93+
withinTheRangeOf(c, 0x23, 0x5B) ||
94+
withinTheRangeOf(c, 0x5D, 0x7E));
95+
// @formatter:on
9296
}
9397

9498
private static boolean isErrorCodeValid(String errorCode) {
95-
return errorCode.chars().allMatch((c) -> withinTheRangeOf(c, 0x20, 0x21) || withinTheRangeOf(c, 0x23, 0x5B)
96-
|| withinTheRangeOf(c, 0x5D, 0x7E));
99+
// @formatter:off
100+
return errorCode.chars().allMatch((c) ->
101+
withinTheRangeOf(c, 0x20, 0x21) ||
102+
withinTheRangeOf(c, 0x23, 0x5B) ||
103+
withinTheRangeOf(c, 0x5D, 0x7E));
104+
// @formatter:on
97105
}
98106

99107
private static boolean isErrorUriValid(String errorUri) {
100108
return errorUri == null || errorUri.chars()
101-
.allMatch((c) -> c == 0x21 || withinTheRangeOf(c, 0x23, 0x5B) || withinTheRangeOf(c, 0x5D, 0x7E));
109+
.allMatch((c) ->
110+
c == 0x21 ||
111+
withinTheRangeOf(c, 0x23, 0x5B) ||
112+
withinTheRangeOf(c, 0x5D, 0x7E));
102113
}
103114

104115
private static boolean isScopeValid(String scope) {
105-
return scope == null || scope.chars().allMatch((c) -> withinTheRangeOf(c, 0x20, 0x21)
106-
|| withinTheRangeOf(c, 0x23, 0x5B) || withinTheRangeOf(c, 0x5D, 0x7E));
116+
// @formatter:off
117+
return scope == null || scope.chars().allMatch((c) ->
118+
withinTheRangeOf(c, 0x20, 0x21) ||
119+
withinTheRangeOf(c, 0x23, 0x5B) ||
120+
withinTheRangeOf(c, 0x5D, 0x7E));
121+
// @formatter:on
107122
}
108123

109124
private static boolean withinTheRangeOf(int c, int min, int max) {

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtIssuerReactiveAuthenticationManagerResolver.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,13 @@ public JwtIssuerReactiveAuthenticationManagerResolver(
122122
*/
123123
@Override
124124
public Mono<ReactiveAuthenticationManager> resolve(ServerWebExchange exchange) {
125+
// @formatter:off
125126
return this.issuerConverter.convert(exchange)
126-
.flatMap((issuer) -> this.issuerAuthenticationManagerResolver.resolve(issuer)
127-
.switchIfEmpty(Mono.error(() -> new InvalidBearerTokenException("Invalid issuer " + issuer))));
127+
.flatMap((issuer) -> this.issuerAuthenticationManagerResolver
128+
.resolve(issuer)
129+
.switchIfEmpty(Mono.error(() -> new InvalidBearerTokenException("Invalid issuer " + issuer)))
130+
);
131+
// @formatter:on
128132
}
129133

130134
private static class JwtClaimIssuerConverter implements Converter<ServerWebExchange, Mono<String>> {
@@ -166,10 +170,13 @@ public Mono<ReactiveAuthenticationManager> resolve(String issuer) {
166170
if (!this.trustedIssuer.test(issuer)) {
167171
return Mono.empty();
168172
}
173+
// @formatter:off
169174
return this.authenticationManagers.computeIfAbsent(issuer,
170-
(k) -> Mono.<ReactiveAuthenticationManager>fromCallable(
171-
() -> new JwtReactiveAuthenticationManager(ReactiveJwtDecoders.fromIssuerLocation(k)))
172-
.subscribeOn(Schedulers.boundedElastic()).cache());
175+
(k) -> Mono.<ReactiveAuthenticationManager>fromCallable(() -> new JwtReactiveAuthenticationManager(ReactiveJwtDecoders.fromIssuerLocation(k)))
176+
.subscribeOn(Schedulers.boundedElastic())
177+
.cache()
178+
);
179+
// @formatter:on
173180
}
174181

175182
}

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtReactiveAuthenticationManager.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ public JwtReactiveAuthenticationManager(ReactiveJwtDecoder jwtDecoder) {
5252

5353
@Override
5454
public Mono<Authentication> authenticate(Authentication authentication) {
55-
return Mono.justOrEmpty(authentication).filter((a) -> a instanceof BearerTokenAuthenticationToken)
56-
.cast(BearerTokenAuthenticationToken.class).map(BearerTokenAuthenticationToken::getToken)
57-
.flatMap(this.jwtDecoder::decode).flatMap(this.jwtAuthenticationConverter::convert)
58-
.cast(Authentication.class).onErrorMap(JwtException.class, this::onError);
55+
// @formatter:off
56+
return Mono.justOrEmpty(authentication)
57+
.filter((a) -> a instanceof BearerTokenAuthenticationToken)
58+
.cast(BearerTokenAuthenticationToken.class)
59+
.map(BearerTokenAuthenticationToken::getToken)
60+
.flatMap(this.jwtDecoder::decode)
61+
.flatMap(this.jwtAuthenticationConverter::convert)
62+
.cast(Authentication.class)
63+
.onErrorMap(JwtException.class, this::onError);
64+
// @formatter:on
5965
}
6066

6167
/**

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/OpaqueTokenReactiveAuthenticationManager.java

+19-10
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,28 @@ public OpaqueTokenReactiveAuthenticationManager(ReactiveOpaqueTokenIntrospector
7575

7676
@Override
7777
public Mono<Authentication> authenticate(Authentication authentication) {
78-
return Mono.justOrEmpty(authentication).filter(BearerTokenAuthenticationToken.class::isInstance)
79-
.cast(BearerTokenAuthenticationToken.class).map(BearerTokenAuthenticationToken::getToken)
80-
.flatMap(this::authenticate).cast(Authentication.class);
78+
// @formatter:off
79+
return Mono.justOrEmpty(authentication)
80+
.filter(BearerTokenAuthenticationToken.class::isInstance)
81+
.cast(BearerTokenAuthenticationToken.class)
82+
.map(BearerTokenAuthenticationToken::getToken)
83+
.flatMap(this::authenticate)
84+
.cast(Authentication.class);
85+
// @formatter:on
8186
}
8287

8388
private Mono<BearerTokenAuthentication> authenticate(String token) {
84-
return this.introspector.introspect(token).map((principal) -> {
85-
Instant iat = principal.getAttribute(OAuth2IntrospectionClaimNames.ISSUED_AT);
86-
Instant exp = principal.getAttribute(OAuth2IntrospectionClaimNames.EXPIRES_AT);
87-
// construct token
88-
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, token, iat, exp);
89-
return new BearerTokenAuthentication(principal, accessToken, principal.getAuthorities());
90-
}).onErrorMap(OAuth2IntrospectionException.class, this::onError);
89+
// @formatter:off
90+
return this.introspector.introspect(token)
91+
.map((principal) -> {
92+
Instant iat = principal.getAttribute(OAuth2IntrospectionClaimNames.ISSUED_AT);
93+
Instant exp = principal.getAttribute(OAuth2IntrospectionClaimNames.EXPIRES_AT);
94+
// construct token
95+
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, token, iat, exp);
96+
return new BearerTokenAuthentication(principal, accessToken, principal.getAuthorities());
97+
})
98+
.onErrorMap(OAuth2IntrospectionException.class, this::onError);
99+
// @formatter:on
91100
}
92101

93102
private AuthenticationException onError(OAuth2IntrospectionException ex) {

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/ReactiveJwtAuthenticationConverter.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ public final class ReactiveJwtAuthenticationConverter implements Converter<Jwt,
3939

4040
@Override
4141
public Mono<AbstractAuthenticationToken> convert(Jwt jwt) {
42-
return this.jwtGrantedAuthoritiesConverter.convert(jwt).collectList()
42+
// @formatter:off
43+
return this.jwtGrantedAuthoritiesConverter.convert(jwt)
44+
.collectList()
4345
.map((authorities) -> new JwtAuthenticationToken(jwt, authorities));
46+
// @formatter:on
4447
}
4548

4649
/**

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/introspection/NimbusReactiveOpaqueTokenIntrospector.java

+21-7
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,39 @@ public NimbusReactiveOpaqueTokenIntrospector(String introspectionUri, WebClient
9090

9191
@Override
9292
public Mono<OAuth2AuthenticatedPrincipal> introspect(String token) {
93-
return Mono.just(token).flatMap(this::makeRequest).flatMap(this::adaptToNimbusResponse)
94-
.map(this::parseNimbusResponse).map(this::castToNimbusSuccess)
95-
.doOnNext((response) -> validate(token, response)).map(this::convertClaimsSet)
93+
// @formatter:off
94+
return Mono.just(token)
95+
.flatMap(this::makeRequest)
96+
.flatMap(this::adaptToNimbusResponse)
97+
.map(this::parseNimbusResponse)
98+
.map(this::castToNimbusSuccess)
99+
.doOnNext((response) -> validate(token, response))
100+
.map(this::convertClaimsSet)
96101
.onErrorMap((e) -> !(e instanceof OAuth2IntrospectionException), this::onError);
102+
// @formatter:on
97103
}
98104

99105
private Mono<ClientResponse> makeRequest(String token) {
100-
return this.webClient.post().uri(this.introspectionUri)
106+
// @formatter:off
107+
return this.webClient.post()
108+
.uri(this.introspectionUri)
101109
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE)
102-
.body(BodyInserters.fromFormData("token", token)).exchange();
110+
.body(BodyInserters.fromFormData("token", token))
111+
.exchange();
112+
// @formatter:on
103113
}
104114

105115
private Mono<HTTPResponse> adaptToNimbusResponse(ClientResponse responseEntity) {
106116
HTTPResponse response = new HTTPResponse(responseEntity.rawStatusCode());
107117
response.setHeader(HttpHeaders.CONTENT_TYPE, responseEntity.headers().contentType().get().toString());
108118
if (response.getStatusCode() != HTTPResponse.SC_OK) {
109-
return responseEntity.bodyToFlux(DataBuffer.class).map(DataBufferUtils::release)
119+
// @formatter:off
120+
return responseEntity.bodyToFlux(DataBuffer.class)
121+
.map(DataBufferUtils::release)
110122
.then(Mono.error(new OAuth2IntrospectionException(
111-
"Introspection endpoint responded with " + response.getStatusCode())));
123+
"Introspection endpoint responded with " + response.getStatusCode()))
124+
);
125+
// @formatter:on
112126
}
113127
return responseEntity.bodyToMono(String.class).doOnNext(response::setContent).map((body) -> response);
114128
}

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/access/server/BearerTokenServerAccessDeniedHandler.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException denie
5959
if (this.realmName != null) {
6060
parameters.put("realm", this.realmName);
6161
}
62-
return exchange.getPrincipal().filter(AbstractOAuth2TokenAuthenticationToken.class::isInstance)
63-
.map((token) -> errorMessageParameters(parameters)).switchIfEmpty(Mono.just(parameters))
62+
// @formatter:off
63+
return exchange.getPrincipal()
64+
.filter(AbstractOAuth2TokenAuthenticationToken.class::isInstance)
65+
.map((token) -> errorMessageParameters(parameters))
66+
.switchIfEmpty(Mono.just(parameters))
6467
.flatMap((params) -> respond(exchange, params));
68+
// @formatter:on
6569
}
6670

6771
/**

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/reactive/function/client/ServerBearerExchangeFilterFunction.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,35 @@ public final class ServerBearerExchangeFilterFunction implements ExchangeFilterF
5353

5454
@Override
5555
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
56-
return oauth2Token().map((token) -> bearer(request, token)).defaultIfEmpty(request).flatMap(next::exchange);
56+
// @formatter:off
57+
return oauth2Token().map((token) -> bearer(request, token))
58+
.defaultIfEmpty(request)
59+
.flatMap(next::exchange);
60+
// @formatter:on
5761
}
5862

5963
private Mono<AbstractOAuth2Token> oauth2Token() {
64+
// @formatter:off
6065
return currentAuthentication()
6166
.filter((authentication) -> authentication.getCredentials() instanceof AbstractOAuth2Token)
62-
.map(Authentication::getCredentials).cast(AbstractOAuth2Token.class);
67+
.map(Authentication::getCredentials)
68+
.cast(AbstractOAuth2Token.class);
69+
// @formatter:on
6370
}
6471

6572
private Mono<Authentication> currentAuthentication() {
66-
return ReactiveSecurityContextHolder.getContext().map(SecurityContext::getAuthentication);
73+
// @formatter:off
74+
return ReactiveSecurityContextHolder.getContext()
75+
.map(SecurityContext::getAuthentication);
76+
// @formatter:on
6777
}
6878

6979
private ClientRequest bearer(ClientRequest request, AbstractOAuth2Token token) {
70-
return ClientRequest.from(request).headers((headers) -> headers.setBearerAuth(token.getTokenValue())).build();
80+
// @formatter:off
81+
return ClientRequest.from(request)
82+
.headers((headers) -> headers.setBearerAuth(token.getTokenValue()))
83+
.build();
84+
// @formatter:on
7185
}
7286

7387
}

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/reactive/function/client/ServletBearerExchangeFilterFunction.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,21 @@ public final class ServletBearerExchangeFilterFunction implements ExchangeFilter
6464

6565
@Override
6666
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
67-
return oauth2Token().map((token) -> bearer(request, token)).defaultIfEmpty(request).flatMap(next::exchange);
67+
// @formatter:off
68+
return oauth2Token().map((token) -> bearer(request, token))
69+
.defaultIfEmpty(request)
70+
.flatMap(next::exchange);
71+
// @formatter:on
6872
}
6973

7074
private Mono<AbstractOAuth2Token> oauth2Token() {
71-
return Mono.subscriberContext().flatMap(this::currentAuthentication)
75+
// @formatter:off
76+
return Mono.subscriberContext()
77+
.flatMap(this::currentAuthentication)
7278
.filter((authentication) -> authentication.getCredentials() instanceof AbstractOAuth2Token)
73-
.map(Authentication::getCredentials).cast(AbstractOAuth2Token.class);
79+
.map(Authentication::getCredentials)
80+
.cast(AbstractOAuth2Token.class);
81+
// @formatter:on
7482
}
7583

7684
private Mono<Authentication> currentAuthentication(Context ctx) {
@@ -88,7 +96,11 @@ private <T> T getAttribute(Context ctx, Class<T> clazz) {
8896
}
8997

9098
private ClientRequest bearer(ClientRequest request, AbstractOAuth2Token token) {
91-
return ClientRequest.from(request).headers((headers) -> headers.setBearerAuth(token.getTokenValue())).build();
99+
// @formatter:off
100+
return ClientRequest.from(request)
101+
.headers((headers) -> headers.setBearerAuth(token.getTokenValue()))
102+
.build();
103+
// @formatter:on
92104
}
93105

94106
}

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/BearerTokenAuthenticationTokenTests.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@ public class BearerTokenAuthenticationTokenTests {
3030

3131
@Test
3232
public void constructorWhenTokenIsNullThenThrowsException() {
33-
assertThatIllegalArgumentException().isThrownBy(() -> new BearerTokenAuthenticationToken(null))
33+
// @formatter:off
34+
assertThatIllegalArgumentException()
35+
.isThrownBy(() -> new BearerTokenAuthenticationToken(null))
3436
.withMessageContaining("token cannot be empty");
37+
// @formatter:on
3538
}
3639

3740
@Test
3841
public void constructorWhenTokenIsEmptyThenThrowsException() {
39-
assertThatIllegalArgumentException().isThrownBy(() -> new BearerTokenAuthenticationToken(""))
42+
// @formatter:off
43+
assertThatIllegalArgumentException()
44+
.isThrownBy(() -> new BearerTokenAuthenticationToken(""))
4045
.withMessageContaining("token cannot be empty");
46+
// @formatter:on
4147
}
4248

4349
@Test

0 commit comments

Comments
 (0)