integrated idm-client library. exposed first users rest api to test the client

This commit is contained in:
Alfredo Oliviero 2024-05-21 17:57:51 +02:00
parent 3e228a5df5
commit fe44723739
8 changed files with 239 additions and 15 deletions

15
pom.xml
View File

@ -220,6 +220,21 @@
<artifactId>jersey-cdi2-se</artifactId>
</dependency>
<!-- idm client -->
<dependency>
<groupId>org.gcube.idm</groupId>
<artifactId>idm-common-library</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.gcube.idm</groupId>
<artifactId>idm-client-library</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
</dependencies>
<build>

View File

@ -2,11 +2,15 @@ package org.gcube.social_networking;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.idm.client.IdmClientFactory;
import org.gcube.idm.common.is.IsServerConfig;
import org.gcube.smartgears.ApplicationManager;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.configuration.Mode;
import org.gcube.social_networking.utils.InfrastructureUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Ahmed Ibrahim
*/
@ -18,7 +22,16 @@ public class SocialServiceApplicationManager implements ApplicationManager {
*/
@Override
public void onInit() {
Secret isSecret = InfrastructureUtils.getSecretForInfrastructure();
IdmClientFactory factory = IdmClientFactory.getSingleton();
factory.setSecret(isSecret);
String baseurl = "http://146.48.85.179:9999/idm-service/";
IsServerConfig config = new IsServerConfig(baseurl);
factory.setConfig(config);
if (ContextProvider.get().container().configuration().mode() == Mode.offline) {
logger.debug("init called in offline mode");
} else {
Secret secret = SecretManagerProvider.get();

View File

@ -12,6 +12,7 @@ import org.gcube.idm.common.is.InfrastrctureServiceClient;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.social_networking.liferay.ws.GroupManagerWSBuilder;
import org.gcube.social_networking.utils.InfrastructureUtils;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.VirtualGroup;
@ -220,7 +221,7 @@ public class SocialNetworkingSiteFinder {
// String END_POINT_NAME = "d4science";
// boolean IS_ROOT_SERVICE = true;
Secret secret = InfrastrctureServiceClient.getSecretForInfrastructure();
Secret secret = InfrastructureUtils.getSecretForInfrastructure();
List<ServiceEndpoint> resources = InfrastrctureServiceClient.getEndopintsFromIS(gatewayName, CATEGORY, true, secret);
ServiceEndpoint serviceEndpoint = resources.get(0);
String host = "https://" + serviceEndpoint.profile().runtime().hostedOn();

View File

@ -12,6 +12,7 @@ import org.gcube.idm.common.is.InfrastrctureServiceClient;
import org.gcube.idm.common.is.IsServerConfig;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.social_networking.utils.InfrastructureUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -75,7 +76,7 @@ public class LiferayJSONWsCredentials {
* Read the properties from the infrastructure
*/
private void lookupPropertiesFromIs() {
Secret secret = InfrastrctureServiceClient.getSecretForInfrastructure();
Secret secret = InfrastructureUtils.getSecretForInfrastructure();
IsServerConfig cfg;
try {

View File

@ -0,0 +1,168 @@
package org.gcube.social_networking.rest;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.List;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.idm.client.IdmClientFactory;
import org.gcube.idm.client.IdmMeClient;
import org.gcube.idm.client.IdmUsersClient;
import org.gcube.idm.client.SearchUsersParams;
import org.gcube.idm.client.model.UserInfo;
import org.gcube.idm.common.models.IdmFullUser;
import org.gcube.idm.common.models.IdmUser;
import org.gcube.social_networking.utils.InfrastructureUtils;
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
@Path("/idm")
@RequestHeaders({
@RequestHeader(name = "Authorization", description = "Bearer token, see <a href=\"https://dev.d4science.org/how-to-access-resources\">https://dev.d4science.org/how-to-access-resources</a>")
})
public class IdmRest {
@GET
@Path("/me")
@Produces({ MediaType.APPLICATION_JSON })
public Response test() throws ServerException {
IdmClientFactory factory = IdmClientFactory.getSingleton();
String token = InfrastructureUtils.getAccessToken();
IdmMeClient client = factory.meClient(token);
UserInfo user = client.getMe();
HashMap<String, Object> resp = new HashMap<String, Object>();
resp.put("result", user);
try {
String jsonData = new ObjectMapper().writeValueAsString(resp);
return Response.ok(jsonData).build();
} catch (JsonProcessingException e) {
e.printStackTrace();
return Response.serverError().build();
}
}
@GET
@Path("/{username}/")
@Produces({ MediaType.APPLICATION_JSON })
public Response getUser(
@PathParam("username") String username) throws ServerException {
IdmClientFactory factory = IdmClientFactory.getSingleton();
String token = InfrastructureUtils.getAccessToken();
IdmUsersClient client = factory.userClient(token);
UserInfo user = client.getUser(username);
HashMap<String, Object> resp = new HashMap<String, Object>();
resp.put("result", user);
try {
String jsonData = new ObjectMapper().writeValueAsString(resp);
return Response.ok(jsonData).build();
} catch (JsonProcessingException e) {
e.printStackTrace();
return Response.serverError().build();
}
}
@GET
@Path("/{search}/")
@Produces({ MediaType.APPLICATION_JSON })
public Response searchUsers(
@QueryParam("format") @DefaultValue("username") IdmUser.USERS_REPR format,
@QueryParam("exact") @DefaultValue("true") Boolean exact,
@QueryParam("username") String username,
@QueryParam("firstName") String firstName,
@QueryParam("lastName") String lastName,
@QueryParam("email") String email,
@QueryParam("first") @DefaultValue("0") int firstResult,
@QueryParam("max") @DefaultValue("100") int maxResults,
@QueryParam("enabled") @DefaultValue("true") Boolean enabled
) throws ServerException {
IdmClientFactory factory = IdmClientFactory.getSingleton();
String token = InfrastructureUtils.getAccessToken();
IdmUsersClient client = factory.userClient(token);
SearchUsersParams params = new SearchUsersParams();
if (exact != null)
params.exact = exact;
if (username != null)
params.username = username;
if (firstName != null)
params.firstName = firstName;
if (lastName != null)
params.lastName = lastName;
if (email != null)
params.email = email;
if (enabled != null)
params.enabled = enabled;
Object resp = null;
if (format.equals(IdmUser.USERS_REPR.compact)) {
List<IdmUser> users = client.searchUsers(firstResult, maxResults, params);
resp = users;
}
if (format.equals(IdmUser.USERS_REPR.full)) {
List<IdmFullUser> users = client.searchFullUsers(firstResult, maxResults, params);
resp = users;
}
if (format.equals(IdmUser.USERS_REPR.username)) {
List<String> users = client.searchUsernames(firstResult, maxResults, params);
resp = users;
}
if (format.equals(IdmUser.USERS_REPR.email)) {
List<String> users = client.searchEmails(firstResult, maxResults, params);
resp = users;
}
try
{
String jsonData = new ObjectMapper().writeValueAsString(resp);
return Response.ok(jsonData).build();
} catch (JsonProcessingException e) {
e.printStackTrace();
return Response.serverError().build();
}
}
}

View File

@ -1,27 +1,18 @@
package org.gcube.social_networking.rest;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import java.util.HashMap;
import java.util.Map;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.security.Owner;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.context.container.ContainerContext;
import org.gcube.smartgears.utils.InnerMethodName;
import org.gcube.social_networking.rest.examples.serializers.IdmObjectSerializator;
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
import com.webcohesion.enunciate.metadata.rs.ResourceGroup;
import com.webcohesion.enunciate.metadata.rs.ResourceLabel;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

View File

@ -9,7 +9,6 @@ import org.gcube.common.security.Owner;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.context.container.ContainerContext;
import org.gcube.smartgears.utils.InnerMethodName;
import org.gcube.social_networking.rest.examples.serializers.IdmObjectSerializator;
import org.slf4j.Logger;

View File

@ -0,0 +1,36 @@
package org.gcube.social_networking.utils;
import java.util.Map;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.configuration.container.ContainerConfiguration;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.context.container.ContainerContext;
public class InfrastructureUtils {
/**
* Retrieve the secret from smartgear configuration
*
* @return Secret
*/
public static Secret getSecretForInfrastructure() {
ApplicationContext ctx = ContextProvider.get();
ContainerContext container = ctx.container();
ContainerConfiguration configuration = container.configuration();
String infra_context = configuration.infrastructure();
Secret secret = ctx.container().authorizationProvider().getSecretForContext(infra_context);
return secret;
}
public static String getAccessToken() {
Map<String, String> authorizations = SecretManagerProvider.get().getHTTPAuthorizationHeaders();
String access_token = authorizations.get("Authorization").replace("Bearer", "").trim();
return access_token;
}
}