GoogleMapsSignatureServlet.java
4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* 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;
}
}
}