idm client implementation
This commit is contained in:
parent
67716e8a5a
commit
09ffcbbe7b
|
@ -0,0 +1,37 @@
|
|||
package org.gcube.idm.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.gcube.idm.client.clients.IdmRestClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class AbstractIdmClient {
|
||||
protected static Logger logger = LoggerFactory.getLogger(AbstractIdmClient.class);
|
||||
|
||||
protected final static String AUTHORIZATION_HEADER = "Authorization";
|
||||
protected String auth_token;
|
||||
|
||||
protected IdmRestClient client;
|
||||
|
||||
public IdmRestClient getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
public void setClient(IdmRestClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public AbstractIdmClient(IdmRestClient client) throws URISyntaxException {
|
||||
this.setClient(client);
|
||||
}
|
||||
|
||||
public AbstractIdmClient(String base_url) throws URISyntaxException {
|
||||
this.setClient(new IdmRestClient(base_url));
|
||||
}
|
||||
|
||||
public AbstractIdmClient(URI base_url) {
|
||||
this.setClient(new IdmRestClient(base_url));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package org.gcube.idm.client;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Constants {
|
||||
|
||||
/** Service name. */
|
||||
public static final String SERVICE_NAME = "IDM";
|
||||
|
||||
/** Service class. */
|
||||
public static final String SERVICE_CLASS = "identity-manager";
|
||||
|
||||
public static final int DEFAULT_TIMEOUT= (int) TimeUnit.SECONDS.toMillis(10);
|
||||
|
||||
// //TODO: cosa è? verificare
|
||||
// public static final String NAMESPACE = "http://gcube-system.org/namespaces/common/idm";
|
||||
|
||||
// //TODO: cosa è? verificare
|
||||
// public static final QName MANAGER_QNAME = new QName(NAMESPACE, "itemmanager");
|
||||
|
||||
|
||||
/*
|
||||
public static final GcubeService<ManagerStubs> manager = service().withName(org.gcube.data.spd.model.service.Constants.manager_name).andInterface(ManagerStubs.class);
|
||||
|
||||
public static final GcubeService<ClassificationStubs> classification = service().withName(org.gcube.data.spd.model.service.Constants.classification_name).andInterface(ClassificationStubs.class);
|
||||
|
||||
public static final GcubeService<ExecutorStubs> executor = service().withName(org.gcube.data.spd.model.service.Constants.executor_name).andInterface(ExecutorStubs.class);
|
||||
|
||||
public static final GcubeService<OccurrenceStubs> occurrence = service().withName(org.gcube.data.spd.model.service.Constants.occurrence_name).andInterface(OccurrenceStubs.class);
|
||||
|
||||
private static final GcubeService<RemoteDispatcher> remoteDispatcher = service().withName(org.gcube.data.spd.model.service.Constants.remoteDispatcher_name).andInterface(RemoteDispatcher.class);
|
||||
|
||||
public static final RemoteDispatcher getRemoteDispatcherService(String address){
|
||||
return stubFor(remoteDispatcher).at(address);
|
||||
}*/
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package org.gcube.idm.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.gcube.idm.client.beans.ResponseBean;
|
||||
import org.gcube.idm.client.clients.IdmRestClient;
|
||||
import org.gcube.idm.client.model.OwnerInfo;
|
||||
import org.gcube.idm.client.model.UserProfile;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DefaultIdmMeClient extends AbstractIdmClient implements IdmMeClient {
|
||||
protected String auth_token;
|
||||
|
||||
public DefaultIdmMeClient(IdmRestClient client, String auth_token) throws URISyntaxException {
|
||||
super(client);
|
||||
this.auth_token = auth_token;
|
||||
}
|
||||
|
||||
public DefaultIdmMeClient(String base_url, String auth_token) throws URISyntaxException {
|
||||
super(base_url);
|
||||
this.auth_token = auth_token;
|
||||
}
|
||||
|
||||
public DefaultIdmMeClient(URI base_url, String auth_token) throws URISyntaxException {
|
||||
super(base_url);
|
||||
this.auth_token = auth_token;
|
||||
}
|
||||
|
||||
protected static Logger logger = LoggerFactory.getLogger(DefaultIdmMeClient.class);
|
||||
|
||||
@Override
|
||||
public String getMeId() {
|
||||
ResponseBean<String> resp = this.client.performGetRequest("users/me/id", auth_token, String.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMeEmail() {
|
||||
ResponseBean<String> resp = this.client.performGetRequest("users/me/email", auth_token, String.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMeUsername() {
|
||||
ResponseBean<String> resp = this.client.performGetRequest("users/me/username", auth_token, String.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OwnerInfo getMeOwner() {
|
||||
ResponseBean<OwnerInfo> resp = this.client.performGetRequest("users/me/owner", auth_token, OwnerInfo.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserProfile getMeProfile() {
|
||||
ResponseBean<UserProfile> resp = this.client.performGetRequest("users/me/profile", auth_token, UserProfile.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object verifyToken() {
|
||||
ResponseBean<String> resp = this.client.performGetRequest("users/me/id", auth_token, String.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,163 @@
|
|||
package org.gcube.idm.client;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.databind.JavaType;
|
||||
import org.gcube.common.keycloak.model.UserInfo;
|
||||
import org.gcube.idm.client.beans.ResponseBean;
|
||||
import org.gcube.idm.client.clients.IdmRestClient;
|
||||
import org.gcube.idm.client.model.UserProfile;
|
||||
import org.gcube.idm.client.model.util.JsonUtilsCustom;
|
||||
import org.gcube.idm.common.models.IdmFullUser;
|
||||
import org.gcube.idm.common.models.IdmUser;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.ws.rs.BadRequestException;
|
||||
|
||||
public class DefaultUsersClient extends AbstractIdmClient implements IdmUsersClient {
|
||||
protected String auth_token;
|
||||
|
||||
public String pathForUserID(String user_id, String extra_path) {
|
||||
try {
|
||||
user_id = URLEncoder.encode(user_id, StandardCharsets.UTF_8.toString());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
throw new BadRequestException("cannot encode the user_id " + user_id);
|
||||
}
|
||||
if (extra_path != null) {
|
||||
return String.format("/users/%s/%s", user_id, extra_path);
|
||||
}
|
||||
return String.format("/users/%s", user_id);
|
||||
}
|
||||
|
||||
public DefaultUsersClient(IdmRestClient client, String auth_token) throws URISyntaxException {
|
||||
super(client);
|
||||
this.auth_token = auth_token;
|
||||
}
|
||||
|
||||
public DefaultUsersClient(String base_url, String auth_token) throws URISyntaxException {
|
||||
super(base_url);
|
||||
this.auth_token = auth_token;
|
||||
}
|
||||
|
||||
public DefaultUsersClient(URI base_url, String auth_token) throws URISyntaxException {
|
||||
super(base_url);
|
||||
this.auth_token = auth_token;
|
||||
}
|
||||
|
||||
protected static Logger logger = LoggerFactory.getLogger(DefaultUsersClient.class);
|
||||
|
||||
@Override
|
||||
public String getUserId(String user_id) {
|
||||
ResponseBean<String> resp = this.client.performGetRequest(pathForUserID(user_id, "id"), this.auth_token,
|
||||
String.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUserEmail(String user_id) {
|
||||
ResponseBean<String> resp = this.client.performGetRequest(pathForUserID(user_id, "email"), this.auth_token,
|
||||
String.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUserUsername(String user_id) {
|
||||
ResponseBean<String> resp = this.client.performGetRequest(pathForUserID(user_id, "username"), this.auth_token,
|
||||
String.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdmUser getUserOwner(String user_id) {
|
||||
ResponseBean<IdmUser> resp = this.client.performGetRequest(pathForUserID(user_id, "owner"), this.auth_token,
|
||||
IdmUser.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserProfile getUserProfile(String user_id) {
|
||||
ResponseBean<UserProfile> resp = this.client.performGetRequest(pathForUserID(user_id, "profile"),
|
||||
this.auth_token,
|
||||
UserProfile.class);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAllUsernames() {
|
||||
// TODO Auto-generated method stub
|
||||
return getAllUsernames(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAllUsernames(Integer first, Integer max) {
|
||||
JavaType listType = JsonUtilsCustom.geListOfObjectsType(String.class);
|
||||
ResponseBean<List<String>> resp = this.client.performGetRequest("/users/search", this.auth_token, listType);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
private HashMap<String, String> getSearchParameters(String format, Integer first, Integer max,
|
||||
SearchUsersParams params) {
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
parameters.put("format", format);
|
||||
if (first != null) {
|
||||
parameters.put("first", first.toString());
|
||||
}
|
||||
if (max != null) {
|
||||
parameters.put("max", max.toString());
|
||||
}
|
||||
if (params != null) {
|
||||
if (params.exact != null)
|
||||
parameters.put("exact", params.exact.toString());
|
||||
if (params.enabled != null)
|
||||
parameters.put("enabled", params.enabled.toString());
|
||||
if (params.username != null)
|
||||
parameters.put("username", params.username.toString());
|
||||
if (params.firstName != null)
|
||||
parameters.put("firstName", params.firstName.toString());
|
||||
if (params.lastName != null)
|
||||
parameters.put("lastName", params.lastName.toString());
|
||||
if (params.email != null)
|
||||
parameters.put("email", params.email.toString());
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> searchUsernames(Integer first, Integer max, SearchUsersParams params) {
|
||||
HashMap<String, String> headers = IdmRestClient.getHeadersWithAuth(this.auth_token, null);
|
||||
HashMap<String, String> parameters = getSearchParameters("username", first, max, params);
|
||||
JavaType listType = JsonUtilsCustom.geListOfObjectsType(String.class);
|
||||
ResponseBean<List<String>> resp = this.client.performGetRequest("/users/search", headers, parameters, listType);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IdmUser> searchUsers(Integer first, Integer max, SearchUsersParams params) {
|
||||
HashMap<String, String> headers = IdmRestClient.getHeadersWithAuth(this.auth_token, null);
|
||||
HashMap<String, String> parameters = getSearchParameters("compact", first, max, params);
|
||||
JavaType listType = JsonUtilsCustom.geListOfObjectsType(UserInfo.class);
|
||||
ResponseBean<List<IdmUser>> resp = this.client.performGetRequest("/users/search", headers, parameters,
|
||||
listType);
|
||||
return resp.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IdmFullUser> searchFullUsers(Integer first, Integer max, SearchUsersParams params) {
|
||||
// TODO Auto-generated method stub
|
||||
HashMap<String, String> headers = IdmRestClient.getHeadersWithAuth(this.auth_token, null);
|
||||
HashMap<String, String> parameters = getSearchParameters("full", first, max, params);
|
||||
JavaType listType = JsonUtilsCustom.geListOfObjectsType(IdmFullUser.class);
|
||||
ResponseBean<List<IdmFullUser>> resp = this.client.performGetRequest("/users/search", headers, parameters,
|
||||
listType);
|
||||
return resp.getResult();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,129 @@
|
|||
package org.gcube.idm.client;
|
||||
|
||||
import java.rmi.ServerException;
|
||||
|
||||
import org.gcube.common.security.secrets.Secret;
|
||||
import org.gcube.idm.common.is.InfrastrctureServiceClient;
|
||||
import org.gcube.idm.common.is.IsServerConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.ws.rs.InternalServerErrorException;
|
||||
|
||||
public class IdmClientFactory {
|
||||
|
||||
private final static String RUNTIME_RESOURCE_NAME = "identity-manager";
|
||||
private final static String CATEGORY = "org.gcube.auth";
|
||||
private final static String END_POINT_NAME = "d4science";
|
||||
private final static boolean IS_ROOT_SERVICE = true;
|
||||
|
||||
protected static final Logger logger = LoggerFactory.getLogger(IdmClientFactory.class);
|
||||
|
||||
/**
|
||||
* keycloak configuration obtained from IS in the private constructor
|
||||
* using the singleton pattern, it's retrieved from IS only for the first
|
||||
* access, then kept in the singleton object
|
||||
*/
|
||||
private IsServerConfig config;
|
||||
private Secret secret;
|
||||
|
||||
// the singleton obj
|
||||
|
||||
private static IdmClientFactory singleton = new IdmClientFactory();
|
||||
|
||||
public static IdmClientFactory getSingleton() {
|
||||
if (singleton == null)
|
||||
singleton = new IdmClientFactory();
|
||||
return singleton;
|
||||
}
|
||||
|
||||
public Secret getSecret() {
|
||||
return secret;
|
||||
}
|
||||
|
||||
// set a custom secret, instead of fetch it from IS
|
||||
public void setSecret(Secret secret) {
|
||||
this.secret = secret;
|
||||
this.setConfig(fetchIsConfig(secret));
|
||||
}
|
||||
|
||||
// set a custom config for the factory, skipping or overriding the fetch from IS
|
||||
public void setConfig(IsServerConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public IsServerConfig fetchIsConfig() throws InternalServerErrorException {
|
||||
if (this.secret == null){
|
||||
String error_message = "secret is null. invoke setSecret(secret) before fetching the configuration. ";
|
||||
throw new InternalServerErrorException(error_message);
|
||||
}
|
||||
return fetchIsConfig(this.secret);
|
||||
}
|
||||
|
||||
public IsServerConfig fetchIsConfig(Secret secret) throws InternalServerErrorException {
|
||||
try {
|
||||
//Secret secret = InfrastrctureServiceClient.getSecretForInfrastructure();
|
||||
IsServerConfig cfg = InfrastrctureServiceClient.serviceConfigFromIS(RUNTIME_RESOURCE_NAME, CATEGORY,
|
||||
END_POINT_NAME, IS_ROOT_SERVICE, secret);
|
||||
return cfg;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new InternalServerErrorException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public IdmMeClient meClient(String auth_token) throws ServerException {
|
||||
logger.debug("Instantiating a new IdmClient instance");
|
||||
|
||||
logger.info("Building KeycloakAPICredentials object");
|
||||
try {
|
||||
if (this.config == null) {
|
||||
this.config = fetchIsConfig();
|
||||
}
|
||||
logger.info("KeycloakAPICredentials object built {} - {}", config.getServerUrl(), config.getName());
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("error obtaining IAM configuration from IS {} ", e);
|
||||
throw new ServerException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
String url = null;
|
||||
try {
|
||||
url = this.config.getServerUrl();
|
||||
return new DefaultIdmMeClient(url, auth_token);
|
||||
} catch (Exception e) {
|
||||
logger.error("cannot create idm client with url ", url);
|
||||
|
||||
e.printStackTrace();
|
||||
throw new ServerException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public IdmUsersClient userClient(String auth_token) throws ServerException {
|
||||
logger.debug("Instantiating a new IdmClient instance");
|
||||
|
||||
logger.info("Building KeycloakAPICredentials object");
|
||||
try {
|
||||
if (this.config == null) {
|
||||
this.config = fetchIsConfig();
|
||||
}
|
||||
logger.info("KeycloakAPICredentials object built {} - {}", config.getServerUrl(), config.getName());
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("error obtaining IAM configuration from IS {} ", e);
|
||||
throw new ServerException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
String url = null;
|
||||
try {
|
||||
url = this.config.getServerUrl();
|
||||
return new DefaultUsersClient(url, auth_token);
|
||||
} catch (Exception e) {
|
||||
logger.error("cannot create idm client with url ", url);
|
||||
|
||||
e.printStackTrace();
|
||||
throw new ServerException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package org.gcube.idm.client;
|
||||
|
||||
import org.gcube.idm.client.model.OwnerInfo;
|
||||
import org.gcube.idm.client.model.UserProfile;
|
||||
|
||||
public interface IdmMeClient {
|
||||
|
||||
public String getMeId();
|
||||
public String getMeEmail();
|
||||
public String getMeUsername();
|
||||
public OwnerInfo getMeOwner();
|
||||
public UserProfile getMeProfile();
|
||||
|
||||
public Object verifyToken();
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package org.gcube.idm.client;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.idm.client.model.UserProfile;
|
||||
import org.gcube.idm.common.models.IdmFullUser;
|
||||
import org.gcube.idm.common.models.IdmUser;
|
||||
|
||||
public interface IdmUsersClient {
|
||||
public String getUserId(String user_id);
|
||||
|
||||
public String getUserEmail(String user_id);
|
||||
|
||||
public String getUserUsername(String user_id);
|
||||
|
||||
public IdmUser getUserOwner(String user_id);
|
||||
|
||||
public UserProfile getUserProfile(String user_id);
|
||||
|
||||
public List<String> getAllUsernames();
|
||||
|
||||
public List<String> getAllUsernames(Integer first, Integer max);
|
||||
|
||||
public List<IdmUser> searchUsers(Integer first, Integer max, SearchUsersParams params);
|
||||
|
||||
public List<IdmFullUser> searchFullUsers(Integer first, Integer max, SearchUsersParams params);
|
||||
|
||||
public List<String> searchUsernames(Integer first, Integer max, SearchUsersParams params);
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.gcube.idm.client;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.gcube.idm.client;
|
||||
|
||||
public class SearchUsersParams {
|
||||
public Boolean exact = true;
|
||||
public Boolean enabled;
|
||||
public String username;
|
||||
public String firstName;
|
||||
public String lastName;
|
||||
public String email;
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package org.gcube.idm.client.beans;
|
||||
|
||||
// import org.jboss.weld.util.LazyValueHolder.Serializable;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
/**
|
||||
* Response bean with real result of type T.
|
||||
*
|
||||
* @author Costantino Perciante at ISTI-CNR
|
||||
* (costantino.perciante@isti.cnr.it)
|
||||
*/
|
||||
@JsonIgnoreProperties
|
||||
public class ResponseBean<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2725238162673879658L;
|
||||
|
||||
protected boolean success;
|
||||
protected String message;
|
||||
protected T result;
|
||||
|
||||
public ResponseBean() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param success
|
||||
* @param message
|
||||
* @param result
|
||||
*/
|
||||
public ResponseBean(boolean success, String message, T result) {
|
||||
super();
|
||||
this.success = success;
|
||||
this.message = message;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public T getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(T result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ResponseBean [success=" + success
|
||||
+ ", message=" + message + ", result=" + result + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package org.gcube.idm.client.beans;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
/**
|
||||
* Response bean
|
||||
*
|
||||
*/
|
||||
@JsonIgnoreProperties
|
||||
public class ResponseBeanPaginated<T> extends ResponseBean<T> {
|
||||
|
||||
private static final long serialVersionUID = -2725238162673879658L;
|
||||
|
||||
protected Integer firstResult = null;
|
||||
protected Integer maxResults = null;
|
||||
|
||||
public Integer getFirstResult() {
|
||||
return firstResult;
|
||||
}
|
||||
|
||||
public void setFirstResult(Integer firstResult) {
|
||||
this.firstResult = firstResult;
|
||||
}
|
||||
|
||||
public Integer getMaxResults() {
|
||||
return maxResults;
|
||||
}
|
||||
|
||||
public void setMaxResults(Integer maxResults) {
|
||||
this.maxResults = maxResults;
|
||||
}
|
||||
|
||||
public ResponseBeanPaginated() {
|
||||
super();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @param success
|
||||
// * @param message
|
||||
// * @param result
|
||||
// */
|
||||
// public ResponseBeanPaginated(boolean success, String message, Object result)
|
||||
// {
|
||||
// super(success, message, result);
|
||||
// }
|
||||
|
||||
/**
|
||||
* @param firstResult
|
||||
* @param maxResults
|
||||
*/
|
||||
public ResponseBeanPaginated(Integer firstResult, Integer maxResults) {
|
||||
this.firstResult = firstResult;
|
||||
this.maxResults = maxResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param success
|
||||
* @param message
|
||||
* @param result
|
||||
* @param firstResult
|
||||
* @param maxResults
|
||||
*/
|
||||
public ResponseBeanPaginated(boolean success, String message, T result, Integer firstResult,
|
||||
Integer maxResults) {
|
||||
super(success, message, result);
|
||||
this.firstResult = firstResult;
|
||||
this.maxResults = maxResults;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ResponseBean [success=" + success
|
||||
+ ", message=" + message + ", result=" + result + ", firstResult=" + firstResult + ", maxResults="
|
||||
+ maxResults + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,314 @@
|
|||
package org.gcube.idm.client.clients;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.databind.JavaType;
|
||||
import org.gcube.common.gxhttp.util.ContentUtils;
|
||||
import org.gcube.common.gxrest.request.GXHTTPStringRequest;
|
||||
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
|
||||
import org.gcube.idm.client.beans.ResponseBean;
|
||||
import org.gcube.idm.client.model.util.JsonUtilsCustom;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.ws.rs.BadRequestException;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
|
||||
public class IdmRestClient {
|
||||
protected static Logger logger = LoggerFactory.getLogger(IdmRestClient.class);
|
||||
|
||||
protected final static String AUTHORIZATION_HEADER = "Authorization";
|
||||
|
||||
protected URI BASE_URL = null;
|
||||
|
||||
public URI getBASE_URL() {
|
||||
return BASE_URL;
|
||||
}
|
||||
|
||||
public void setBASE_URL(URI bASE_URL) {
|
||||
BASE_URL = bASE_URL;
|
||||
}
|
||||
|
||||
public IdmRestClient(String base_url) throws URISyntaxException {
|
||||
if (!base_url.endsWith("/")) {
|
||||
base_url += "/";
|
||||
}
|
||||
this.BASE_URL = new URI(base_url);
|
||||
}
|
||||
|
||||
public IdmRestClient(URI base_url) {
|
||||
this.BASE_URL = base_url;
|
||||
}
|
||||
|
||||
public URL getUrl(String extraPath) throws URISyntaxException,
|
||||
MalformedURLException {
|
||||
if (extraPath.startsWith("/")) {
|
||||
extraPath = extraPath.substring(1);
|
||||
}
|
||||
|
||||
return BASE_URL.resolve(extraPath).toURL();
|
||||
}
|
||||
|
||||
public URI getUri(String extraPath) throws URISyntaxException,
|
||||
MalformedURLException {
|
||||
return BASE_URL.resolve(extraPath);
|
||||
}
|
||||
|
||||
public String getBearerAuth(String token) {
|
||||
return "Bearer " + token;
|
||||
}
|
||||
|
||||
public <T> ResponseBean<T> performGetRequest(String relativeUrl, String auth_token, Class<T> classtype) {
|
||||
JavaType objectsType = JsonUtilsCustom.getObjectJT(classtype);
|
||||
return performGetRequest(relativeUrl, auth_token, objectsType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param auth_token
|
||||
* @param headers // null to create a new HashMap
|
||||
* @return
|
||||
*/
|
||||
public static HashMap<String, String> getHeadersWithAuth(String auth_token, HashMap<String, String> headers) {
|
||||
if (headers == null)
|
||||
headers = new HashMap<String, String>();
|
||||
headers.put(AUTHORIZATION_HEADER, "Bearer " + auth_token);
|
||||
return headers;
|
||||
}
|
||||
|
||||
public <T> ResponseBean<T> performGetRequest(String relativeUrl, String auth_token, JavaType objectsType)
|
||||
throws WebApplicationException {
|
||||
HashMap<String, String> headers = null;
|
||||
if (auth_token != null) {
|
||||
headers = new HashMap<String, String>();
|
||||
headers.put(AUTHORIZATION_HEADER, "Bearer " + auth_token);
|
||||
}
|
||||
|
||||
return performGetRequest(relativeUrl, headers, null, objectsType);
|
||||
}
|
||||
|
||||
public static GXHTTPStringRequest preparePostRequest(
|
||||
URL url, Map<String, String> headers,
|
||||
Map<String, List<String>> params) {
|
||||
|
||||
// if (headers == null || !headers.containsKey(AUTHORIZATION_HEADER)
|
||||
// || "".equals(headers.get(AUTHORIZATION_HEADER))) {
|
||||
// throw new NotAuthorizedException("Authorization must be not null nor empty");
|
||||
// }
|
||||
|
||||
// Constructing request object
|
||||
GXHTTPStringRequest request;
|
||||
try {
|
||||
|
||||
String queryString = null;
|
||||
if (params != null) {
|
||||
queryString = params.entrySet().stream()
|
||||
.flatMap(p -> p.getValue().stream().map(v -> p.getKey() + "=" + v))
|
||||
.reduce((p1, p2) -> p1 + "&" + p2).orElse("");
|
||||
}
|
||||
|
||||
logger.trace("Query string is {}", queryString);
|
||||
|
||||
request = GXHTTPStringRequest.newRequest(url.toString())
|
||||
.header("Content-Type", "application/x-www-form-urlencoded").withBody(queryString);
|
||||
|
||||
safeSetAsExternalCallForOldAPI(request);
|
||||
|
||||
logger.trace("Adding provided headers: {}", headers);
|
||||
for (String headerName : headers.keySet()) {
|
||||
request.header(headerName, headers.get(headerName));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new BadRequestException("Cannot construct the request object correctly", e);
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
public static GXHTTPStringRequest prepareGettRequest(
|
||||
URL url, Map<String, String> headers,
|
||||
Map<String, String> params) {
|
||||
|
||||
// if (headers == null || !headers.containsKey(AUTHORIZATION_HEADER)
|
||||
// || "".equals(headers.get(AUTHORIZATION_HEADER))) {
|
||||
// throw new NotAuthorizedException("Authorization must be not null nor empty");
|
||||
// }
|
||||
|
||||
// Constructing request object
|
||||
GXHTTPStringRequest request;
|
||||
try {
|
||||
|
||||
String queryString = null;
|
||||
|
||||
logger.trace("Query string is {}", queryString);
|
||||
|
||||
request = GXHTTPStringRequest.newRequest(url.toString());
|
||||
|
||||
if (params != null) {
|
||||
request.queryParams(params);
|
||||
}
|
||||
|
||||
safeSetAsExternalCallForOldAPI(request);
|
||||
|
||||
logger.trace("Adding provided headers: {}", headers);
|
||||
for (String headerName : headers.keySet()) {
|
||||
request.header(headerName, headers.get(headerName));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new BadRequestException("Cannot construct the request object correctly", e);
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
public <T> ResponseBean<T> performGetRequest(String relativeUrl, Map<String, String> headers,
|
||||
Map<String, String> params, Class<T> classtype) {
|
||||
JavaType jt = JsonUtilsCustom.getObjectJT(classtype);
|
||||
return performGetRequest(relativeUrl, headers, params, jt);
|
||||
}
|
||||
|
||||
public <T> ResponseBean<T> performGetRequest(String relativeUrl, Map<String, String> headers,
|
||||
Map<String, String> params, JavaType objectsType)
|
||||
throws WebApplicationException {
|
||||
if (relativeUrl == null) {
|
||||
throw new BadRequestException("relativeUrl URL must be not null");
|
||||
}
|
||||
|
||||
URL url = null;
|
||||
try {
|
||||
url = getUrl(relativeUrl);
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException("Url not acceptable", e);
|
||||
|
||||
}
|
||||
|
||||
GXHTTPStringRequest request = prepareGettRequest(url, headers, params);
|
||||
GXInboundResponse response;
|
||||
try {
|
||||
response = request.get();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new BadRequestException("Cannot send request correctly", e);
|
||||
}
|
||||
|
||||
try {
|
||||
if (response.isSuccessResponse()) {
|
||||
JavaType rb_type = JsonUtilsCustom.getResponseBeanOfObjectsType(objectsType);
|
||||
return tryConvertStreamedContentFromJson(response, rb_type);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
throw new BadRequestException("Cannot send request correctly", e);
|
||||
}
|
||||
throw new BadRequestException("Cannot send request correctly");
|
||||
|
||||
}
|
||||
|
||||
public <T> ResponseBean<T> performPostRequest(String relativeUrl, Map<String, String> headers,
|
||||
Map<String, List<String>> params, Class<T> classtype)
|
||||
throws WebApplicationException {
|
||||
if (relativeUrl == null) {
|
||||
throw new BadRequestException("relativeUrl URL must be not null");
|
||||
}
|
||||
|
||||
URL url = null;
|
||||
try {
|
||||
url = getUrl(relativeUrl);
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException("Url not acceptable", e);
|
||||
|
||||
}
|
||||
|
||||
GXHTTPStringRequest request = preparePostRequest(url, headers, params);
|
||||
GXInboundResponse response;
|
||||
try {
|
||||
response = request.post();
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException("Cannot send request correctly", e);
|
||||
}
|
||||
|
||||
try {
|
||||
if (response.isSuccessResponse()) {
|
||||
JavaType rb_type = JsonUtilsCustom.getResponseBeansObjectJT(classtype);
|
||||
return tryConvertStreamedContentFromJson(response, rb_type);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
throw new BadRequestException("Cannot send request correctly", e);
|
||||
}
|
||||
throw new BadRequestException("Cannot send request correctly");
|
||||
|
||||
}
|
||||
|
||||
// TODO: coso è?
|
||||
public static void safeSetAsExternalCallForOldAPI(GXHTTPStringRequest request) {
|
||||
try {
|
||||
logger.trace("Looking for the 'isExternalCall' method in the 'GXHTTPStringRequest' class");
|
||||
Method isExetnalCallMethod = request.getClass().getMethod("isExternalCall", boolean.class);
|
||||
logger.trace("Method found, is the old gxJRS API. Invoking it with 'true' argument");
|
||||
isExetnalCallMethod.invoke(request, true);
|
||||
} catch (NoSuchMethodException e) {
|
||||
logger.trace("Method not found, is the new gxJRS API");
|
||||
} catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
logger.warn("Cannot invoke 'isExternalCall' method via reflection on 'GXHTTPStringRequest' class", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the streamed content as a string, if possible.
|
||||
*
|
||||
* @return the content
|
||||
* @throws IOException
|
||||
* if unable to read the content
|
||||
*/
|
||||
public static String getStreamedContentAsString(HttpURLConnection response) throws IOException {
|
||||
String body = ContentUtils.toString(ContentUtils.toByteArray(response.getInputStream()));
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to convert the content from its Json serialization, if possible.
|
||||
*
|
||||
* @param <T>
|
||||
* the type of the desired object
|
||||
* @return an object of type T from the content
|
||||
* @throws IOException
|
||||
* @throws Exception
|
||||
* if the deserialization fails
|
||||
*/
|
||||
public static <T> T tryConvertStreamedContentFromJson(HttpURLConnection response, Class<T> classtype)
|
||||
throws IOException, Exception {
|
||||
return JsonUtilsCustom.fromJson(getStreamedContentAsString(response), classtype);
|
||||
}
|
||||
|
||||
public static <T> T tryConvertStreamedContentFromJson(HttpURLConnection response, JavaType objectType)
|
||||
throws IOException, Exception {
|
||||
return JsonUtilsCustom.fromJson(ContentUtils.toByteArray(response.getInputStream()), objectType);
|
||||
}
|
||||
|
||||
public static <T> T tryConvertStreamedContentFromJson(GXInboundResponse response, JavaType objectType)
|
||||
throws IOException, Exception {
|
||||
String body = response.getStreamedContentAsString();
|
||||
|
||||
return JsonUtilsCustom.fromJson(body, objectType);
|
||||
}
|
||||
|
||||
public static <T> T tryConvertStreamedContentFromJson(GXInboundResponse response, Class<T> classtype)
|
||||
throws IOException, Exception {
|
||||
return JsonUtilsCustom.fromJson(response.getStreamedContentAsString(), classtype);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package org.gcube.idm.client.filter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
// import org.gcube.common.authorization.library.provider.AccessTokenProvider;
|
||||
// import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.ws.rs.client.ClientRequestContext;
|
||||
import jakarta.ws.rs.client.ClientRequestFilter;
|
||||
|
||||
/**
|
||||
* Authorization filter for the jersey client.
|
||||
*/
|
||||
public class AuthorizationFilter implements ClientRequestFilter {
|
||||
// private final static String AUTH_TOKEN_PARAMETER = "Authorization";
|
||||
// private final static String LEGACY_AUTH_TOKEN_PARAMETER = "gcube-token";
|
||||
private static Logger logger = LoggerFactory.getLogger(AuthorizationFilter.class);
|
||||
|
||||
@Override
|
||||
public void filter(ClientRequestContext original) throws IOException {
|
||||
// TODO: IMPLEMENT AuthorizationFilter
|
||||
logger.debug("Adding token to the request " + original.getUri());
|
||||
|
||||
logger.debug("TODO: IMPLEMENT AuthorizationFilter");
|
||||
//
|
||||
// logger.debug("Adding token to the request " + original.getUri());
|
||||
// String token = AccessTokenProvider.instance.get();
|
||||
// String legacyToken = SecurityTokenProvider.instance.get();
|
||||
// if (token != null)
|
||||
// original.getHeaders().add(AUTH_TOKEN_PARAMETER, " Bearer " + token);
|
||||
// if (legacyToken != null)
|
||||
// original.getHeaders().add(LEGACY_AUTH_TOKEN_PARAMETER, legacyToken);
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package org.gcube.idm.client.model;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class AddressClaimSet {
|
||||
|
||||
public static final String FORMATTED = "formatted";
|
||||
public static final String STREET_ADDRESS = "street_address";
|
||||
public static final String LOCALITY = "locality";
|
||||
public static final String REGION = "region";
|
||||
public static final String POSTAL_CODE = "postal_code";
|
||||
public static final String COUNTRY = "country";
|
||||
|
||||
@JsonProperty(FORMATTED)
|
||||
protected String formattedAddress;
|
||||
|
||||
@JsonProperty(STREET_ADDRESS)
|
||||
protected String streetAddress;
|
||||
|
||||
@JsonProperty(LOCALITY)
|
||||
protected String locality;
|
||||
|
||||
@JsonProperty(REGION)
|
||||
protected String region;
|
||||
|
||||
@JsonProperty(POSTAL_CODE)
|
||||
protected String postalCode;
|
||||
|
||||
@JsonProperty(COUNTRY)
|
||||
protected String country;
|
||||
|
||||
public String getFormattedAddress() {
|
||||
return this.formattedAddress;
|
||||
}
|
||||
|
||||
public void setFormattedAddress(String formattedAddress) {
|
||||
this.formattedAddress = formattedAddress;
|
||||
}
|
||||
|
||||
public String getStreetAddress() {
|
||||
return this.streetAddress;
|
||||
}
|
||||
|
||||
public void setStreetAddress(String streetAddress) {
|
||||
this.streetAddress = streetAddress;
|
||||
}
|
||||
|
||||
public String getLocality() {
|
||||
return this.locality;
|
||||
}
|
||||
|
||||
public void setLocality(String locality) {
|
||||
this.locality = locality;
|
||||
}
|
||||
|
||||
public String getRegion() {
|
||||
return this.region;
|
||||
}
|
||||
|
||||
public void setRegion(String region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public String getPostalCode() {
|
||||
return this.postalCode;
|
||||
}
|
||||
|
||||
public void setPostalCode(String postalCode) {
|
||||
this.postalCode = postalCode;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return this.country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package org.gcube.idm.client.model;
|
||||
|
||||
import jakarta.ws.rs.ext.ContextResolver;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
|
||||
/**
|
||||
* Custom mapper with property CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES to
|
||||
* perform serialization/deserialization
|
||||
* with snake case over camel case for json beans.
|
||||
*
|
||||
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
||||
*/
|
||||
@Provider
|
||||
public class CustomObjectMapper implements ContextResolver<ObjectMapper> {
|
||||
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CustomObjectMapper.class);
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public CustomObjectMapper() {
|
||||
logger.debug("new ObjectMapperResolver()");
|
||||
mapper = new ObjectMapper();
|
||||
mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
||||
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectMapper getContext(Class<?> type) {
|
||||
logger.debug("ObjectMapperResolver.getContext(...) invoked");
|
||||
return mapper;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
package org.gcube.idm.client.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class OwnerInfo {
|
||||
|
||||
@JsonProperty("roles")
|
||||
private List<String> roles;
|
||||
|
||||
public List<String> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles(List<String> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
@JsonProperty("globalRoles")
|
||||
private List<String> globalRoles;
|
||||
|
||||
|
||||
@JsonProperty("externalClient")
|
||||
private boolean externalClient;
|
||||
|
||||
@JsonProperty("email")
|
||||
private String email;
|
||||
|
||||
@JsonProperty("firstName")
|
||||
private String firstName;
|
||||
|
||||
@JsonProperty("lastName")
|
||||
private String lastName;
|
||||
|
||||
@JsonProperty("clientName")
|
||||
private String clientName;
|
||||
|
||||
@JsonProperty("contactPerson")
|
||||
private String contactPerson;
|
||||
|
||||
@JsonProperty("contactOrganisation")
|
||||
private String contactOrganisation;
|
||||
|
||||
@JsonProperty("application")
|
||||
private String application;
|
||||
|
||||
@JsonProperty("id")
|
||||
private String id;
|
||||
|
||||
public OwnerInfo(List<String> roles, List<String> globalRoles, boolean externalClient, String email,
|
||||
String firstName, String lastName, String clientName, String contactPerson, String contactOrganisation,
|
||||
String application, String id) {
|
||||
this.roles = roles;
|
||||
this.globalRoles = globalRoles;
|
||||
this.externalClient = externalClient;
|
||||
this.email = email;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.clientName = clientName;
|
||||
this.contactPerson = contactPerson;
|
||||
this.contactOrganisation = contactOrganisation;
|
||||
this.application = application;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public OwnerInfo() {
|
||||
super();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @param username
|
||||
// * @param roles
|
||||
// * @param avatar
|
||||
// * @param fullname
|
||||
// */
|
||||
// public UserProfile(String username, List<String> roles, String avatar,
|
||||
// String fullname) {
|
||||
// super();
|
||||
// this.username = username;
|
||||
// this.roles = roles;
|
||||
// this.avatar = avatar;
|
||||
// this.fullname = fullname;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserProfile [id = " + id + ", roles = " + roles + ", gloablRoles = " + globalRoles + "]";
|
||||
}
|
||||
|
||||
public boolean isExternalClient() {
|
||||
return externalClient;
|
||||
}
|
||||
|
||||
public void setExternalClient(boolean externalClient) {
|
||||
this.externalClient = externalClient;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getClientName() {
|
||||
return clientName;
|
||||
}
|
||||
|
||||
public void setClientName(String clientName) {
|
||||
this.clientName = clientName;
|
||||
}
|
||||
|
||||
public String getContactPerson() {
|
||||
return contactPerson;
|
||||
}
|
||||
|
||||
public void setContactPerson(String contactPerson) {
|
||||
this.contactPerson = contactPerson;
|
||||
}
|
||||
|
||||
public String getContactOrganisation() {
|
||||
return contactOrganisation;
|
||||
}
|
||||
|
||||
public void setContactOrganisation(String contactOrganisation) {
|
||||
this.contactOrganisation = contactOrganisation;
|
||||
}
|
||||
|
||||
public String getApplication() {
|
||||
return application;
|
||||
}
|
||||
|
||||
public void setApplication(String application) {
|
||||
this.application = application;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<String> getGlobalRoles() {
|
||||
return globalRoles;
|
||||
}
|
||||
|
||||
public void setGlobalRoles(List<String> globalRoles) {
|
||||
this.globalRoles = globalRoles;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,308 @@
|
|||
package org.gcube.idm.client.model;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||
import org.gcube.com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import org.gcube.com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import org.gcube.com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import org.gcube.com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import org.gcube.idm.client.model.util.StringOrArrayDeserializer;
|
||||
import org.gcube.idm.client.model.util.StringOrArraySerializer;
|
||||
/**
|
||||
* @author pedroigor
|
||||
*/
|
||||
public class TokenInfo {
|
||||
|
||||
// Should be in signed UserInfo response
|
||||
@JsonProperty("iss")
|
||||
protected String issuer;
|
||||
@JsonProperty("aud")
|
||||
@JsonSerialize(using = StringOrArraySerializer.class)
|
||||
@JsonDeserialize(using = StringOrArrayDeserializer.class)
|
||||
protected String[] audience;
|
||||
|
||||
@JsonProperty("sub")
|
||||
protected String sub;
|
||||
|
||||
@JsonProperty("name")
|
||||
protected String name;
|
||||
|
||||
@JsonProperty("given_name")
|
||||
protected String givenName;
|
||||
|
||||
@JsonProperty("family_name")
|
||||
protected String familyName;
|
||||
|
||||
@JsonProperty("middle_name")
|
||||
protected String middleName;
|
||||
|
||||
@JsonProperty("nickname")
|
||||
protected String nickName;
|
||||
|
||||
@JsonProperty("preferred_username")
|
||||
protected String preferredUsername;
|
||||
|
||||
@JsonProperty("profile")
|
||||
protected String profile;
|
||||
|
||||
@JsonProperty("picture")
|
||||
protected String picture;
|
||||
|
||||
@JsonProperty("website")
|
||||
protected String website;
|
||||
|
||||
@JsonProperty("email")
|
||||
protected String email;
|
||||
|
||||
@JsonProperty("email_verified")
|
||||
protected Boolean emailVerified;
|
||||
|
||||
@JsonProperty("gender")
|
||||
protected String gender;
|
||||
|
||||
@JsonProperty("birthdate")
|
||||
protected String birthdate;
|
||||
|
||||
@JsonProperty("zoneinfo")
|
||||
protected String zoneinfo;
|
||||
|
||||
@JsonProperty("locale")
|
||||
protected String locale;
|
||||
|
||||
@JsonProperty("phone_number")
|
||||
protected String phoneNumber;
|
||||
|
||||
@JsonProperty("phone_number_verified")
|
||||
protected Boolean phoneNumberVerified;
|
||||
|
||||
@JsonProperty("address")
|
||||
protected AddressClaimSet address;
|
||||
|
||||
@JsonProperty("updated_at")
|
||||
protected Long updatedAt;
|
||||
|
||||
@JsonProperty("claims_locales")
|
||||
protected String claimsLocales;
|
||||
|
||||
protected Map<String, Object> otherClaims = new HashMap<>();
|
||||
|
||||
public String getIssuer() {
|
||||
return issuer;
|
||||
}
|
||||
|
||||
public void setIssuer(String issuer) {
|
||||
this.issuer = issuer;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public String[] getAudience() {
|
||||
return audience;
|
||||
}
|
||||
|
||||
public boolean hasAudience(String audience) {
|
||||
for (String a : this.audience) {
|
||||
if (a.equals(audience)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setAudience(String... audience) {
|
||||
this.audience = audience;
|
||||
}
|
||||
|
||||
public String getSubject() {
|
||||
return this.sub;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.sub = subject;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getGivenName() {
|
||||
return this.givenName;
|
||||
}
|
||||
|
||||
public void setGivenName(String givenName) {
|
||||
this.givenName = givenName;
|
||||
}
|
||||
|
||||
public String getFamilyName() {
|
||||
return this.familyName;
|
||||
}
|
||||
|
||||
public void setFamilyName(String familyName) {
|
||||
this.familyName = familyName;
|
||||
}
|
||||
|
||||
public String getMiddleName() {
|
||||
return this.middleName;
|
||||
}
|
||||
|
||||
public void setMiddleName(String middleName) {
|
||||
this.middleName = middleName;
|
||||
}
|
||||
|
||||
public String getNickName() {
|
||||
return this.nickName;
|
||||
}
|
||||
|
||||
public void setNickName(String nickName) {
|
||||
this.nickName = nickName;
|
||||
}
|
||||
|
||||
public String getPreferredUsername() {
|
||||
return this.preferredUsername;
|
||||
}
|
||||
|
||||
public void setPreferredUsername(String preferredUsername) {
|
||||
this.preferredUsername = preferredUsername;
|
||||
}
|
||||
|
||||
public String getProfile() {
|
||||
return this.profile;
|
||||
}
|
||||
|
||||
public void setProfile(String profile) {
|
||||
this.profile = profile;
|
||||
}
|
||||
|
||||
public String getPicture() {
|
||||
return this.picture;
|
||||
}
|
||||
|
||||
public void setPicture(String picture) {
|
||||
this.picture = picture;
|
||||
}
|
||||
|
||||
public String getWebsite() {
|
||||
return this.website;
|
||||
}
|
||||
|
||||
public void setWebsite(String website) {
|
||||
this.website = website;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return this.email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Boolean getEmailVerified() {
|
||||
return this.emailVerified;
|
||||
}
|
||||
|
||||
public void setEmailVerified(Boolean emailVerified) {
|
||||
this.emailVerified = emailVerified;
|
||||
}
|
||||
|
||||
public String getGender() {
|
||||
return this.gender;
|
||||
}
|
||||
|
||||
public void setGender(String gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public String getBirthdate() {
|
||||
return this.birthdate;
|
||||
}
|
||||
|
||||
public void setBirthdate(String birthdate) {
|
||||
this.birthdate = birthdate;
|
||||
}
|
||||
|
||||
public String getZoneinfo() {
|
||||
return this.zoneinfo;
|
||||
}
|
||||
|
||||
public void setZoneinfo(String zoneinfo) {
|
||||
this.zoneinfo = zoneinfo;
|
||||
}
|
||||
|
||||
public String getLocale() {
|
||||
return this.locale;
|
||||
}
|
||||
|
||||
public void setLocale(String locale) {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
public String getPhoneNumber() {
|
||||
return this.phoneNumber;
|
||||
}
|
||||
|
||||
public void setPhoneNumber(String phoneNumber) {
|
||||
this.phoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
public Boolean getPhoneNumberVerified() {
|
||||
return this.phoneNumberVerified;
|
||||
}
|
||||
|
||||
public void setPhoneNumberVerified(Boolean phoneNumberVerified) {
|
||||
this.phoneNumberVerified = phoneNumberVerified;
|
||||
}
|
||||
|
||||
public AddressClaimSet getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(AddressClaimSet address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Long getUpdatedAt() {
|
||||
return this.updatedAt;
|
||||
}
|
||||
|
||||
public void setUpdatedAt(Long updatedAt) {
|
||||
this.updatedAt = updatedAt;
|
||||
}
|
||||
|
||||
public String getSub() {
|
||||
return this.sub;
|
||||
}
|
||||
|
||||
public void setSub(String sub) {
|
||||
this.sub = sub;
|
||||
}
|
||||
|
||||
public String getClaimsLocales() {
|
||||
return this.claimsLocales;
|
||||
}
|
||||
|
||||
public void setClaimsLocales(String claimsLocales) {
|
||||
this.claimsLocales = claimsLocales;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a map of any other claims and data that might be in the UserInfo. Could be custom claims set up by the auth server
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@JsonAnyGetter
|
||||
public Map<String, Object> getOtherClaims() {
|
||||
return otherClaims;
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void setOtherClaims(String name, Object value) {
|
||||
otherClaims.put(name, value);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package org.gcube.idm.client.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class UserProfile {
|
||||
|
||||
@JsonProperty("username")
|
||||
private String username;
|
||||
|
||||
@JsonProperty("roles")
|
||||
private List<String> roles;
|
||||
|
||||
@JsonProperty("avatar")
|
||||
private String avatar;
|
||||
|
||||
@JsonProperty("fullname")
|
||||
private String fullname;
|
||||
|
||||
public UserProfile() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username
|
||||
* @param roles
|
||||
* @param avatar
|
||||
* @param fullname
|
||||
*/
|
||||
public UserProfile(String username, List<String> roles, String avatar,
|
||||
String fullname) {
|
||||
super();
|
||||
this.username = username;
|
||||
this.roles = roles;
|
||||
this.avatar = avatar;
|
||||
this.fullname = fullname;
|
||||
}
|
||||
|
||||
public String getUsername ()
|
||||
{
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername (String username)
|
||||
{
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public List<String> getRoles ()
|
||||
{
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles (List<String> roles)
|
||||
{
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
public String getAvatar ()
|
||||
{
|
||||
return avatar;
|
||||
}
|
||||
|
||||
public void setAvatar (String avatar)
|
||||
{
|
||||
this.avatar = avatar;
|
||||
}
|
||||
|
||||
public String getFullname ()
|
||||
{
|
||||
return fullname;
|
||||
}
|
||||
|
||||
public void setFullname (String fullname)
|
||||
{
|
||||
this.fullname = fullname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "UserProfile [username = "+username+", roles = "+roles+", avatar = "+avatar+", fullname = "+fullname+"]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,146 @@
|
|||
package org.gcube.idm.client.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class UserProfileExtended extends UserProfile {
|
||||
|
||||
@JsonProperty("middle_name")
|
||||
private String middleName;
|
||||
|
||||
@JsonProperty("male")
|
||||
private boolean male;
|
||||
|
||||
@JsonProperty("location_industry")
|
||||
private String locationIndustry;
|
||||
|
||||
@JsonProperty("first_name")
|
||||
private String firstName;
|
||||
|
||||
@JsonProperty("email")
|
||||
private String email;
|
||||
|
||||
@JsonProperty("job_title")
|
||||
private String jobTitle;
|
||||
|
||||
@JsonProperty("last_name")
|
||||
private String lastName;
|
||||
|
||||
@JsonProperty("registration_date")
|
||||
private long registrationDate;
|
||||
|
||||
@JsonProperty("user_id")
|
||||
private long userId;
|
||||
|
||||
@JsonProperty("email_addresses")
|
||||
private List<String> emailAddresses;
|
||||
|
||||
public UserProfileExtended() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username
|
||||
* @param roles
|
||||
* @param avatar
|
||||
* @param fullname
|
||||
*/
|
||||
public UserProfileExtended(String username, List<String> roles,
|
||||
String avatar, String fullname) {
|
||||
super(username, roles, avatar, fullname);
|
||||
}
|
||||
|
||||
public String getMiddleName() {
|
||||
return middleName;
|
||||
}
|
||||
|
||||
public void setMiddleName(String middleName) {
|
||||
this.middleName = middleName;
|
||||
}
|
||||
|
||||
public boolean isMale() {
|
||||
return male;
|
||||
}
|
||||
|
||||
public void setMale(boolean male) {
|
||||
this.male = male;
|
||||
}
|
||||
|
||||
public String getLocationIndustry() {
|
||||
return locationIndustry;
|
||||
}
|
||||
|
||||
public void setLocationIndustry(String locationIndustry) {
|
||||
this.locationIndustry = locationIndustry;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getJobTitle() {
|
||||
return jobTitle;
|
||||
}
|
||||
|
||||
public void setJobTitle(String jobTitle) {
|
||||
this.jobTitle = jobTitle;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public long getRegistrationDate() {
|
||||
return registrationDate;
|
||||
}
|
||||
|
||||
public void setRegistrationDate(long registrationDate) {
|
||||
this.registrationDate = registrationDate;
|
||||
}
|
||||
|
||||
public long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public List<String> getEmailAddresses() {
|
||||
return emailAddresses;
|
||||
}
|
||||
|
||||
public void setEmailAddresses(List<String> emailAddresses) {
|
||||
this.emailAddresses = emailAddresses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserProfileExtended [UserProfile =" + super.toString() + ", middleName=" + middleName + ", male="
|
||||
+ male + ", locationIndustry=" + locationIndustry
|
||||
+ ", firstName=" + firstName + ", email=" + email
|
||||
+ ", jobTitle=" + jobTitle + ", lastName=" + lastName
|
||||
+ ", registrationDate=" + registrationDate + ", userId="
|
||||
+ userId + ", emailAddresses=" + emailAddresses + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,177 @@
|
|||
package org.gcube.idm.client.model.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JavaType;
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.gcube.com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import org.gcube.idm.client.beans.ResponseBean;
|
||||
|
||||
// cannot extend JsonUtils , it's final
|
||||
// public class JsonUtilsCustom extends JsonUtils {
|
||||
|
||||
public class JsonUtilsCustom {
|
||||
|
||||
private static TypeFactory _tf = null;
|
||||
private static ObjectMapper _om = null;
|
||||
|
||||
private static ObjectMapper getObjectMapper() {
|
||||
if (_om == null) {
|
||||
_om = new ObjectMapper();
|
||||
}
|
||||
return _om;
|
||||
|
||||
}
|
||||
|
||||
private static TypeFactory getTypeFactory() {
|
||||
if (_tf == null) {
|
||||
ObjectMapper om = getObjectMapper();
|
||||
_tf = om.getTypeFactory();
|
||||
}
|
||||
return _tf;
|
||||
}
|
||||
|
||||
|
||||
public static JavaType getObjectJT(Class<?> classType) {
|
||||
TypeFactory tf = getTypeFactory();
|
||||
// map key type
|
||||
JavaType objectsType = tf.constructType(classType);
|
||||
return objectsType;
|
||||
}
|
||||
|
||||
public static JavaType getResponseBeansObjectJT(Class<?> classType) {
|
||||
TypeFactory tf = getTypeFactory();
|
||||
// map key type
|
||||
JavaType objectsType = tf.constructType(classType);
|
||||
return getResponseBeanOfObjectsType(objectsType);
|
||||
}
|
||||
|
||||
public static JavaType getResponseBeanOfObjectsType(JavaType objectsType) {
|
||||
TypeFactory tf = getTypeFactory();
|
||||
JavaType beansType = tf.constructParametrizedType(ResponseBean.class, ResponseBean.class, objectsType);
|
||||
return beansType;
|
||||
}
|
||||
|
||||
public static JavaType geListOfObjectsType(Class<?> classType) {
|
||||
TypeFactory tf = getTypeFactory();
|
||||
JavaType objectsType = tf.constructType(classType);
|
||||
return geListOfObjectsType(objectsType);
|
||||
}
|
||||
|
||||
public static JavaType geListOfObjectsType(JavaType objectsType) {
|
||||
TypeFactory tf = getTypeFactory();
|
||||
JavaType listOfObject = tf.constructParametrizedType(List.class, List.class, objectsType);
|
||||
return listOfObject;
|
||||
}
|
||||
|
||||
public static JavaType getResponseBeanOfListOfObjectsType(Class<?> classType) {
|
||||
JavaType listOfObjectType = geListOfObjectsType(classType);
|
||||
return getResponseBeanOfListOfObjectsType(listOfObjectType);
|
||||
}
|
||||
|
||||
public static JavaType getResponseBeanOfListOfObjectsType(JavaType listOfObjectType) {
|
||||
JavaType beansType = getResponseBeanOfObjectsType(listOfObjectType);
|
||||
return beansType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @param bytes
|
||||
* @param type
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
/**
|
||||
* Deserializes the specified Json bytes into an object of the specified class
|
||||
*
|
||||
* @param <T> the type of the desired object
|
||||
* @param json the string from which the object is to be deserialized
|
||||
* @param classOfT the class of T
|
||||
* @return an object of type T from the bytes
|
||||
* @throws Exception if the deserialization fails
|
||||
*/
|
||||
public static <T> T fromJson(byte[] bytes, JavaType type) throws Exception {
|
||||
try {
|
||||
return getObjectMapper().readValue(bytes, type);
|
||||
} catch (Exception e) {
|
||||
throw new Exception("Cannot deserialize to the object.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the specified Json bytes into an object of the specified class
|
||||
*
|
||||
* @param <T> the type of the desired object
|
||||
* @param json the string from which the object is to be deserialized
|
||||
* @param classOfT the class of T
|
||||
* @return an object of type T from the bytes
|
||||
* @throws Exception if the deserialization fails
|
||||
*/
|
||||
public static <T> T responseBeanFromJson(byte[] bytes, JavaType type) throws Exception {
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
return objectMapper.readValue(bytes, type);
|
||||
} catch (Exception e) {
|
||||
throw new Exception("Cannot deserialize to the object.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the specified Json bytes into an object of the specified class
|
||||
*
|
||||
* @param <T> the type of the desired object
|
||||
* @param json the string from which the object is to be deserialized
|
||||
* @param classOfT the class of T
|
||||
* @return an object of type T from the bytes
|
||||
* @throws Exception if the deserialization fails
|
||||
*/
|
||||
public static <T> T fromJson(byte[] bytes, Class<T> raw) throws Exception {
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
return objectMapper.readValue(bytes, raw);
|
||||
} catch (Exception e) {
|
||||
throw new Exception("Cannot deserialize to the object.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the specified Json bytes into an object of the specified class
|
||||
*
|
||||
* @param <T> the type of the desired object
|
||||
* @param json the string from which the object is to be deserialized
|
||||
* @param raw the class of T
|
||||
* @return an object of type T from the bytes
|
||||
* @throws Exception if the deserialization fails
|
||||
*/
|
||||
public static <T> T fromJson(String json, Class<T> raw) throws Exception {
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
return objectMapper.readValue(json, raw);
|
||||
} catch (Exception e) {
|
||||
throw new Exception("Cannot deserialize to the object.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the specified Json bytes into an object of the specified class
|
||||
*
|
||||
* @param <T> the type of the desired object
|
||||
* @param json the string from which the object is to be deserialized
|
||||
* @param raw the class of T
|
||||
* @return an object of type T from the bytes
|
||||
* @throws Exception if the deserialization fails
|
||||
*/
|
||||
public static <T> T fromJson(String json, JavaType objectType) throws Exception {
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
return objectMapper.readValue(json, objectType);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new Exception("Cannot deserialize to the object.", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package org.gcube.idm.client.model.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonParser;
|
||||
import org.gcube.com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
|
||||
public class StringListMapDeserializer extends JsonDeserializer<Object> {
|
||||
|
||||
@Override
|
||||
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
|
||||
JsonNode jsonNode = jsonParser.readValueAsTree();
|
||||
Iterator<Map.Entry<String, JsonNode>> itr = jsonNode.fields();
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
while (itr.hasNext()) {
|
||||
Map.Entry<String, JsonNode> e = itr.next();
|
||||
List<String> values = new LinkedList<>();
|
||||
if (!e.getValue().isArray()) {
|
||||
values.add((e.getValue().isNull()) ? null : e.getValue().asText());
|
||||
} else {
|
||||
ArrayNode a = (ArrayNode) e.getValue();
|
||||
Iterator<JsonNode> vitr = a.elements();
|
||||
while (vitr.hasNext()) {
|
||||
JsonNode node = vitr.next();
|
||||
values.add((node.isNull() ? null : node.asText()));
|
||||
}
|
||||
}
|
||||
map.put(e.getKey(), values);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package org.gcube.idm.client.model.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonParser;
|
||||
import org.gcube.com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class StringOrArrayDeserializer extends JsonDeserializer<Object> {
|
||||
|
||||
@Override
|
||||
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
|
||||
JsonNode jsonNode = jsonParser.readValueAsTree();
|
||||
if (jsonNode.isArray()) {
|
||||
ArrayList<String> a = new ArrayList<>(1);
|
||||
Iterator<JsonNode> itr = jsonNode.iterator();
|
||||
while (itr.hasNext()) {
|
||||
a.add(itr.next().textValue());
|
||||
}
|
||||
return a.toArray(new String[a.size()]);
|
||||
} else {
|
||||
return new String[] { jsonNode.textValue() };
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package org.gcube.idm.client.model.util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonGenerator;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import org.gcube.com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
public class StringOrArraySerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||
String[] array = (String[]) o;
|
||||
if (array == null) {
|
||||
jsonGenerator.writeNull();
|
||||
} else if (array.length == 1) {
|
||||
jsonGenerator.writeString(array[0]);
|
||||
} else {
|
||||
jsonGenerator.writeStartArray();
|
||||
for (String s : array) {
|
||||
jsonGenerator.writeString(s);
|
||||
}
|
||||
jsonGenerator.writeEndArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package org.gcube.idm.client.model.util;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Time {
|
||||
|
||||
private static int offset;
|
||||
|
||||
/**
|
||||
* Returns current time in seconds adjusted by adding {@link #offset) seconds.
|
||||
* @return see description
|
||||
*/
|
||||
public static int currentTime() {
|
||||
return ((int) (System.currentTimeMillis() / 1000)) + offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current time in milliseconds adjusted by adding {@link #offset) seconds.
|
||||
* @return see description
|
||||
*/
|
||||
public static long currentTimeMillis() {
|
||||
return System.currentTimeMillis() + (offset * 1000L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@link Date} object, its value set to time
|
||||
* @param time Time in milliseconds since the epoch
|
||||
* @return see description
|
||||
*/
|
||||
public static Date toDate(int time) {
|
||||
return new Date(time * 1000L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@link Date} object, its value set to time
|
||||
* @param time Time in milliseconds since the epoch
|
||||
* @return see description
|
||||
*/
|
||||
public static Date toDate(long time) {
|
||||
return new Date(time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns time in milliseconds for a time in seconds. No adjustment is made to the parameter.
|
||||
* @param time Time in seconds since the epoch
|
||||
* @return Time in milliseconds
|
||||
*/
|
||||
public static long toMillis(int time) {
|
||||
return time * 1000L;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Time offset in seconds that will be added to {@link #currentTime()} and {@link #currentTimeMillis()}.
|
||||
*/
|
||||
public static int getOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets time offset in seconds that will be added to {@link #currentTime()} and {@link #currentTimeMillis()}.
|
||||
* @param offset Offset (in seconds)
|
||||
*/
|
||||
public static void setOffset(int offset) {
|
||||
Time.offset = offset;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package org.gcube.idm.client.utils;
|
||||
|
||||
public class Validate {
|
||||
|
||||
/**
|
||||
* <p>Validate that the argument condition is <code>true</code>; otherwise
|
||||
* throwing an exception with the specified message. This method is useful when
|
||||
* validating according to an arbitrary boolean expression, such as validating a
|
||||
* primitive number or using your own custom validation expression.</p>
|
||||
*
|
||||
* <pre>
|
||||
* Validate.isTrue( (i > 0), "The value must be greater than zero");
|
||||
* Validate.isTrue( myObject.isOk(), "The object is not OK");
|
||||
* </pre>
|
||||
*
|
||||
* @param expression the boolean expression to check
|
||||
* @param message the exception message if invalid
|
||||
* @throws IllegalArgumentException if expression is <code>false</code>
|
||||
*/
|
||||
public static void isTrue(boolean expression, String message) {
|
||||
if (expression == false) {
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,26 @@
|
|||
.d4science_intro {
|
||||
top: 0;
|
||||
z-index: 2000;
|
||||
position: fixed;
|
||||
display: block ruby;
|
||||
padding: 10px;
|
||||
background: white;
|
||||
width: 100%;
|
||||
height: 100px;
|
||||
}
|
||||
|
||||
.navbar-fixed-top {
|
||||
top: 100px !important;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
top: 160px !important;
|
||||
}
|
||||
|
||||
.navbar {
|
||||
margin-bottom: 40px !important;
|
||||
}
|
||||
|
||||
.main {
|
||||
top: 90px;
|
||||
}
|
|
@ -0,0 +1,280 @@
|
|||
package org.gcube.idm.client.test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.rmi.ServerException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.gcube.common.keycloak.KeycloakClientFactory;
|
||||
import org.gcube.common.keycloak.KeycloakClientHelper;
|
||||
import org.gcube.common.keycloak.model.TokenResponse;
|
||||
import org.gcube.common.security.providers.SecretManagerProvider;
|
||||
import org.gcube.common.security.secrets.AccessTokenSecret;
|
||||
import org.gcube.common.security.secrets.Secret;
|
||||
import org.gcube.idm.client.IdmClientFactory;
|
||||
import org.gcube.idm.client.IdmMeClient;
|
||||
import org.gcube.idm.client.IdmUsersClient;
|
||||
import org.gcube.idm.client.clients.IdmRestClient;
|
||||
import org.gcube.idm.common.is.IsServerConfig;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.ws.rs.BadRequestException;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.gcube.common.gxrest.request.GXHTTPStringRequest;
|
||||
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
|
||||
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public abstract class ClientContextTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ClientContextTest.class);
|
||||
|
||||
protected static final String CONFIG_INI_FILENAME = "test.ini";
|
||||
|
||||
public static final String DEFAULT_TEST_SCOPE;
|
||||
public static Secret current_secret;
|
||||
|
||||
public IsServerConfig getMokedIsConfig() {
|
||||
String baseurl = properties.getProperty("SERVICE_URL"); // "http://146.48.85.179:9999/idm-service/";
|
||||
// String baseurl = "http://localhost:8080/idm-service";
|
||||
IsServerConfig config = new IsServerConfig(baseurl);
|
||||
return config;
|
||||
}
|
||||
|
||||
public IdmMeClient getMeClient() throws IOException {
|
||||
String token = getUserToken();
|
||||
return getMeClient(token);
|
||||
}
|
||||
|
||||
public IdmMeClient getMeClient(String token) throws ServerException {
|
||||
IdmClientFactory factory = IdmClientFactory.getSingleton();
|
||||
factory.setConfig(getMokedIsConfig());
|
||||
IdmMeClient client = factory.meClient(token);
|
||||
return client;
|
||||
}
|
||||
|
||||
public IdmUsersClient getUserClient() throws ServerException {
|
||||
String token = getServiceToken();
|
||||
return getUserClient(token);
|
||||
}
|
||||
|
||||
public IdmUsersClient getUserClient(String token) throws ServerException {
|
||||
|
||||
IdmClientFactory factory = IdmClientFactory.getSingleton();
|
||||
factory.setConfig(getMokedIsConfig());
|
||||
IdmUsersClient client = factory.userClient(token);
|
||||
return client;
|
||||
}
|
||||
|
||||
public String getServiceToken() {
|
||||
String token = properties.getProperty("TOKEN_SERVICE");
|
||||
return token;
|
||||
}
|
||||
|
||||
private String user_token;
|
||||
|
||||
public String getUserToken() throws IOException {
|
||||
if (user_token == null) {
|
||||
user_token = loginUser();
|
||||
}
|
||||
|
||||
return user_token;
|
||||
}
|
||||
|
||||
public String getExpiredServiceToken() {
|
||||
String token = properties.getProperty("EXPIRED_SERVICE_TOKEN");
|
||||
return token;
|
||||
}
|
||||
|
||||
public String getExpiredUserToken() {
|
||||
String token = properties.getProperty("EXPIRED_USER_TOKEN");
|
||||
return token;
|
||||
}
|
||||
|
||||
public static final String VRE;
|
||||
|
||||
protected static final Properties properties;
|
||||
|
||||
public static final String TYPE_PROPERTY_KEY = "type";
|
||||
public static final String USERNAME_PROPERTY_KEY = "username";
|
||||
public static final String PASSWORD_PROPERTY_KEY = "password";
|
||||
public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
properties = readProperties(CONFIG_INI_FILENAME);
|
||||
VRE = properties.getProperty("context");
|
||||
DEFAULT_TEST_SCOPE = VRE;
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Properties readProperties(String filename) throws IOException {
|
||||
|
||||
Properties p = new Properties();
|
||||
InputStream input = ClientContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME);
|
||||
// load the properties file
|
||||
p.load(input);
|
||||
return p;
|
||||
|
||||
}
|
||||
|
||||
private enum Type {
|
||||
USER, CLIENT_ID
|
||||
};
|
||||
|
||||
public static void set(Secret secret) throws Exception {
|
||||
SecretManagerProvider.reset();
|
||||
SecretManagerProvider.set(secret);
|
||||
current_secret = secret;
|
||||
}
|
||||
|
||||
public static void setContextByName(String fullContextName) throws Exception {
|
||||
logger.debug("Going to set credentials for context {}", fullContextName);
|
||||
Secret secret = getSecretByContextName(fullContextName);
|
||||
set(secret);
|
||||
}
|
||||
|
||||
private static TokenResponse getJWTAccessToken(String context) throws Exception {
|
||||
Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
|
||||
|
||||
TokenResponse tr = null;
|
||||
|
||||
int index = context.indexOf('/', 1);
|
||||
String root = context.substring(0, index == -1 ? context.length() : index);
|
||||
|
||||
switch (type) {
|
||||
case CLIENT_ID:
|
||||
String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
|
||||
String clientSecret = properties.getProperty(root);
|
||||
|
||||
tr = KeycloakClientFactory.newInstance().queryUMAToken(context, clientId, clientSecret, context, null);
|
||||
break;
|
||||
|
||||
case USER:
|
||||
default:
|
||||
String username = properties.getProperty(USERNAME_PROPERTY_KEY);
|
||||
String password = properties.getProperty(PASSWORD_PROPERTY_KEY);
|
||||
|
||||
switch (root) {
|
||||
case "/gcube":
|
||||
default:
|
||||
clientId = "next.d4science.org";
|
||||
break;
|
||||
|
||||
case "/pred4s":
|
||||
clientId = "pre.d4science.org";
|
||||
break;
|
||||
|
||||
case "/d4science.research-infrastructures.eu":
|
||||
clientId = "services.d4science.org";
|
||||
break;
|
||||
}
|
||||
clientSecret = null;
|
||||
|
||||
tr = KeycloakClientHelper.getTokenForUser(context, username, password);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return tr;
|
||||
|
||||
}
|
||||
|
||||
public static Secret getSecretByContextName(String context) throws Exception {
|
||||
TokenResponse tr = getJWTAccessToken(context);
|
||||
Secret secret = new AccessTokenSecret(tr.getAccessToken(), context);
|
||||
return secret;
|
||||
}
|
||||
|
||||
public static void setContext(String token) throws Exception {
|
||||
Secret secret = getSecret(token);
|
||||
set(secret);
|
||||
}
|
||||
|
||||
private static Secret getSecret(String token) throws Exception {
|
||||
// TODO: verificare classe (AccessTokenSecret anziche JWTToken) e context(VRE)
|
||||
Secret secret = new AccessTokenSecret(token, VRE);
|
||||
return secret;
|
||||
}
|
||||
|
||||
public static String getUser() {
|
||||
String user = "UNKNOWN";
|
||||
try {
|
||||
user = SecretManagerProvider.get().getOwner().getId();
|
||||
} catch (Exception e) {
|
||||
logger.error("Unable to retrieve user. {} will be used", user);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
setContextByName(DEFAULT_TEST_SCOPE);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
SecretManagerProvider.reset();
|
||||
}
|
||||
|
||||
public String loginUser() throws IOException {
|
||||
|
||||
URL login_url = null;
|
||||
try {
|
||||
login_url = new URL(properties.getProperty("LOGIN_URL"));
|
||||
} catch (MalformedURLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String context = properties.getProperty("LOGIN_CONTEXT");
|
||||
String encoded_context = context.replace("/", "%2F");
|
||||
|
||||
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
Map<String, List<String>> params = new HashMap<String, List<String>>();
|
||||
headers.put("Content-Type", "application/x-www-form-urlencoded");
|
||||
headers.put("X-D4Science-Context", encoded_context);
|
||||
|
||||
params.put("client_id", Collections.singletonList(properties.getProperty("LOGIN_USER_CLIENT_ID")));
|
||||
params.put("username", Collections.singletonList(properties.getProperty("LOGIN_USER_USERNAME")));
|
||||
params.put("password", Collections.singletonList(properties.getProperty("LOGIN_USER_PASS")));
|
||||
params.put("grant_type", Collections.singletonList("password"));
|
||||
params.put("client_secret", Collections.singletonList(properties.getProperty("LOGIN_USER_CLIENT_SECRET")));
|
||||
|
||||
GXHTTPStringRequest request = IdmRestClient.preparePostRequest(login_url, headers, params);
|
||||
GXInboundResponse response;
|
||||
try {
|
||||
response = request.post();
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException("Cannot send request correctly", e);
|
||||
}
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
TypeReference<Map<String,String>> typeRef = new TypeReference<Map<String,String>>() {};
|
||||
String jsonstring = response.getStreamedContentAsString();
|
||||
HashMap<String, String> result = om.readValue(jsonstring, typeRef);
|
||||
|
||||
return result.get("access_token");
|
||||
// return properties.getProperty("TOKEN_USER");
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package org.gcube.idm.client.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.gcube.idm.client.IdmMeClient;
|
||||
import org.gcube.idm.client.model.OwnerInfo;
|
||||
import org.gcube.idm.client.model.UserProfile;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IdmMeClientTest extends ClientContextTest {
|
||||
|
||||
@Test
|
||||
public void getUserEmail() throws IOException {
|
||||
IdmMeClient client = getMeClient();
|
||||
String email = client.getMeEmail();
|
||||
String expected = "m.assante@gmail.com";
|
||||
assertEquals(expected, email);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMeOwner() throws IOException {
|
||||
IdmMeClient client = getMeClient();
|
||||
OwnerInfo owner = client.getMeOwner();
|
||||
|
||||
assertNotNull(owner);
|
||||
assertEquals(properties.getProperty("USER_EMAIL"), owner.getEmail());
|
||||
assertEquals(properties.getProperty("USER_ID"), owner.getId());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMeProfile() throws IOException {
|
||||
IdmMeClient client = getMeClient();
|
||||
UserProfile profile = client.getMeProfile();
|
||||
assertNotNull(profile);
|
||||
assertEquals(properties.getProperty("USER_ID"), profile.getUsername());
|
||||
}
|
||||
|
||||
// public String getUserEmail(String user_id);
|
||||
// public String getUserUsername(String user_id);
|
||||
// public UserProfile getUserProfile(String user_id);
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package org.gcube.idm.client.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.rmi.ServerException;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.idm.client.IdmUsersClient;
|
||||
import org.gcube.idm.client.SearchUsersParams;
|
||||
import org.gcube.idm.common.models.IdmFullUser;
|
||||
import org.gcube.idm.common.models.IdmUser;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IdmUsersClientTest extends ClientContextTest {
|
||||
|
||||
@Test
|
||||
public void getUserEmail() throws ServerException {
|
||||
IdmUsersClient client = getUserClient();
|
||||
String email = client.getUserEmail("alfredo.oliviero");
|
||||
String expected = "alfredo.oliviero@isti.cnr.it";
|
||||
assertEquals(expected, email);
|
||||
}
|
||||
// public String getUserEmail(String user_id);
|
||||
// public String getUserUsername(String user_id);
|
||||
// public TokenInfo getUserOwner(String user_id);
|
||||
// public UserProfile getUserProfile(String user_id);
|
||||
|
||||
@Test
|
||||
public void searchUsernames() throws ServerException {
|
||||
IdmUsersClient client = getUserClient();
|
||||
List<String> usernames = client.searchUsernames(null, 3, null);
|
||||
assertNotNull("expected to receive username", usernames);
|
||||
assertEquals("size expected 3", 3, usernames.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void searchUsers() throws ServerException {
|
||||
IdmUsersClient client = getUserClient();
|
||||
List<IdmUser> users = client.searchUsers(null, 3, null);
|
||||
assertNotNull("expected to receive users", users);
|
||||
assertEquals("size expected 3", 3, users.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void searchFullUsers() throws ServerException {
|
||||
IdmUsersClient client = getUserClient();
|
||||
List<IdmFullUser> users = client.searchFullUsers(null, 3, null);
|
||||
assertNotNull("expected to receive users", users);
|
||||
assertEquals("size expected 3", 3, users.size());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void searchFilteredFullUsers() throws ServerException {
|
||||
IdmUsersClient client = getUserClient();
|
||||
SearchUsersParams params = new SearchUsersParams();
|
||||
params.email="alfredo.oliviero";
|
||||
params.exact = false;
|
||||
List<IdmFullUser> users = client.searchFullUsers(null, 1, params);
|
||||
assertNotNull("expected to receive users", users);
|
||||
assertEquals("size expected 1", 1, users.size());
|
||||
assertEquals("email expected alfredo.oliviero@isti.cnr.it", "alfredo.oliviero@isti.cnr.it", users.get(0).getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkExpireServiceToken() throws ServerException {
|
||||
String expired_user_token = getExpiredServiceToken();
|
||||
IdmUsersClient client = getUserClient(expired_user_token);
|
||||
String email = client.getUserEmail("alfredo.oliviero");
|
||||
String expected = "alfredo.oliviero@isti.cnr.it";
|
||||
assertEquals(expected, email);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package org.gcube.idm.client.test;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.gcube.idm.client.IdmClientFactory;
|
||||
import org.gcube.idm.common.is.InfrastrctureServiceClient;
|
||||
import org.gcube.idm.common.is.IsServerConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.ws.rs.NotFoundException;
|
||||
|
||||
public class IsTest extends ClientContextTest {
|
||||
private static final Logger logger = LoggerFactory.getLogger(IsTest.class);
|
||||
|
||||
IsServerConfig config = new IsServerConfig("http://localhost:8080/idm-service");
|
||||
|
||||
|
||||
// @Test
|
||||
// public void checkContext() throws Exception {
|
||||
// ContextTest.setContextByName("gcube/devsec/devVRE");
|
||||
|
||||
// ApplicationContext ctx = ContextProvider.get();
|
||||
// ContainerContext container = ctx.container();
|
||||
// ContainerConfiguration configuration = container.configuration();
|
||||
|
||||
// String infra_context = "/" + configuration.infrastructure();
|
||||
// logger.debug("Testing Keycloak service IS config");
|
||||
|
||||
// }
|
||||
|
||||
|
||||
//@Test
|
||||
public void testIsIDMConfig() throws Exception {
|
||||
|
||||
String RUNTIME_RESOURCE_NAME = "identity-manager";
|
||||
String CATEGORY = "org.gcube.auth";
|
||||
String END_POINT_NAME = "d4science";
|
||||
boolean IS_ROOT_SERVICE = true;
|
||||
|
||||
IsServerConfig cfg = InfrastrctureServiceClient.serviceConfigFromIS(RUNTIME_RESOURCE_NAME, CATEGORY,
|
||||
END_POINT_NAME, IS_ROOT_SERVICE, current_secret);
|
||||
|
||||
assertNotNull(cfg);
|
||||
}
|
||||
|
||||
// @Test
|
||||
public void testIsIDM() throws Exception {
|
||||
|
||||
logger.debug("Testing Keycloak service IS config");
|
||||
IsServerConfig config = new IsServerConfig("http://localhost:8080/idm-service");
|
||||
|
||||
IdmClientFactory factory = IdmClientFactory.getSingleton();
|
||||
factory.setConfig(config);
|
||||
org.junit.Assert.assertNotNull(factory);
|
||||
|
||||
try {
|
||||
config = factory.fetchIsConfig();
|
||||
} catch (NotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
org.junit.Assert.assertNotNull(config);
|
||||
|
||||
logger.debug("fetched IDM service IS config ");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
context=/gcube
|
||||
type=CLIENT_ID
|
||||
clientId=id.d4science.org
|
||||
/gcube=09c26f24-3c65-4039-9fa0-e5cc4f4032cd
|
||||
|
||||
SERVICE_URL_PROCACCINI = http://146.48.85.179:9999/idm-service/
|
||||
SERVICE_URL_LOCAL = http://localhost:8080/idm-service/
|
||||
|
||||
SERVICE_URL = http://localhost:8080/idm-service/
|
||||
#SERVICE_URL = http://146.48.85.179:9999/idm-service/
|
||||
|
||||
TOKEN_USER = eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJSSklZNEpoNF9qdDdvNmRE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AAqWBojeVP6T_PCrAHBbJQL1l6WeIvsYrbhZfMwe16POBs6I3O6b-5RywonxFY3BMxYq5glUzxd3KqJdFBmnrWHn3hepkBa4Y2rVjFSThkzUcZMtxEUTw_McV96bckonuHWcQeJ4uG7pRswRYItgjbK7Gi9K_M49Uh7PovImtfjUgt3MSHEgTKeK5GMwLIgNmsYhIs7woWc3BvgkrlNyi7BSiKLra_vzQxlF9lVoJLrs9-sSoVHN6Us8s_zvNAV_DIA
|
||||
|
||||
USER_ID = andrea.rossi
|
||||
USER_EMAIL = m.assante@gmail.com
|
||||
|
||||
|
||||
LOGIN_URL = https://accounts.dev.d4science.org/auth/realms/d4science/protocol/openid-connect/token
|
||||
LOGIN_USER_USERNAME = andrea.rossi
|
||||
LOGIN_USER_PASS = gcube321
|
||||
LOGIN_USER_CLIENT_ID = id.d4science.org
|
||||
LOGIN_USER_CLIENT_SECRET = 09c26f24-3c65-4039-9fa0-e5cc4f4032cd
|
||||
LOGIN_CONTEXT = /gcube/devsec/devVRE
|
||||
|
||||
|
Loading…
Reference in New Issue