From c5e9810fc035e6bf826d1e78be1e488cfddc293e Mon Sep 17 00:00:00 2001 From: Alfredo Oliviero Date: Tue, 27 Feb 2024 10:51:58 +0100 Subject: [PATCH] reorganized packages --- .../org/gcube/service/idm/rest/UsersRest.java | 259 +++++++++--------- .../test}/AuthenticatedService.java | 7 +- .../test}/ExcludeAuthorizationService.java | 0 .../services => rest/test}/HelloService.java | 9 +- 4 files changed, 137 insertions(+), 138 deletions(-) rename src/main/java/org/gcube/service/idm/{test/services => rest/test}/AuthenticatedService.java (90%) rename src/main/java/org/gcube/service/idm/{test/services => rest/test}/ExcludeAuthorizationService.java (100%) rename src/main/java/org/gcube/service/idm/{test/services => rest/test}/HelloService.java (86%) diff --git a/src/main/java/org/gcube/service/idm/rest/UsersRest.java b/src/main/java/org/gcube/service/idm/rest/UsersRest.java index 0c82769..aebb9c1 100644 --- a/src/main/java/org/gcube/service/idm/rest/UsersRest.java +++ b/src/main/java/org/gcube/service/idm/rest/UsersRest.java @@ -1,150 +1,139 @@ package org.gcube.service.idm.rest; import org.gcube.common.authorization.library.policies.Users; +import org.gcube.common.security.providers.SecretManagerProvider; +import org.gcube.service.rest.ResponseBean; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + // @Path("2/users") // @ResourceGroup("Users APIs") // @ResourceLabel("Greetings APIs") // @RequestHeaders({ -// @RequestHeader(name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources") +// @RequestHeader(name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources") // }) public class UsersRest { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Users.class); - /* - * // @GET - * // @Path("/get-usernames-by-role") - * // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" - * }) - * public Response getUsernamesByRole( - * - * @QueryParam("role-name") String roleName) { - * Status status = Status.OK; - * ResponseBean responseBean = new ResponseBean(); - * - * List usernames = new ArrayList(); - * try { - * String ctx = SecretManagerProvider.get().getContext(); - * KeycloakApiClient keycloackApiClient = - * KeycloakAPIFactory.getSingleton().createtKeycloakInstance(ctx); - * - * List users = null; // searchByRole(keycloackApiClient, - * roleName); - * if (users != null) { - * for (UserRepresentation user : users) { - * usernames.add(user.getUsername()); - * } - * } - * responseBean.setResult(usernames); - * responseBean.setSuccess(true); - * } catch (Exception e) { - * logger.error("Unable to retrieve user with the requested role", e); - * responseBean.setMessage(e.getMessage()); - * status = Status.INTERNAL_SERVER_ERROR; - * } - * - * return Response.status(status).entity(responseBean).build(); - * - * } - * - * private static List searchByRole(KeycloackApiClient - * keycloackApiClient, String roleName) { - * logger.info("Searching by role: {}", roleName); - * - * List clients = - * keycloackApiClient.kclient.realm(keycloackApiClient.realmName) - * .clients().findByClientId(keycloackApiClient.clientIdContext); - * - * String id = ""; - * for (ClientRepresentation client : clients) { - * logger.info("found client =" + client.getClientId()); - * logger.info("found client id=" + client.getId()); - * id = client.getId(); - * } - * - * List users = - * keycloackApiClient.kclient.realm(keycloackApiClient.realmName) - * .clients() - * .get(id).roles().get(roleName) - * .getUserMembers(0, 100000); - * return users; - * } - * - * @GET - * - * @Path("/{get-profile}") - * - * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - * public String getCurrentProfile() { - * // SMARTGEARS Specializza il tracciamento della chiamata su Accounting - * InnerMethodName.instance.set("getCurrentProfile"); - * Owner owner = SecretManagerProvider.get().getOwner(); - * - * ApplicationContext appContext = ContextProvider.get(); - * SimpleCredentials credentials = ((DefaultAuthorizationProvider) - * appContext.container().authorizationProvider()) - * .getCredentials(); - * - * String ctx = SecretManagerProvider.get().getContext(); - * KeycloackApiClient keycloackApiClient = - * KeycloakAPIFactory.getSingleton().createtKeycloakInstance(ctx); - * return null; - * } - * - * @GET - * - * @Path("/{get-email}") - * - * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - * public String getCurrentEmail() { - * throw new NotImplementedYetException(); - * } - * - * @GET - * - * @Path("/{get-fullname}") - * - * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - * public String getCurrentFullname() { - * throw new NotImplementedYetException(); - * } - * - * @GET - * - * @Path("/{get-all-usernames}") - * - * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - * public String getAllUsernames() { - * throw new NotImplementedYetException(); - * } - * - * @GET - * - * @Path("/{get-all-fullnames-and-usernames}") - * - * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - * public String getAllUsernamesFullnames() { - * throw new NotImplementedYetException(); - * } - * - * @GET - * - * @Path("/{user-exists}") - * - * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - * public boolean checkUserExists() { - * throw new NotImplementedYetException(); - * } - * - * @GET - * - * @Path("/{get-oauth-profile}") - * - * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - * public boolean getCurrentOAuthProfile() { - * throw new NotImplementedYetException(); - * } - */ + // @GET + // @Path("/get-usernames-by-role") + // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + // public Response getUsernamesByRole( + // @QueryParam("role-name") String roleName) { + // Status status = Status.OK; + // ResponseBean responseBean = new ResponseBean(); + + // List usernames = new ArrayList(); + // try { + // String ctx = SecretManagerProvider.get().getContext(); + // KeycloakApiClient keycloackApiClient = KeycloakAPIFactory.getSingleton().createtKeycloakInstance(ctx); + + // List users = null; // searchByRole(keycloackApiClient, roleName); + // if (users != null) { + // for (UserRepresentation user : users) { + // usernames.add(user.getUsername()); + // } + // } + // responseBean.setResult(usernames); + // responseBean.setSuccess(true); + // } catch (Exception e) { + // logger.error("Unable to retrieve user with the requested role", e); + // responseBean.setMessage(e.getMessage()); + // status = Status.INTERNAL_SERVER_ERROR; + // } + + // return Response.status(status).entity(responseBean).build(); + + // } + + // private static List searchByRole(KeycloackApiClient keycloackApiClient, String roleName) { + // logger.info("Searching by role: {}", roleName); + + // List clients = keycloackApiClient.kclient.realm(keycloackApiClient.realmName) + // .clients().findByClientId(keycloackApiClient.clientIdContext); + + // String id = ""; + // for (ClientRepresentation client : clients) { + // logger.info("found client =" + client.getClientId()); + // logger.info("found client id=" + client.getId()); + // id = client.getId(); + // } + + // List users = keycloackApiClient.kclient.realm(keycloackApiClient.realmName) + // .clients() + // .get(id).roles().get(roleName) + // .getUserMembers(0, 100000); + // return users; + // } + + // @GET + // @Path("/{get-profile}") + // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + // public String getCurrentProfile() { + // // SMARTGEARS Specializza il tracciamento della chiamata su Accounting + // InnerMethodName.instance.set("getCurrentProfile"); + // Owner owner = SecretManagerProvider.get().getOwner(); + + // ApplicationContext appContext = ContextProvider.get(); + // SimpleCredentials credentials = ((DefaultAuthorizationProvider) appContext.container().authorizationProvider()) + // .getCredentials(); + + // String ctx = SecretManagerProvider.get().getContext(); + // KeycloackApiClient keycloackApiClient = KeycloakAPIFactory.getSingleton().createtKeycloakInstance(ctx); + // return null; + // } + + // @GET + // @Path("/{get-email}") + // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + // public String getCurrentEmail() { + // throw new NotImplementedYetException(); + // } + + // @GET + // @Path("/{get-fullname}") + // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + // public String getCurrentFullname() { + // throw new NotImplementedYetException(); + // } + + // @GET + // @Path("/{get-all-usernames}") + // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + // public String getAllUsernames() { + // throw new NotImplementedYetException(); + // } + + // @GET + // @Path("/{get-all-fullnames-and-usernames}") + // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + // public String getAllUsernamesFullnames() { + // throw new NotImplementedYetException(); + // } + + // @GET + // @Path("/{user-exists}") + // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + // public boolean checkUserExists() { + // throw new NotImplementedYetException(); + // } + + // @GET + // @Path("/{get-oauth-profile}") + // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + // public boolean getCurrentOAuthProfile() { + // throw new NotImplementedYetException(); + // } } diff --git a/src/main/java/org/gcube/service/idm/test/services/AuthenticatedService.java b/src/main/java/org/gcube/service/idm/rest/test/AuthenticatedService.java similarity index 90% rename from src/main/java/org/gcube/service/idm/test/services/AuthenticatedService.java rename to src/main/java/org/gcube/service/idm/rest/test/AuthenticatedService.java index a804668..c47ea8a 100644 --- a/src/main/java/org/gcube/service/idm/test/services/AuthenticatedService.java +++ b/src/main/java/org/gcube/service/idm/rest/test/AuthenticatedService.java @@ -1,4 +1,4 @@ -package org.gcube.service.idm.test.services; +package org.gcube.service.idm.rest.test; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -8,7 +8,10 @@ import javax.ws.rs.core.MediaType; 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.service.idm.rest.TestRest; import org.gcube.smartgears.utils.InnerMethodName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author Lucio Le (ISTI - CNR) @@ -18,6 +21,8 @@ import org.gcube.smartgears.utils.InnerMethodName; @Path("auth") public class AuthenticatedService { + private final Logger logger = LoggerFactory.getLogger(AuthenticatedService.class); + private static final String ALLOWED_ROLE = "myRole"; private static final String ALLOWED_ROLE_ORG = "OrganizationMember"; private static final String ALLOWED_ROLE_MEMBER = "Member"; diff --git a/src/main/java/org/gcube/service/idm/test/services/ExcludeAuthorizationService.java b/src/main/java/org/gcube/service/idm/rest/test/ExcludeAuthorizationService.java similarity index 100% rename from src/main/java/org/gcube/service/idm/test/services/ExcludeAuthorizationService.java rename to src/main/java/org/gcube/service/idm/rest/test/ExcludeAuthorizationService.java diff --git a/src/main/java/org/gcube/service/idm/test/services/HelloService.java b/src/main/java/org/gcube/service/idm/rest/test/HelloService.java similarity index 86% rename from src/main/java/org/gcube/service/idm/test/services/HelloService.java rename to src/main/java/org/gcube/service/idm/rest/test/HelloService.java index 46965e6..65eafdf 100644 --- a/src/main/java/org/gcube/service/idm/test/services/HelloService.java +++ b/src/main/java/org/gcube/service/idm/rest/test/HelloService.java @@ -1,4 +1,4 @@ -package org.gcube.service.idm.test.services; +package org.gcube.service.idm.rest.test; import java.util.HashMap; @@ -69,7 +69,12 @@ public class HelloService { Owner owner = secret.getOwner(); data.put("owner", owner); - ContainerContext container = ContextProvider.get().container(); + logger.info("details for {} - {} {} - {}", owner.getId(), owner.getFirstName(), owner.getLastName(), owner.getEmail()); + + ApplicationContext contextProvider = ContextProvider.get(); + ContainerContext container = contextProvider.container(); + + //ContainerContext container = ContextProvider.get().container(); data.put("container", container); ObjectMapper objectMapper = ContextSerializator.getSerializer();