Search in sources :

Example 1 with RemotePartner

use of org.jbei.ice.storage.model.RemotePartner in project ice by JBEI.

the class WebPartners method get.

/**
 * Retrieve a partner based on partner token and unique identifier
 * This is mostly intended to validate the token associated with url
 *
 * @param token partner token generated by this ICE instance and sent to other instance
 * @param url   unique identifier for partner instance
 * @return found partner
 */
public RegistryPartner get(String token, String url) {
    String urlToken = AccessTokens.getUrlToken(url);
    if (urlToken == null || token == null || !token.equalsIgnoreCase(urlToken))
        return null;
    RemotePartner remotePartner = dao.getByUrl(url);
    if (remotePartner == null) {
        // likely scenario
        RegistryPartner partner = new RegistryPartner();
        partner.setUrl(url);
        return partner;
    }
    return remotePartner.toDataTransferObject();
}
Also used : RemotePartner(org.jbei.ice.storage.model.RemotePartner) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner)

Example 2 with RemotePartner

use of org.jbei.ice.storage.model.RemotePartner in project ice by JBEI.

the class WebPartners method updateAPIKey.

/**
 * Refreshes the API key for the referenced partner
 *
 * @param userId identifier for user making request. Must have administrative privileges
 * @param id     unique (local) identifier for remote partner whose API key is being refreshed
 * @return null if this instance is not in web of registries
 * @throws PermissionException      if user making request does not have administrative privileges
 * @throws IllegalArgumentException if the partner identifier is invalid (cannot be used to retrieve a valid
 *                                  partner)
 */
public RegistryPartner updateAPIKey(String userId, long id) {
    if (!isInWebOfRegistries())
        return null;
    if (!accountController.isAdministrator(userId))
        throw new PermissionException(userId + " is not an admin");
    RemotePartner partner = dao.get(id);
    if (partner == null) {
        throw new IllegalArgumentException("Cannot retrieve partner with id " + id);
    }
    RegistryPartner thisPartner = getThisInstanceWithNewApiKey();
    if (thisPartner == null) {
        Logger.error("Cannot exchange api token with remote host due to invalid local url");
        return null;
    }
    // contact partner (with new key) to refresh its api key for this partner
    RegistryPartner remotePartner = remoteContact.refreshPartnerKey(thisPartner, partner.getUrl(), partner.getApiKey());
    if (remotePartner == null) {
        // contact failed (keeping existing key)
        Logger.error("Remote contact of partner " + partner.getUrl() + " to update api key failed");
        return null;
    }
    // contact succeeded with return of api key, generate new salt
    partner.setSalt(tokenHash.generateSalt());
    String hash = tokenHash.encrypt(thisPartner.getApiKey() + remotePartner.getUrl(), partner.getSalt());
    partner.setAuthenticationToken(hash);
    // todo : check api key (validate?)
    partner.setApiKey(remotePartner.getApiKey());
    partner = dao.update(partner);
    return partner.toDataTransferObject();
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) RemotePartner(org.jbei.ice.storage.model.RemotePartner) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner)

Example 3 with RemotePartner

use of org.jbei.ice.storage.model.RemotePartner in project ice by JBEI.

the class WebPartners method removeRemotePartner.

/**
 * Delete the partner information specified by the url in the param
 *
 * @param id  unique local identifier of the partner making request.
 * @param url url of partner being deleted
 * @return true if specified partner is successfully removed, false otherwise
 */
public boolean removeRemotePartner(long id, String url) {
    RemotePartner requester = dao.get(id);
    if (requester == null)
        throw new IllegalArgumentException("Could not retrieve partner with local id " + id);
    if (!requester.getUrl().equalsIgnoreCase(url)) {
        throw new PermissionException("Cannot delete another partner's record");
    }
    dao.delete(requester);
    return true;
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) RemotePartner(org.jbei.ice.storage.model.RemotePartner)

Example 4 with RemotePartner

use of org.jbei.ice.storage.model.RemotePartner in project ice by JBEI.

the class WebPartners method updateRemoteAPIKey.

/**
 * Updates the api token of a remote partner using information sent by that partner
 *
 * @param url           URL of partner making request. This is obtained from the old api key
 * @param remotePartner information sent by remote partner
 * @return information about this partner including a new api token
 */
public RegistryPartner updateRemoteAPIKey(String url, RegistryPartner remotePartner) {
    RemotePartner remotePartnerModel = dao.getByUrl(url);
    if (remotePartnerModel == null) {
        Logger.error("Could not find a local record of partner with url " + url);
        // todo : so create a new one?
        return null;
    }
    Logger.info("Refreshing local api key for " + url);
    RegistryPartner thisInstance = getThisInstanceWithNewApiKey();
    remotePartnerModel.setUrl(remotePartner.getUrl());
    if (!StringUtils.isEmpty(remotePartner.getName()))
        remotePartnerModel.setName(remotePartner.getName());
    // todo : no need to validate since url is authenticated
    remotePartnerModel.setApiKey(remotePartner.getApiKey());
    String salt = tokenHash.generateSalt();
    remotePartnerModel.setSalt(salt);
    String hash = tokenHash.encrypt(thisInstance.getApiKey() + remotePartner.getUrl(), salt);
    remotePartnerModel.setAuthenticationToken(hash);
    dao.update(remotePartnerModel);
    return thisInstance;
}
Also used : RemotePartner(org.jbei.ice.storage.model.RemotePartner) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner)

Example 5 with RemotePartner

use of org.jbei.ice.storage.model.RemotePartner in project ice by JBEI.

the class WebPartners method handleRemoteAddRequest.

/**
 * Handles requests from remote ice instances that will like to be in a WoR config with this instance
 * Serves the dual purpose of:
 * <ul>
 * <li>please add me as a partner to your list with token</li>
 * <li>add accepted; use this as the authorization token</li>
 * </ul>
 * <p>
 * Note that the request is rejected if this ICE instance has not opted to be a member of web of
 * registries
 *
 * @param request partner request object containing all information needed with a validated url
 * @return information about this instance to be sent to the remote
 */
protected RegistryPartner handleRemoteAddRequest(RegistryPartner request) {
    if (request == null || StringUtils.isEmpty(request.getApiKey())) {
        Logger.error("Received invalid partner add request");
        return null;
    }
    Logger.info("Processing request to connect by " + request.getUrl());
    String myURL = getThisUri();
    if (request.getUrl().equalsIgnoreCase(myURL))
        return null;
    boolean apiKeyValidates = remoteContact.apiKeyValidates(myURL, request);
    if (!apiKeyValidates) {
        Logger.error("Received api token could not be validated");
        return null;
    }
    // request should contain api key for use to contact third party
    RemotePartner partner = dao.getByUrl(request.getUrl());
    RegistryPartner thisInstance = getThisInstanceWithNewApiKey();
    // create new partner object or update existing with new token hash
    if (partner != null) {
        Logger.info("Updating authentication for existing");
        // validated. update the authorization token
        partner.setApiKey(request.getApiKey());
        partner.setSalt(tokenHash.generateSalt());
        partner.setAuthenticationToken(tokenHash.encrypt(thisInstance.getApiKey() + request.getUrl(), partner.getSalt()));
        dao.update(partner);
    } else {
        // save in db
        request.setStatus(RemotePartnerStatus.APPROVED);
        createRemotePartnerObject(request, thisInstance.getApiKey());
    }
    Logger.info("Successfully added remote partner " + request.getUrl());
    // send information about this instance (with token) as response
    return thisInstance;
}
Also used : RemotePartner(org.jbei.ice.storage.model.RemotePartner) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner)

Aggregations

RemotePartner (org.jbei.ice.storage.model.RemotePartner)34 RegistryPartner (org.jbei.ice.lib.dto.web.RegistryPartner)10 Account (org.jbei.ice.storage.model.Account)8 PermissionException (org.jbei.ice.lib.access.PermissionException)7 AccountTransfer (org.jbei.ice.lib.account.AccountTransfer)6 RemoteUser (org.jbei.ice.lib.dto.web.RemoteUser)4 Folder (org.jbei.ice.storage.model.Folder)4 RemoteAccessModel (org.jbei.ice.storage.model.RemoteAccessModel)4 Test (org.junit.Test)4 PartData (org.jbei.ice.lib.dto.entry.PartData)3 FolderDetails (org.jbei.ice.lib.dto.folder.FolderDetails)3 UserGroup (org.jbei.ice.lib.dto.group.UserGroup)3 Entry (org.jbei.ice.storage.model.Entry)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HibernateException (org.hibernate.HibernateException)2 AccountController (org.jbei.ice.lib.account.AccountController)2 IceRestClient (org.jbei.ice.services.rest.IceRestClient)2 DAOException (org.jbei.ice.storage.DAOException)2 RemoteClientModel (org.jbei.ice.storage.model.RemoteClientModel)2