Skip to content

Commit

Permalink
Move authentication filter under their own classes
Browse files Browse the repository at this point in the history
  • Loading branch information
besidev committed Dec 15, 2023
1 parent 192b0bc commit 72f85a7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import atlantafx.base.theme.CupertinoLight;
import com.jpro.webapi.WebAPI;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableMap;
import one.jpro.platform.auth.core.AuthAPI;
import one.jpro.platform.auth.core.authentication.User;
Expand All @@ -12,7 +10,7 @@
import one.jpro.platform.auth.example.login.page.LoginPage;
import one.jpro.platform.auth.example.login.page.SignedInPage;
import one.jpro.platform.auth.example.oauth.OAuthApp;
import one.jpro.platform.auth.routing.AuthFilters;
import one.jpro.platform.auth.routing.OAuth2Filter;
import one.jpro.platform.routing.Redirect;
import one.jpro.platform.routing.Route;
import one.jpro.platform.routing.RouteApp;
Expand Down Expand Up @@ -78,7 +76,7 @@ public Route createRoute() {
.when((r) -> getUser() != null, Route.empty()
.and(getNode("/user/signed-in", (r) -> new SignedInPage(this, googleAuthProvider))))
.filter(DevFilter.create())
.filter(AuthFilters.oauth2(googleAuthProvider, googleCredentials, user -> {
.filter(OAuth2Filter.create(googleAuthProvider, googleCredentials, user -> {
setUser(user);
return FXFuture.unit(new Redirect("/user/signed-in"));
}, error -> FXFuture.unit(viewFromNode(new ErrorPage(error)))));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package one.jpro.platform.auth.example.login.page;

import javafx.beans.binding.Bindings;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import one.jpro.platform.auth.core.oauth2.OAuth2AuthenticationProvider;
import one.jpro.platform.auth.core.oauth2.OAuth2Credentials;
import one.jpro.platform.auth.example.oauth.page.*;
import one.jpro.platform.auth.routing.AuthFilters;
import one.jpro.platform.auth.routing.OAuth2Filter;
import one.jpro.platform.routing.Filter;
import one.jpro.platform.routing.Redirect;
import one.jpro.platform.routing.Route;
Expand Down Expand Up @@ -90,9 +90,9 @@ public Route createRoute() {
.and(getNode("/keycloak", (r) -> new AuthProviderDiscoveryPage(this, keycloakAuth)))))
.filter(DevFilter.create())
.filter(StatisticsFilter.create())
.filter(oauth2(googleAuth, googleCredentials))
.filter(oauth2(microsoftAuth, microsoftCredentials))
.filter(oauth2(keycloakAuth, keycloakCredentials));
.filter(oauth2Filter(googleAuth, googleCredentials))
.filter(oauth2Filter(microsoftAuth, microsoftCredentials))
.filter(oauth2Filter(keycloakAuth, keycloakCredentials));
}

/**
Expand All @@ -105,8 +105,8 @@ public Route createRoute() {
* @param credentials The OAuth2 credentials used for authentication.
* @return A {@link Filter} object configured for OAuth2 authentication flow.
*/
private Filter oauth2(OAuth2AuthenticationProvider authProvider, OAuth2Credentials credentials) {
return AuthFilters.oauth2(authProvider, credentials, user -> {
private Filter oauth2Filter(OAuth2AuthenticationProvider authProvider, OAuth2Credentials credentials) {
return OAuth2Filter.create(authProvider, credentials, user -> {
setUser(user);
setAuthProvider(authProvider);
return FXFuture.unit(new Redirect(USER_CONSOLE_PATH));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import java.util.function.Function;

/**
* Utility class with authorization filters used in the routing process.
* Defines a {@link Route} filter using JWT authentication mechanism.
*
* @author Besmir Beqiri
*/
public final class AuthFilters {
public interface JWTFilter {

/**
* Creates {@link Route} filter from a given {@link OAuth2AuthenticationProvider},
Expand All @@ -33,12 +33,12 @@ public final class AuthFilters {
* @param errorFunction operation on the given error argument
* @return a {@link Filter} object
*/
static Filter jwt(JWTAuthenticationProvider authProvider,
JSONObject credentials,
String authPath,
String tokenPath,
Function<User, FXFuture<Response>> userFunction,
Function<Throwable, FXFuture<Response>> errorFunction) {
static Filter create(JWTAuthenticationProvider authProvider,
JSONObject credentials,
String authPath,
String tokenPath,
Function<User, FXFuture<Response>> userFunction,
Function<Throwable, FXFuture<Response>> errorFunction) {
Objects.requireNonNull(authProvider, "auth provider cannot be null");
Objects.requireNonNull(credentials, "credentials cannot be null");
Objects.requireNonNull(authPath, "authentication path cannot be null");
Expand All @@ -57,39 +57,4 @@ static Filter jwt(JWTAuthenticationProvider authProvider,
}
};
}

/**
* 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 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,
OAuth2Credentials credentials,
Function<User, FXFuture<Response>> userFunction,
Function<Throwable, FXFuture<Response>> errorFunction) {
Objects.requireNonNull(authProvider, "auth provider can not be null");
Objects.requireNonNull(credentials, "credentials can not be null");
Objects.requireNonNull(userFunction, "user function can not be null");
Objects.requireNonNull(errorFunction, "error function cannot be null");

return (route) -> (request) -> {
if (request.path().equals(credentials.getRedirectUri())) {
return FXFuture.fromJava(authProvider.authenticate(credentials))
.flatMap(userFunction::apply)
.flatExceptionally(errorFunction::apply);
} else {
return route.apply(request);
}
};
}

private AuthFilters() {
// Hide the default constructor.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package one.jpro.platform.auth.routing;

import one.jpro.platform.auth.core.authentication.User;
import one.jpro.platform.auth.core.oauth2.OAuth2AuthenticationProvider;
import one.jpro.platform.auth.core.oauth2.OAuth2Credentials;
import one.jpro.platform.routing.Filter;
import one.jpro.platform.routing.Response;
import one.jpro.platform.routing.Route;
import simplefx.experimental.parts.FXFuture;

import java.util.Objects;
import java.util.function.Function;

/**
* Defines a {@link Route} filter using OAuth2 authentication mechanism.
*
* @author Besmir Beqiri
*/
public interface OAuth2Filter {

/**
* 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 userFunction operation on the given user argument
* @param errorFunction operation on the given error argument
* @return a {@link Filter} object
*/
static Filter create(OAuth2AuthenticationProvider authProvider,
OAuth2Credentials credentials,
Function<User, FXFuture<Response>> userFunction,
Function<Throwable, FXFuture<Response>> errorFunction) {
Objects.requireNonNull(authProvider, "auth provider can not be null");
Objects.requireNonNull(credentials, "credentials can not be null");
Objects.requireNonNull(userFunction, "user function can not be null");
Objects.requireNonNull(errorFunction, "error function cannot be null");

return (route) -> (request) -> {
if (request.path().equals(credentials.getRedirectUri())) {
return FXFuture.fromJava(authProvider.authenticate(credentials))
.flatMap(userFunction::apply)
.flatExceptionally(errorFunction::apply);
} else {
return route.apply(request);
}
};
}
}

0 comments on commit 72f85a7

Please sign in to comment.