From bda65850d905cff19986a9f38f63ad7035c08693 Mon Sep 17 00:00:00 2001 From: Besmir Beqiri Date: Thu, 14 Dec 2023 17:37:52 +0100 Subject: [PATCH] Correct the `jwt` routing filter --- .../platform/auth/routing/AuthFilters.java | 64 +++---------------- 1 file changed, 10 insertions(+), 54 deletions(-) diff --git a/jpro-auth/routing/src/main/java/one/jpro/platform/auth/routing/AuthFilters.java b/jpro-auth/routing/src/main/java/one/jpro/platform/auth/routing/AuthFilters.java index fa9c036f..bff3af83 100644 --- a/jpro-auth/routing/src/main/java/one/jpro/platform/auth/routing/AuthFilters.java +++ b/jpro-auth/routing/src/main/java/one/jpro/platform/auth/routing/AuthFilters.java @@ -11,7 +11,6 @@ import simplefx.experimental.parts.FXFuture; import java.util.Objects; -import java.util.function.Consumer; import java.util.function.Function; /** @@ -30,72 +29,29 @@ public final class AuthFilters { * @param credentials a JSON object with the authentication information * @param authPath the authentication path for the routing * @param tokenPath the token path - * @param userConsumer operation on the given user argument - * @param errorConsumer operation on the given error argument + * @param userFunction operation on the given user argument + * @param errorFunction operation on the given error argument * @return a {@link Filter} object */ static Filter jwt(JWTAuthenticationProvider authProvider, JSONObject credentials, String authPath, String tokenPath, - Consumer userConsumer, - Consumer errorConsumer) { + Function> userFunction, + Function> errorFunction) { Objects.requireNonNull(authProvider, "auth provider cannot be null"); Objects.requireNonNull(credentials, "credentials cannot be null"); Objects.requireNonNull(authPath, "authentication path cannot be null"); Objects.requireNonNull(tokenPath, "token path cannot be null"); - Objects.requireNonNull(userConsumer, "user consumer cannot be null"); - Objects.requireNonNull(errorConsumer, "error consumer cannot be null"); + Objects.requireNonNull(userFunction, "user function cannot be null"); + Objects.requireNonNull(errorFunction, "error function cannot be null"); return (route) -> (request) -> { if (request.path().equals(authPath)) { return FXFuture.fromJava(authProvider.token(tokenPath, credentials) .thenCompose(authProvider::authenticate)) - .flatMap(user -> { - userConsumer.accept(user); - return route.apply(request); - }) - .flatExceptionally(ex -> { - errorConsumer.accept(ex); - return route.apply(request); - }); - } else { - return route.apply(request); - } - }; - } - - /** - * Creates {@link Route} filter from a given {@link OAuth2AuthenticationProvider}, - * {@link OAuth2Credentials} and an operation a given user if the authentication - * is successful. - * - * @param authProvider the OAuth2 authentication provider - * @param credentials the OAuth2 credentials - * @param userConsumer consumer operation on the given user argument - * @param errorConsumer consumer operation on the given error argument - * @return a {@link Filter} object - */ - public static Filter oauth2(OAuth2AuthenticationProvider authProvider, - OAuth2Credentials credentials, - Consumer userConsumer, - Consumer errorConsumer) { - Objects.requireNonNull(authProvider, "auth provider can not be null"); - Objects.requireNonNull(credentials, "credentials can not be null"); - Objects.requireNonNull(userConsumer, "user consumer can not be null"); - Objects.requireNonNull(errorConsumer, "error consumer cannot be null"); - - return (route) -> (request) -> { - if (request.path().equals(credentials.getRedirectUri())) { - return FXFuture.fromJava(authProvider.authenticate(credentials)) - .flatMap(user -> { - userConsumer.accept(user); - return route.apply(request); - }) - .flatExceptionally(ex -> { - errorConsumer.accept(ex); - return route.apply(request); - }); + .flatMap(userFunction::apply) + .flatExceptionally(errorFunction::apply); } else { return route.apply(request); } @@ -109,8 +65,8 @@ public static Filter oauth2(OAuth2AuthenticationProvider authProvider, * * @param authProvider the OAuth2 authentication provider * @param credentials the OAuth2 credentials - * @param userFunction function operation on the given user argument - * @param errorFunction function operation on the given error argument + * @param userFunction operation on the given user argument + * @param errorFunction operation on the given error argument * @return a {@link Filter} object */ public static Filter oauth2(OAuth2AuthenticationProvider authProvider,