Skip to content

Commit

Permalink
Add jpro-auth-routing module
Browse files Browse the repository at this point in the history
  • Loading branch information
besidev committed Dec 14, 2023
1 parent 36842c0 commit 08a9da5
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 10 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ configure(subprojects.findAll { it.name != 'example' }) {
}
}

configure([project("tree-showing"), project("jpro-auth:core"), project("jpro-file"), project("jpro-image-manager"),
project("jpro-mdfx"), project("jpro-media"), project("jpro-sessions"), project("jpro-html-scrollpane"),
project("freeze-detector"), project("jpro-routing:core"), project("jpro-routing:dev"),
project("jpro-routing:popup"), project("jpro-webrtc"), project("jpro-youtube"),
configure([project("tree-showing"), project("jpro-auth:core"), project("jpro-auth:routing"), project("jpro-file"),
project("jpro-image-manager"), project("jpro-mdfx"), project("jpro-media"), project("jpro-sessions"),
project("jpro-html-scrollpane"), project("freeze-detector"), project("jpro-routing:core"),
project("jpro-routing:dev"), project("jpro-routing:popup"), project("jpro-webrtc"), project("jpro-youtube"),
project("internal:openlink")]) {
apply plugin: 'maven-publish'

Expand Down
4 changes: 2 additions & 2 deletions jpro-auth/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ publishing {
mavenJava(MavenPublication) {
pom {
packaging = 'jar'
name = 'JPro Auth'
description = 'A library for authenticating and authorizing users in JavaFX applications running via JPro server.'
name = 'JPro Auth Core'
description = 'Core library for authenticating and authorizing users.'
url = 'https://www.jpro.one'

developers {
Expand Down
10 changes: 6 additions & 4 deletions jpro-auth/core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
*
* @author Besmir Beqiri
*/
module one.jpro.platform.auth {
module one.jpro.platform.auth.core {
requires javafx.graphics;
requires org.jetbrains.annotations;
requires java.net.http;
requires jpro.webapi;
requires org.jetbrains.annotations;
requires org.json;
requires jwks.rsa;
requires org.slf4j;
requires com.auth0.jwt;
requires one.jpro.platform.internal.openlink;

requires transitive org.json;
requires transitive org.slf4j;

opens one.jpro.platform.auth.core;

exports one.jpro.platform.auth.core;
exports one.jpro.platform.auth.core.api;
exports one.jpro.platform.auth.core.authentication;
Expand Down
29 changes: 29 additions & 0 deletions jpro-auth/routing/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
dependencies {
api project(":jpro-auth:core")
api project(":jpro-routing:core")

testImplementation "org.junit.jupiter:junit-jupiter-api:$JUNIT_VERSION"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$JUNIT_VERSION"
testRuntimeOnly "ch.qos.logback:logback-classic:$LOGBACK_VERSION"
}

publishing {
publications {
mavenJava(MavenPublication) {
pom {
packaging = 'jar'
name = 'JPro Auth Routing'
description = 'A library that makes it easy to combine and use the auth and routing functionalities.'
url = 'https://www.jpro.one'

developers {
developer {
id = 'besidev'
name = 'Besmir Beqiri'
email = '[email protected]'
}
}
}
}
}
}
11 changes: 11 additions & 0 deletions jpro-auth/routing/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Module descriptor for the Auth Routing module.
*
* @author Besmir Beqiri
*/
module one.jpro.platform.auth.routing {
requires transitive one.jpro.platform.auth.core;
requires transitive one.jpro.platform.routing.core;

exports one.jpro.platform.auth.routing;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package one.jpro.platform.auth.routing;

import one.jpro.platform.auth.core.authentication.User;
import one.jpro.platform.auth.core.jwt.JWTAuthenticationProvider;
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 org.json.JSONObject;
import simplefx.experimental.parts.FXFuture;

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

/**
* Utility class with authorization filters used in the routing process.
*
* @author Besmir Beqiri
*/
public final class AuthFilters {

/**
* 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 JWT authentication provider
* @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
* @return a {@link Filter} object
*/
static Filter jwt(JWTAuthenticationProvider authProvider,
JSONObject credentials,
String authPath,
String tokenPath,
Consumer<? super User> userConsumer,
Consumer<? super Throwable> errorConsumer) {
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");

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<? super User> userConsumer,
Consumer<? super Throwable> 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);
});
} 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 userFunction function operation on the given user argument
* @param errorFunction function 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(user -> {
userFunction.apply(user);
return route.apply(request);
})
.flatExceptionally(ex -> {
errorFunction.apply(ex);
return route.apply(request);
});
} else {
return route.apply(request);
}
};
}

private AuthFilters() {
// Hide the default constructor.
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include "jpro-auth:core"
include "jpro-auth:routing"
include "jpro-auth:example"
include "jpro-file"
include "jpro-file:example"
Expand Down

0 comments on commit 08a9da5

Please sign in to comment.