DownloadPublicKey.java
9.14 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* 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;
}
}
}