From b26f2af5d51a9ba8a251447c63e043443f9371da Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Mon, 7 Oct 2024 16:40:22 -0600 Subject: [PATCH] Polish Formatting as well as adding a missing defer Issue gh-15699 --- .../web/server/OneTimeTokenLoginSpecTests.java | 12 ++++++++---- .../OneTimeTokenReactiveAuthenticationManager.java | 2 +- .../ott/GenerateOneTimeTokenWebFilter.java | 5 +++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/config/src/test/java/org/springframework/security/config/web/server/OneTimeTokenLoginSpecTests.java b/config/src/test/java/org/springframework/security/config/web/server/OneTimeTokenLoginSpecTests.java index e816337e14..f3b20e3266 100644 --- a/config/src/test/java/org/springframework/security/config/web/server/OneTimeTokenLoginSpecTests.java +++ b/config/src/test/java/org/springframework/security/config/web/server/OneTimeTokenLoginSpecTests.java @@ -292,7 +292,8 @@ public class OneTimeTokenLoginSpecTests { http .authorizeExchange((authorize) -> authorize .anyExchange() - .authenticated()) + .authenticated() + ) .oneTimeTokenLogin((ott) -> ott .generatedOneTimeTokenHandler(new TestServerGeneratedOneTimeTokenHandler()) ); @@ -314,7 +315,8 @@ public class OneTimeTokenLoginSpecTests { http .authorizeExchange((authorize) -> authorize .anyExchange() - .authenticated()) + .authenticated() + ) .oneTimeTokenLogin((ott) -> ott .generateTokenUrl("/generateurl") .generatedOneTimeTokenHandler(new TestServerGeneratedOneTimeTokenHandler("/redirected")) @@ -339,7 +341,8 @@ public class OneTimeTokenLoginSpecTests { http .authorizeExchange((authorize) -> authorize .anyExchange() - .authenticated()) + .authenticated() + ) .formLogin(Customizer.withDefaults()) .oneTimeTokenLogin((ott) -> ott .generatedOneTimeTokenHandler(new TestServerGeneratedOneTimeTokenHandler()) @@ -362,7 +365,8 @@ public class OneTimeTokenLoginSpecTests { http .authorizeExchange((authorize) -> authorize .anyExchange() - .authenticated()) + .authenticated() + ) .oneTimeTokenLogin(Customizer.withDefaults()); // @formatter:on return http.build(); diff --git a/core/src/main/java/org/springframework/security/authentication/ott/reactive/OneTimeTokenReactiveAuthenticationManager.java b/core/src/main/java/org/springframework/security/authentication/ott/reactive/OneTimeTokenReactiveAuthenticationManager.java index d7607a6ccc..c8ccc3d85d 100644 --- a/core/src/main/java/org/springframework/security/authentication/ott/reactive/OneTimeTokenReactiveAuthenticationManager.java +++ b/core/src/main/java/org/springframework/security/authentication/ott/reactive/OneTimeTokenReactiveAuthenticationManager.java @@ -54,7 +54,7 @@ public final class OneTimeTokenReactiveAuthenticationManager implements Reactive return Mono.empty(); } return this.oneTimeTokenService.consume(otpAuthenticationToken) - .switchIfEmpty(Mono.error(new InvalidOneTimeTokenException("Invalid token"))) + .switchIfEmpty(Mono.defer(() -> Mono.error(new InvalidOneTimeTokenException("Invalid token")))) .flatMap((consumed) -> this.userDetailsService.findByUsername(consumed.getUsername())) .map(onSuccess(otpAuthenticationToken)); } diff --git a/web/src/main/java/org/springframework/security/web/server/authentication/ott/GenerateOneTimeTokenWebFilter.java b/web/src/main/java/org/springframework/security/web/server/authentication/ott/GenerateOneTimeTokenWebFilter.java index 5cb181b5db..18f95f030a 100644 --- a/web/src/main/java/org/springframework/security/web/server/authentication/ott/GenerateOneTimeTokenWebFilter.java +++ b/web/src/main/java/org/springframework/security/web/server/authentication/ott/GenerateOneTimeTokenWebFilter.java @@ -58,8 +58,9 @@ public final class GenerateOneTimeTokenWebFilter implements WebFilter { // @formatter:off return this.matcher.matches(exchange) .filter(ServerWebExchangeMatcher.MatchResult::isMatch) - .flatMap((mathResult) -> exchange.getFormData()) - .flatMap((data) -> Mono.justOrEmpty(data.getFirst(USERNAME))) + .switchIfEmpty(chain.filter(exchange).then(Mono.empty())) + .then(exchange.getFormData()) + .mapNotNull((data) -> data.getFirst(USERNAME)) .switchIfEmpty(chain.filter(exchange).then(Mono.empty())) .flatMap((username) -> this.oneTimeTokenService.generate(new GenerateOneTimeTokenRequest(username))) .flatMap((token) -> this.generatedOneTimeTokenHandler.handle(exchange, token));