From 65946e467cb1b794f6e4cca7293c95b51bdf0276 Mon Sep 17 00:00:00 2001 From: "Ahmad K. Bawaneh" Date: Wed, 10 Jul 2024 18:06:14 +0300 Subject: [PATCH] tweaks and enhancements --- .../java/org/dominokit/brix/CoreModule.java | 3 +- .../dominokit/brix/PresentationModule.java | 4 +-- .../org/dominokit/brix/api/Presenter.java | 6 ++-- .../dominokit/brix/security/Authorizer.java | 4 ++- .../brix/security/DefaultAuthorizer.java | 4 ++- .../brix/security/DenyAllAuthorizer.java | 4 ++- .../brix/security/IsSecurityContext.java | 36 +++++++++++++++++++ .../brix/security/PermitAllAuthorizer.java | 5 ++- .../brix/security/RolesAllowedAuthorizer.java | 5 ++- .../brix/security/SecurityContext.java | 5 +-- .../org/dominokit/brix/events}/BrixUser.java | 2 +- .../org/dominokit/brix/events}/HasRoles.java | 2 +- 12 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 domino-brix-client/src/main/java/org/dominokit/brix/security/IsSecurityContext.java rename {domino-brix-client/src/main/java/org/dominokit/brix/security => domino-brix-shared/src/main/java/org/dominokit/brix/events}/BrixUser.java (94%) rename {domino-brix-client/src/main/java/org/dominokit/brix/security => domino-brix-shared/src/main/java/org/dominokit/brix/events}/HasRoles.java (94%) diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/CoreModule.java b/domino-brix-client/src/main/java/org/dominokit/brix/CoreModule.java index 8cee4fa..8efd9ad 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/CoreModule.java +++ b/domino-brix-client/src/main/java/org/dominokit/brix/CoreModule.java @@ -27,6 +27,7 @@ import org.dominokit.brix.api.ConfigImpl; import org.dominokit.brix.api.StartupTask; import org.dominokit.brix.events.BrixEvents; +import org.dominokit.brix.security.IsSecurityContext; import org.dominokit.brix.security.SecurityContext; import org.dominokit.brix.tasks.TasksRunner; import org.dominokit.domino.client.history.StateHistory; @@ -64,7 +65,7 @@ public BrixSlots slots() { @Singleton @Provides - public SecurityContext securityContext() { + public IsSecurityContext securityContext() { return new SecurityContext(); } diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/PresentationModule.java b/domino-brix-client/src/main/java/org/dominokit/brix/PresentationModule.java index de7cd2a..55d21b7 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/PresentationModule.java +++ b/domino-brix-client/src/main/java/org/dominokit/brix/PresentationModule.java @@ -22,7 +22,7 @@ import org.dominokit.brix.api.BrixSlots; import org.dominokit.brix.api.Config; import org.dominokit.brix.events.BrixEvents; -import org.dominokit.brix.security.SecurityContext; +import org.dominokit.brix.security.IsSecurityContext; import org.dominokit.domino.history.AppHistory; @Module @@ -60,7 +60,7 @@ default Config globalConfig() { @Singleton @Provides - default SecurityContext globalSecurityContext() { + default IsSecurityContext globalSecurityContext() { return coreComponent().core().getSecurityContext(); } } diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/api/Presenter.java b/domino-brix-client/src/main/java/org/dominokit/brix/api/Presenter.java index 189939d..7d2fc4f 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/api/Presenter.java +++ b/domino-brix-client/src/main/java/org/dominokit/brix/api/Presenter.java @@ -28,12 +28,12 @@ import org.dominokit.brix.events.BrixEvent; import org.dominokit.brix.events.BrixEvents; import org.dominokit.brix.events.EventListener; +import org.dominokit.brix.events.HasRoles; import org.dominokit.brix.events.RegistrationRecord; import org.dominokit.brix.security.Authorizer; import org.dominokit.brix.security.DefaultAuthorizer; import org.dominokit.brix.security.HasAuthorizer; -import org.dominokit.brix.security.HasRoles; -import org.dominokit.brix.security.SecurityContext; +import org.dominokit.brix.security.IsSecurityContext; import org.dominokit.domino.history.AppHistory; import org.dominokit.domino.history.DominoHistory; import org.dominokit.domino.history.HistoryInterceptor; @@ -54,7 +54,7 @@ public abstract class Presenter @Inject @Global protected BrixSlots slots; - @Inject protected SecurityContext securityContext; + @Inject protected IsSecurityContext securityContext; @Inject @Global protected Config config; private RegistrationRecord eventsListenerRecord; diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/security/Authorizer.java b/domino-brix-client/src/main/java/org/dominokit/brix/security/Authorizer.java index 8d51ec1..fb9d823 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/security/Authorizer.java +++ b/domino-brix-client/src/main/java/org/dominokit/brix/security/Authorizer.java @@ -15,6 +15,8 @@ */ package org.dominokit.brix.security; +import org.dominokit.brix.events.HasRoles; + public interface Authorizer { - boolean isAuthorized(SecurityContext context, HasRoles hasRoles); + boolean isAuthorized(IsSecurityContext context, HasRoles hasRoles); } diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/security/DefaultAuthorizer.java b/domino-brix-client/src/main/java/org/dominokit/brix/security/DefaultAuthorizer.java index 0db20f2..955f13c 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/security/DefaultAuthorizer.java +++ b/domino-brix-client/src/main/java/org/dominokit/brix/security/DefaultAuthorizer.java @@ -15,12 +15,14 @@ */ package org.dominokit.brix.security; +import org.dominokit.brix.events.HasRoles; + public class DefaultAuthorizer implements Authorizer { public static final Authorizer INSTANCE = new DefaultAuthorizer(); @Override - public boolean isAuthorized(SecurityContext context, HasRoles hasRoles) { + public boolean isAuthorized(IsSecurityContext context, HasRoles hasRoles) { return true; } } diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/security/DenyAllAuthorizer.java b/domino-brix-client/src/main/java/org/dominokit/brix/security/DenyAllAuthorizer.java index 76f23df..30ec832 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/security/DenyAllAuthorizer.java +++ b/domino-brix-client/src/main/java/org/dominokit/brix/security/DenyAllAuthorizer.java @@ -15,11 +15,13 @@ */ package org.dominokit.brix.security; +import org.dominokit.brix.events.HasRoles; + public class DenyAllAuthorizer implements Authorizer { public static final Authorizer INSTANCE = new DenyAllAuthorizer(); @Override - public boolean isAuthorized(SecurityContext context, HasRoles hasRoles) { + public boolean isAuthorized(IsSecurityContext context, HasRoles hasRoles) { return false; } } diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/security/IsSecurityContext.java b/domino-brix-client/src/main/java/org/dominokit/brix/security/IsSecurityContext.java new file mode 100644 index 0000000..c082e75 --- /dev/null +++ b/domino-brix-client/src/main/java/org/dominokit/brix/security/IsSecurityContext.java @@ -0,0 +1,36 @@ +/* + * Copyright © 2019 Dominokit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dominokit.brix.security; + +import java.util.Collection; +import java.util.Optional; +import org.dominokit.brix.events.BrixUser; + +public interface IsSecurityContext { + Optional getUser(); + + boolean isAuthorizedFor(String role); + + boolean isAuthorizedForAll(String... roles); + + void reportUnAuthorizedAccess(); + + boolean isAuthorizedForAll(Collection roles); + + boolean isAuthorizedForAny(String... roles); + + boolean isAuthorizedForAny(Collection roles); +} diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/security/PermitAllAuthorizer.java b/domino-brix-client/src/main/java/org/dominokit/brix/security/PermitAllAuthorizer.java index 6a372a5..b266717 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/security/PermitAllAuthorizer.java +++ b/domino-brix-client/src/main/java/org/dominokit/brix/security/PermitAllAuthorizer.java @@ -15,11 +15,14 @@ */ package org.dominokit.brix.security; +import org.dominokit.brix.events.BrixUser; +import org.dominokit.brix.events.HasRoles; + public class PermitAllAuthorizer implements Authorizer { public static final Authorizer INSTANCE = new PermitAllAuthorizer(); @Override - public boolean isAuthorized(SecurityContext context, HasRoles hasRoles) { + public boolean isAuthorized(IsSecurityContext context, HasRoles hasRoles) { return context.getUser().map(BrixUser::isAuthenticated).orElse(false); } } diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/security/RolesAllowedAuthorizer.java b/domino-brix-client/src/main/java/org/dominokit/brix/security/RolesAllowedAuthorizer.java index 8938f54..941db03 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/security/RolesAllowedAuthorizer.java +++ b/domino-brix-client/src/main/java/org/dominokit/brix/security/RolesAllowedAuthorizer.java @@ -15,11 +15,14 @@ */ package org.dominokit.brix.security; +import org.dominokit.brix.events.BrixUser; +import org.dominokit.brix.events.HasRoles; + public class RolesAllowedAuthorizer implements Authorizer { public static final Authorizer INSTANCE = new RolesAllowedAuthorizer(); @Override - public boolean isAuthorized(SecurityContext context, HasRoles hasRoles) { + public boolean isAuthorized(IsSecurityContext context, HasRoles hasRoles) { Boolean authenticated = context.getUser().map(BrixUser::isAuthenticated).orElse(false); return authenticated && context.isAuthorizedForAny(hasRoles.getRoles()); } diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/security/SecurityContext.java b/domino-brix-client/src/main/java/org/dominokit/brix/security/SecurityContext.java index 4917c26..8e37c0c 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/security/SecurityContext.java +++ b/domino-brix-client/src/main/java/org/dominokit/brix/security/SecurityContext.java @@ -22,11 +22,12 @@ import java.util.Optional; import javax.inject.Inject; import javax.inject.Singleton; +import org.dominokit.brix.events.BrixUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Singleton -public class SecurityContext { +public class SecurityContext implements IsSecurityContext { private static final Logger LOGGER = LoggerFactory.getLogger(SecurityContext.class); @@ -48,7 +49,7 @@ public SecurityContext setUser(BrixUser user) { return this; } - public void unauthorizedAccessHandler(Runnable handler) { + public void setUnauthorizedAccessHandler(Runnable handler) { this.unauthorizedAccessHandler = handler; } diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/security/BrixUser.java b/domino-brix-shared/src/main/java/org/dominokit/brix/events/BrixUser.java similarity index 94% rename from domino-brix-client/src/main/java/org/dominokit/brix/security/BrixUser.java rename to domino-brix-shared/src/main/java/org/dominokit/brix/events/BrixUser.java index 3e19153..357e7f5 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/security/BrixUser.java +++ b/domino-brix-shared/src/main/java/org/dominokit/brix/events/BrixUser.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.dominokit.brix.security; +package org.dominokit.brix.events; public interface BrixUser extends HasRoles { boolean isAuthenticated(); diff --git a/domino-brix-client/src/main/java/org/dominokit/brix/security/HasRoles.java b/domino-brix-shared/src/main/java/org/dominokit/brix/events/HasRoles.java similarity index 94% rename from domino-brix-client/src/main/java/org/dominokit/brix/security/HasRoles.java rename to domino-brix-shared/src/main/java/org/dominokit/brix/events/HasRoles.java index 6848d28..0f221fe 100644 --- a/domino-brix-client/src/main/java/org/dominokit/brix/security/HasRoles.java +++ b/domino-brix-shared/src/main/java/org/dominokit/brix/events/HasRoles.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.dominokit.brix.security; +package org.dominokit.brix.events; import java.util.Set;