AMSUtils.java 6.88 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  javax.jcr.RepositoryException
 *  org.apache.commons.codec.binary.Base64
 *  org.apache.commons.io.IOUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.mobileservices.impl.util;

import com.adobe.cq.mobile.platform.impl.MobileAppException;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jcr.RepositoryException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AMSUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(AMSUtils.class);
    private static final String AMS_SERVICE_NAME = "mobileservices";
    private static final String DPS_METADATA_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    private static SecureRandom rand = new SecureRandom();

    public static JSONObject getADBMobileConfigJSON(Configuration config) throws JSONException, RepositoryException, IOException {
        Resource content = config.getContentResource();
        Resource adbmobileconfig = content.getChild("adbmobileconfig");
        if (adbmobileconfig != null) {
            InputStream jsonConfig = (InputStream)adbmobileconfig.adaptTo(InputStream.class);
            try {
                String json = IOUtils.toString((InputStream)jsonConfig);
                return new JSONObject(json);
            }
            catch (IOException e) {
                LOGGER.error("Unable to read {}", (Object)adbmobileconfig.getPath());
                throw e;
            }
        }
        LOGGER.warn("Unable to locate a adbmobileconfig entry for the config {}", (Object)config.getPath());
        return null;
    }

    public static String getAMSVersionString(Configuration config) {
        return AMSUtils.getDateAsAMSString(new Date(AMSUtils.getLastModified(config)));
    }

    public static Long getLastModified(Configuration config) {
        long ts = config.getLastModified();
        if (ts == 0) {
            ts = (Long)config.getInherited("cq:lastModified", (Object)ts);
        }
        return ts;
    }

    public static String getDateAsAMSString(Date date) {
        String stringDate = "";
        if (date != null) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            stringDate = formatter.format(date);
        }
        return stringDate;
    }

    public static Configuration getAMSConfiguration(ConfigurationManager configurationManagerService, Page page) {
        Configuration config = null;
        try {
            String[] cloudServiceConfigPathsArray = (String[])page.getProperties().get("cq:cloudserviceconfigs", String[].class);
            if (cloudServiceConfigPathsArray != null) {
                config = configurationManagerService.getConfiguration("mobileservices", cloudServiceConfigPathsArray);
            }
        }
        catch (Exception ex) {
            LOGGER.warn("Failed to get AMS account configuration", (Throwable)ex);
        }
        return config;
    }

    public static String getReportSuites(Configuration configuration) {
        String[] accounts = (String[])configuration.getInherited("reportsuites", (Object)new String[0]);
        String s_account = "";
        boolean first = true;
        for (int i = 0; i < accounts.length; ++i) {
            String rsid = accounts[i].contains(";") ? accounts[i].substring(0, accounts[i].indexOf(";")) : accounts[i];
            s_account = s_account + (first ? "" : ",") + rsid;
            first = false;
        }
        return s_account;
    }

    public static String generateWSSEHeader(String clientId, String clientSecret) throws IOException, NoSuchAlgorithmException {
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
        StringBuffer wsseHeader = new StringBuffer();
        String created = dateFormatter.format(new Date());
        ByteArrayOutputStream digest = new ByteArrayOutputStream(40);
        byte[] nonce = new byte[20];
        rand.nextBytes(nonce);
        digest.write(nonce);
        digest.write(created.getBytes());
        digest.write(clientSecret.getBytes());
        wsseHeader.append("UsernameToken Username=\"");
        wsseHeader.append(clientId);
        wsseHeader.append("\", PasswordDigest=\"");
        wsseHeader.append(Base64.encodeBase64String((byte[])AMSUtils.toSHA1(digest.toByteArray())));
        wsseHeader.append("\", Nonce=\"");
        wsseHeader.append(Base64.encodeBase64String((byte[])nonce));
        wsseHeader.append("\", Created=\"");
        wsseHeader.append(created);
        wsseHeader.append("\"");
        return wsseHeader.toString();
    }

    public static Resource getCloudServiceConfiguration(Resource resource) {
        if (resource == null || ResourceUtil.isNonExistingResource((Resource)resource) || resource.getParent() == null) {
            return null;
        }
        Resource nextResource = resource;
        while (nextResource.getParent() != null && nextResource.getParent().getParent() != null && !nextResource.getParent().getParent().getPath().equals("/etc/cloudservices")) {
            nextResource = nextResource.getParent();
        }
        return nextResource;
    }

    public static boolean verifyMobileServicesFrameworkResource(Resource resource, boolean throwException) throws MobileAppException {
        Resource content = resource.getChild("jcr:content");
        boolean valid = content.isResourceType("mobileapps/components/mobileservicesframework");
        if (!valid && throwException) {
            String message = "Invalid resource.  It must be an AMS Framework resource.";
            LOGGER.error(message);
            throw new MobileAppException(message);
        }
        return valid;
    }

    private static byte[] toSHA1(byte[] data) throws NoSuchAlgorithmException {
        MessageDigest md = null;
        md = MessageDigest.getInstance("SHA-1");
        return md.digest(data);
    }
}