From f5d8522e4717ad36e7be3fa0c20868c0a263f641 Mon Sep 17 00:00:00 2001 From: lucio Date: Thu, 22 Feb 2024 09:56:36 +0100 Subject: [PATCH] added authorized method --- pom.xml | 40 ++++++++++++++++++- .../gcube/service/helloworld/HelloWorld.java | 2 + .../services/AuthorizedMethods.java | 26 ++++++++++++ .../helloworld/services/HelloService.java | 4 +- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/gcube/service/helloworld/services/AuthorizedMethods.java diff --git a/pom.xml b/pom.xml index 9f104f1..e69c2f8 100644 --- a/pom.xml +++ b/pom.xml @@ -11,9 +11,11 @@ Smartgears HelloWorld Service war + 11 + 1.14.0 UTF-8 - 11 - 11 + ${java.version} + ${java.version} @@ -39,6 +41,14 @@ org.gcube.core common-smartgears + + org.gcube.common + common-security + + + org.gcube.common + authorization-control-library + org.gcube.core common-smartgears-app @@ -74,5 +84,31 @@ ${project.artifactId} + + + + org.codehaus.mojo + aspectj-maven-plugin + ${aspectj-plugin.version} + + 11 + 11 + 11 + + + org.gcube.common + authorization-control-library + + + + + + + compile + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/service/helloworld/HelloWorld.java b/src/main/java/org/gcube/service/helloworld/HelloWorld.java index 3321ec0..e463df0 100644 --- a/src/main/java/org/gcube/service/helloworld/HelloWorld.java +++ b/src/main/java/org/gcube/service/helloworld/HelloWorld.java @@ -6,6 +6,7 @@ import java.util.Set; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; +import org.gcube.service.helloworld.services.AuthorizedMethods; import org.gcube.service.helloworld.services.HelloService; @@ -16,6 +17,7 @@ public class HelloWorld extends Application { final Set> classes = new HashSet>(); // register resources classes implementing Servlets classes.add(HelloService.class); + classes.add(AuthorizedMethods.class); return classes; } } diff --git a/src/main/java/org/gcube/service/helloworld/services/AuthorizedMethods.java b/src/main/java/org/gcube/service/helloworld/services/AuthorizedMethods.java new file mode 100644 index 0000000..401b345 --- /dev/null +++ b/src/main/java/org/gcube/service/helloworld/services/AuthorizedMethods.java @@ -0,0 +1,26 @@ +package org.gcube.service.helloworld.services; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +import org.gcube.common.authorization.control.annotations.AuthorizationControl; +import org.gcube.common.security.providers.SecretManagerProvider; +import org.gcube.common.security.secrets.Secret; +import org.gcube.smartgears.utils.InnerMethodName; + +@Path("auth") +public class AuthorizedMethods { + + private static final String ALLOWED_ROLE = "myRole"; + + @AuthorizationControl(allowedRoles={ALLOWED_ROLE}) + @GET + public String authorized() { + InnerMethodName.set("auth"); + Secret secret = SecretManagerProvider.get(); + String userId = secret.getOwner().getId(); + String context = secret.getContext(); + return String.format("User %s in context %s is authorized to execute this method because he has the correct role", userId,context); + } + +} diff --git a/src/main/java/org/gcube/service/helloworld/services/HelloService.java b/src/main/java/org/gcube/service/helloworld/services/HelloService.java index 275e5d4..cdea7a4 100644 --- a/src/main/java/org/gcube/service/helloworld/services/HelloService.java +++ b/src/main/java/org/gcube/service/helloworld/services/HelloService.java @@ -7,6 +7,7 @@ import javax.ws.rs.core.MediaType; import org.gcube.common.security.providers.SecretManagerProvider; import org.gcube.common.security.secrets.Secret; +import org.gcube.smartgears.utils.InnerMethodName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,7 +19,8 @@ public class HelloService { @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { - Secret secret = SecretManagerProvider.instance.get(); + InnerMethodName.set("hello"); + Secret secret = SecretManagerProvider.get(); String userId = secret.getOwner().getId(); String context = secret.getContext(); logger.info("caller id is {}",userId);