Skip to content

Commit 8f73bbd

Browse files
Make the reactive oauth stack more reactive by allowing JwtValidators in NimbusReactiveJwtDecoder to be reactive.
Signed-off-by: Iain Henderson <Iain.henderson@mac.com>
1 parent 5a7d93e commit 8f73bbd

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public final class NimbusReactiveJwtDecoder implements ReactiveJwtDecoder {
9696

9797
private final Converter<JWT, Mono<JWTClaimsSet>> jwtProcessor;
9898

99-
private OAuth2TokenValidator<Jwt> jwtValidator = JwtValidators.createDefault();
99+
private Converter<Jwt, Mono<Jwt>> jwtValidator;
100100

101101
private Converter<Map<String, Object>, Map<String, Object>> claimSetConverter = MappedJwtClaimSetConverter
102102
.withDefaults(Collections.emptyMap());
@@ -302,6 +302,37 @@ private static <C extends SecurityContext> JWTClaimsSet createClaimsSet(JWTProce
302302
}
303303
}
304304

305+
/**
306+
* Use the provided {@link OAuth2TokenValidator} to validate incoming {@link Jwt}s.
307+
* @param jwtValidator the {@link OAuth2TokenValidator} to use
308+
*/
309+
public void setJwtValidator(OAuth2TokenValidator<Jwt> jwtValidator) {
310+
Assert.notNull(jwtValidator, "jwtValidator cannot be null");
311+
this.jwtValidator = jwt -> Mono.fromSupplier(() -> jwtValidator.validate(jwt))
312+
.subscribeOn(Schedulers.boundedElastic())
313+
.handle((result, sink) -> {
314+
if (result.hasErrors()) {
315+
Collection<OAuth2Error> errors = result.getErrors();
316+
String validationErrorString = getJwtValidationExceptionMessage(errors);
317+
sink.error(new JwtValidationException(validationErrorString, errors));
318+
}
319+
else {
320+
sink.next(jwt);
321+
}
322+
});
323+
}
324+
325+
/**
326+
* Use the provided {@link Converter} to validate incoming {@link Jwt}s.
327+
* This overrides the specified OAuth2TokenValidator, but allows for a purely reactive implementation.
328+
* @param jwtValidator the {@link Converter} to use
329+
* @since 7.1
330+
*/
331+
public void setJwtValidator(Converter<Jwt, Mono<Jwt>> jwtValidator) {
332+
Assert.notNull(jwtValidator, "jwtValidator cannot be null");
333+
this.jwtValidator = jwtValidator;
334+
}
335+
305336
/**
306337
* A builder for creating {@link NimbusReactiveJwtDecoder} instances based on a
307338
* <a target="_blank" href="https://tools.ietf.org/html/rfc7517#section-5">JWK Set</a>

0 commit comments

Comments
 (0)