GoogleMapsSignatureServlet.java 4.75 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletException
 *  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.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.servlets.SlingSafeMethodsServlet
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.contexthub.impl;

import com.adobe.granite.contexthub.api.ContextHub;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.ServletException;
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.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(resourceTypes={"/libs/granite/contexthub/cloudsettings/components/basestore"}, selectors={"signature"}, extensions={"json"}, methods={"GET"})
public class GoogleMapsSignatureServlet
extends SlingSafeMethodsServlet {
    private static final Logger log = LoggerFactory.getLogger(GoogleMapsSignatureServlet.class);
    @Reference
    private ContextHub contextHub;
    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
    final String GOOGLE_MAPS_API_CHANNEL_NAME = "contexthub";

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        ValueMap vm = ResourceUtil.getValueMap((Resource)request.getResource());
        try {
            JSONObject result = new JSONObject();
            String url = request.getParameter("url");
            String clientId = (String)vm.get("googleClientId", String.class);
            String secret = (String)vm.get("googleCryptoKey", String.class);
            if (clientId == null || secret == null) {
                String msg = "Edit your configuration: Google Maps API client id or crypto key is missing.";
                result.put("error", true);
                result.put("message", (Object)"Edit your configuration: Google Maps API client id or crypto key is missing.");
                log.warn("Edit your configuration: Google Maps API client id or crypto key is missing.");
            } else {
                String signature = this.generateSignature(url, clientId, secret);
                if (this.contextHub.isDebug(request)) {
                    result.put("key", (Object)secret);
                    result.put("url", (Object)url);
                }
                result.put("client", (Object)clientId);
                result.put("channel", (Object)"contexthub");
                result.put("signature", (Object)signature);
            }
            response.getWriter().append(result.toString(4));
        }
        catch (Exception e) {
            log.warn("Could not create a signature for Google Maps API: ", (Throwable)e);
            response.getWriter().append("{ \"error\": true }");
        }
    }

    private String generateSignature(String inputUrl, String clientId, String keyString) throws NoSuchAlgorithmException, InvalidKeyException {
        keyString = keyString.replace('-', '+');
        keyString = keyString.replace('_', '/');
        byte[] key = Base64.decodeBase64((String)keyString);
        inputUrl = inputUrl + "&client=" + clientId;
        inputUrl = inputUrl + "&channel=contexthub";
        SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(sha1Key);
        byte[] signatureBytes = mac.doFinal(inputUrl.getBytes());
        String signature = Base64.encodeBase64String((byte[])signatureBytes);
        signature = signature.replace('+', '-');
        signature = signature.replace('/', '_');
        return signature;
    }

    protected void bindContextHub(ContextHub contextHub) {
        this.contextHub = contextHub;
    }

    protected void unbindContextHub(ContextHub contextHub) {
        if (this.contextHub == contextHub) {
            this.contextHub = null;
        }
    }
}