Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide UserAPI #37

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import one.jpro.platform.auth.example.basic.page.SignedInPage;
import one.jpro.platform.auth.example.oauth.OAuthApp;
import one.jpro.platform.auth.routing.AuthBasicFilter;
import one.jpro.platform.auth.routing.UserAPI;
import one.jpro.platform.routing.Response;
import one.jpro.platform.routing.Route;
import one.jpro.platform.routing.RouteApp;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class BasicLoginApp extends RouteApp {

private static final SessionManager sessionManager = new SessionManager("basic-login-app");
ObservableMap<String, String> session;
public UserAPI userAPI;

public BasicLoginApp() {
userManager.createUser(new UsernamePasswordCredentials("admin", "password"),
Expand All @@ -73,6 +75,7 @@ public BasicLoginApp() {
public Route createRoute() {
session = (WebAPI.isBrowser()) ? sessionManager.getSession(getWebAPI()) :
sessionManager.getSession("user-session");
userAPI = new UserAPI(session);

Optional.ofNullable(CupertinoLight.class.getResource(new CupertinoLight().getUserAgentStylesheet()))
.map(URL::toExternalForm)
Expand All @@ -85,31 +88,13 @@ public Route createRoute() {
.when(request -> isUserAuthenticated(), Route.empty()
.and(Route.get("/user/signed-in", request -> Response.node(new SignedInPage(this)))))
.filter(AuthBasicFilter.create(basicAuthProvider, credentials, user -> {
setUser(user);
userAPI.setUser(user);
return Response.redirect("/user/signed-in");
}, error -> Response.node(new ErrorPage(error))))
.filter(DevFilter.create());
}

public final User getUser() {
final var userJsonString = session.get("user");
if (userJsonString != null) {
final JSONObject userJson = new JSONObject(userJsonString);
return new User(userJson);
} else {
return null;
}
}

public final void setUser(User value) {
if (value != null) {
session.put("user", value.toJSON().toString());
} else {
session.remove("user");
}
}

private boolean isUserAuthenticated() {
return getUser() != null;
return userAPI.getUser() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public SignedInPage(BasicLoginApp app) {
final var headerLabel = new Label("Not signed in.");
headerLabel.getStyleClass().add("header-label");

final var user = app.getUser();
final var user = app.userAPI.getUser();
if (user == null) {
getChildren().add(headerLabel);
} else {
Expand All @@ -31,7 +31,7 @@ public SignedInPage(BasicLoginApp app) {

final var signOutButton = new Button("Sign out");
signOutButton.setOnAction(event -> {
app.setUser(null);
app.userAPI.setUser(null);
app.getSessionManager().gotoURL("/");
});
signOutButton.setDefaultButton(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
import com.jpro.webapi.WebAPI;
import javafx.collections.ObservableMap;
import one.jpro.platform.auth.core.AuthAPI;
import one.jpro.platform.auth.core.authentication.User;
import one.jpro.platform.auth.example.login.page.ErrorPage;
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.AuthOAuth2Filter;
import one.jpro.platform.auth.routing.UserAPI;
import one.jpro.platform.routing.Response;
import one.jpro.platform.routing.Route;
import one.jpro.platform.routing.RouteApp;
import one.jpro.platform.routing.dev.DevFilter;
import one.jpro.platform.session.SessionManager;
import org.json.JSONObject;

import java.net.URL;
import java.util.Optional;
Expand Down Expand Up @@ -49,10 +48,13 @@ public class GoogleLoginApp extends RouteApp {
private static final SessionManager sessionManager = new SessionManager("google-login-app");
ObservableMap<String, String> session;

public UserAPI userAPI;

@Override
public Route createRoute() {
session = (WebAPI.isBrowser()) ? sessionManager.getSession(getWebAPI())
: sessionManager.getSession("user-session");
userAPI = new UserAPI(session);

Optional.ofNullable(CupertinoLight.class.getResource(new CupertinoLight().getUserAgentStylesheet()))
.map(URL::toExternalForm)
Expand All @@ -70,32 +72,13 @@ public Route createRoute() {
.and(Route.get("/", request -> Response.node(new LoginPage(googleAuthProvider))))
.when(request -> isUserAuthenticated(), Route.empty()
.and(Route.get("/user/signed-in", request -> Response.node(new SignedInPage(this, googleAuthProvider)))))
.filter(AuthOAuth2Filter.create(googleAuthProvider, user -> {
setUser(user);
.filter(AuthOAuth2Filter.create(googleAuthProvider, userAPI, user -> {
return Response.redirect("/user/signed-in");
}, error -> Response.node(new ErrorPage(error))))
.filter(DevFilter.create());
}

public final User getUser() {
final var userJsonString = session.get("user");
if (userJsonString != null) {
final JSONObject userJson = new JSONObject(userJsonString);
return new User(userJson);
} else {
return null;
}
}

public final void setUser(User value) {
if (value != null) {
session.put("user", value.toJSON().toString());
} else {
session.remove("user");
}
}

private boolean isUserAuthenticated() {
return getUser() != null;
return userAPI.getUser() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public SignedInPage(GoogleLoginApp app, OAuth2AuthenticationProvider authProvide
final var headerLabel = new Label("Not signed in.");
headerLabel.getStyleClass().add("header-label");

final var user = app.getUser();
final var user = app.userAPI.getUser();
if (user == null) {
getChildren().add(headerLabel);
} else {
Expand All @@ -33,9 +33,9 @@ public SignedInPage(GoogleLoginApp app, OAuth2AuthenticationProvider authProvide

final var signOutButton = new Button("Sign out");
signOutButton.setOnAction(event ->
FXFuture.fromJava(authProvider.revoke(app.getUser(), "access_token"))
FXFuture.fromJava(authProvider.revoke(app.userAPI.getUser(), "access_token"))
.onSuccess(nothing -> {
app.setUser(null);
app.userAPI.setUser(null);
app.getSessionManager().gotoURL("/");
}));
signOutButton.setDefaultButton(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package one.jpro.platform.auth.example.oauth;

import atlantafx.base.theme.CupertinoLight;
import com.jpro.webapi.WebAPI;
import javafx.collections.ObservableMap;
import one.jpro.platform.auth.core.AuthAPI;
import one.jpro.platform.auth.core.oauth2.provider.OpenIDAuthenticationProvider;
import one.jpro.platform.auth.example.oauth.page.*;
import one.jpro.platform.auth.routing.AuthOAuth2Filter;
import one.jpro.platform.auth.routing.UserAPI;
import one.jpro.platform.routing.Filter;
import one.jpro.platform.routing.Response;
import one.jpro.platform.routing.Route;
import one.jpro.platform.routing.dev.DevFilter;
import one.jpro.platform.routing.dev.StatisticsFilter;
import one.jpro.platform.session.SessionManager;

import java.net.URL;
import java.util.Optional;
Expand All @@ -23,8 +27,17 @@
*/
public class OAuthApp extends BaseOAuthApp {

private static final SessionManager sessionManager = new SessionManager("oauth-app");

ObservableMap<String, String> session;
UserAPI userManager;

@Override
public Route createRoute() {
session = (WebAPI.isBrowser()) ? sessionManager.getSession(getWebAPI())
: sessionManager.getSession("user-session");
userManager = new UserAPI(session);

Optional.ofNullable(CupertinoLight.class.getResource(new CupertinoLight().getUserAgentStylesheet()))
.map(URL::toExternalForm)
.ifPresent(getScene()::setUserAgentStylesheet);
Expand Down Expand Up @@ -92,8 +105,7 @@ public Route createRoute() {
* @return A {@link Filter} object configured for OAuth2 authentication flow.
*/
private Filter oauth2Filter(OpenIDAuthenticationProvider openIDAuthProvider) {
return AuthOAuth2Filter.create(openIDAuthProvider, user -> {
setUser(user);
return AuthOAuth2Filter.create(openIDAuthProvider, userManager, user -> {
setAuthProvider(openIDAuthProvider);
return Response.redirect(USER_CONSOLE_PATH);
}, error -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,32 @@ public interface AuthOAuth2Filter {
* {@link OAuth2Credentials} and functions for handling successful and error cases.
*
* @param openidAuthProvider the OpenID authentication provider
* @param userAPI the user API
* @param userFunction operation on the given user argument
* @param errorFunction operation on the given error argument
* @return a {@link Filter} object
*/
static Filter create(@NotNull OpenIDAuthenticationProvider openidAuthProvider,
@NotNull UserAPI userAPI,
@NotNull Function<User, Response> userFunction,
@NotNull Function<Throwable, Response> errorFunction) {
final var credentials = openidAuthProvider.getCredentials();
return create(openidAuthProvider, credentials, userFunction, errorFunction);
return create(openidAuthProvider, userAPI, credentials, userFunction, errorFunction);
}

/**
* Creates {@link Route} filter from a given {@link OAuth2AuthenticationProvider},
* {@link OAuth2Credentials} and functions for handling successful and error cases.
*
* @param authProvider an OAuth2 authentication provider
* @param userAPI the user API
* @param credentials an 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(@NotNull OAuth2AuthenticationProvider authProvider,
@NotNull UserAPI userAPI,
@NotNull OAuth2Credentials credentials,
@NotNull Function<User, Response> userFunction,
@NotNull Function<Throwable, Response> errorFunction) {
Expand All @@ -58,7 +62,10 @@ static Filter create(@NotNull OAuth2AuthenticationProvider authProvider,
return (route) -> (request) -> {
if (request.getPath().equals(credentials.getRedirectUri())) {
return new Response(FXFuture.fromJava(authProvider.authenticate(credentials))
.flatMap(r -> userFunction.apply(r).future())
.flatMap(r -> {
userAPI.setUser(r);
return userFunction.apply(r).future();
})
.flatExceptionally(r -> errorFunction.apply(r).future()));
} else {
return route.apply(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package one.jpro.platform.auth.routing;

import javafx.collections.ObservableMap;
import one.jpro.platform.auth.core.authentication.User;
import org.json.JSONObject;

/**
* The UserAPI class is a simple
* class that provides a way to
* get and set the user object
* from the session.
* @author floriankirmaier
*/
public class UserAPI {

private ObservableMap<String, String> session;

/**
* The constructor for the UserAPI class
* @param session
*/
public UserAPI(ObservableMap<String, String> session) {
this.session = session;
}

/**
* The getUser method returns the user
* object from the session
* @return User
*/
public final User getUser() {
final var userJsonString = session.get("user");
if (userJsonString != null) {
final JSONObject userJson = new JSONObject(userJsonString);
return new User(userJson);
} else {
return null;
}
}

/**
* The setUser method sets the user
* object in the session
* @param user
*/
public final void setUser(User user) {
if (user != null) {
session.put("user", user.toJSON().toString());
} else {
session.remove("user");
}
}
}
Loading