DownloadPublicKey.java 9.14 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoException
 *  com.adobe.granite.crypto.CryptoSupport
 *  com.adobe.granite.keystore.KeyStoreService
 *  com.day.cq.i18n.I18n
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.codec.binary.Base64
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dam.mac.sync.helper.impl.servlets;

import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfiguration;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfigurationResolver;
import com.adobe.cq.dam.mac.sync.helper.impl.util.OAuthUtil;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.adobe.granite.keystore.KeyStoreService;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.PublicKey;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.codec.binary.Base64;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(methods={"GET"}, resourceTypes={"dam/components/marketingcloud/config", "dam/components/mediaportal/config"}, selectors={"pem"}, extensions={"html"})
public class DownloadPublicKey
extends SlingAllMethodsServlet {
    private static final Logger LOG = LoggerFactory.getLogger(DownloadPublicKey.class);
    @Reference
    private KeyStoreService keyStoreService = null;
    @Reference
    private CryptoSupport cryptoSupport = null;
    @Reference
    private MACTenantConfigurationResolver mtcr = null;
    private static final String DOWNLOAD_MODE_PARAM = "mode";
    private static final String DOWNLOAD_MODE_VALUE_TEXT = "text";
    private static final String MAC_CONFIG_RESOURCE_TYPE = "dam/components/marketingcloud/config";
    private static final String MP_CONFIG_RESOURCE_TYPE = "dam/components/mediaportal/config";
    private static final String MP_KEY_PAIR_ALIAS = "mpreplication";

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        block20 : {
            Object bos = null;
            JSONObject ret = new JSONObject();
            I18n i18n = new I18n((HttpServletRequest)request);
            boolean error = false;
            String errorMessage = null;
            PrintWriter out = response.getWriter();
            boolean isTextMode = "text".equals(request.getParameter("mode"));
            boolean included = request.getAttribute("org.apache.sling.api.include.servlet") != null || isTextMode;
            try {
                Resource configPage = request.getResource().getParent();
                if (configPage != null) {
                    MACTenantConfiguration macConfiguration = this.mtcr.getConfiguration(request.getResourceResolver(), configPage.getPath());
                    if (macConfiguration.getDAMUser() == null) {
                        errorMessage = i18n.get("DAM User is not configured");
                        try {
                            ret.put("error", (Object)errorMessage);
                        }
                        catch (JSONException je) {
                            LOG.error("JSON exception while building response", (Throwable)je);
                        }
                        out.write(ret.toString());
                        return;
                    }
                    PublicKey key = null;
                    key = !request.getResource().getResourceType().startsWith("dam/components/mediaportal/config") ? OAuthUtil.getPublicKey(request.getResourceResolver(), macConfiguration.getDAMUser(), this.cryptoSupport, this.keyStoreService) : OAuthUtil.getPublicKey(request.getResourceResolver(), macConfiguration.getDAMUser(), this.cryptoSupport, this.keyStoreService, "mpreplication");
                    this.writeHeader(out);
                    this.writeEncoded(out, key.getEncoded());
                    this.writeFooter(out);
                    if (!included) {
                        response.setContentType("application/text");
                        response.setHeader("Content-Disposition", "attachment; filename=\"" + macConfiguration.getDAMUser() + ".pem" + "\"");
                    }
                    break block20;
                }
                response.setStatus(500);
                errorMessage = i18n.get("Invalid marketing cloud config {}", request.getResource().getPath());
                try {
                    ret.put("error", (Object)errorMessage);
                }
                catch (JSONException je) {
                    LOG.error("JSON exception while building response", (Throwable)je);
                }
                out.write(ret.toString());
                return;
            }
            catch (CryptoException e) {
                response.setStatus(500);
                response.setContentType("application/json");
                LOG.error("Error in retrieving the keypair", (Throwable)e);
                errorMessage = i18n.get("Error in retrieving the keypair");
                try {
                    ret.put("error", (Object)errorMessage);
                }
                catch (JSONException je) {
                    LOG.error("JSON exception while building response", (Throwable)je);
                }
                out.write(ret.toString());
            }
            catch (IOException e) {
                response.setStatus(500);
                response.setContentType("application/json");
                LOG.error("Error in generating public key", (Throwable)e);
                errorMessage = i18n.get("Error in generating public key");
                try {
                    ret.put("error", (Object)errorMessage);
                }
                catch (JSONException je) {
                    LOG.error("JSON exception while building response", (Throwable)je);
                }
                out.write(ret.toString());
            }
            finally {
                if (out != null && !included) {
                    out.close();
                }
            }
        }
    }

    private PrintWriter writeHeader(PrintWriter out) throws IOException {
        return this.writeNewLine(this.writeString(out, "-----BEGIN PUBLIC KEY-----"));
    }

    private PrintWriter writeString(PrintWriter out, String str) throws IOException {
        out.write(str);
        return out;
    }

    private PrintWriter writeNewLine(PrintWriter out) throws IOException {
        return this.writeString(out, String.format("%n", new Object[0]));
    }

    private PrintWriter writeFooter(PrintWriter out) throws IOException {
        return this.writeNewLine(this.writeString(out, "-----END PUBLIC KEY-----"));
    }

    private PrintWriter writeEncoded(PrintWriter out, byte[] bytes) throws IOException {
        char[] buf = new char[64];
        Base64 base64 = new Base64();
        bytes = base64.encode(bytes);
        for (int i = 0; i < bytes.length; i += buf.length) {
            int index;
            for (index = 0; index != buf.length && i + index < bytes.length; ++index) {
                buf[index] = bytes[i + index];
            }
            out.write(buf, 0, index);
            this.writeNewLine(out);
        }
        return out;
    }

    protected void bindKeyStoreService(KeyStoreService keyStoreService) {
        this.keyStoreService = keyStoreService;
    }

    protected void unbindKeyStoreService(KeyStoreService keyStoreService) {
        if (this.keyStoreService == keyStoreService) {
            this.keyStoreService = null;
        }
    }

    protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
        this.cryptoSupport = cryptoSupport;
    }

    protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
        if (this.cryptoSupport == cryptoSupport) {
            this.cryptoSupport = null;
        }
    }

    protected void bindMtcr(MACTenantConfigurationResolver mACTenantConfigurationResolver) {
        this.mtcr = mACTenantConfigurationResolver;
    }

    protected void unbindMtcr(MACTenantConfigurationResolver mACTenantConfigurationResolver) {
        if (this.mtcr == mACTenantConfigurationResolver) {
            this.mtcr = null;
        }
    }
}