SalesforceSecretServlet.java
8.98 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.crypto.CryptoSupport
* com.day.cq.commons.TidyJSONWriter
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.servlets.SlingSafeMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mcm.salesforce.internal;
import com.adobe.cq.mcm.salesforce.SalesforceClient;
import com.adobe.cq.mcm.salesforce.SalesforceException;
import com.adobe.cq.mcm.salesforce.SalesforceResponse;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.commons.TidyJSONWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/mcm/salesforce/customer"}, propertyPrivate=1), @Property(name="sling.servlet.methods", value={"GET"}, propertyPrivate=1), @Property(name="sling.servlet.extensions", value={"json"}, propertyPrivate=1)})
public class SalesforceSecretServlet
extends SlingSafeMethodsServlet {
static final long serialVersionUID = 6542654;
public static final String TIDY_PARAM = "tidy";
public static final String CUSTOMER_KEY_PARAM = "customer_key";
public static final String CUSTOMER_SECRET_PARAM = "customer_secret";
public static final String REFRESH_TOKEN_PARAM = "refresh_token";
public static final String INSTANCE_URL_PARAM = "instance_url";
public static final String CHECK_TYPE = "checkType";
public static final String AUTHORIZE_REQUEST = "authorize";
public static final String CODE = "code";
public static final String AUTHORIZATION_URL_PARAM = "authorization_url";
public static final String REDIRECT_URI = "redirect_uri";
public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";
public static final String GRANT_TYPE = "grant_type";
public static final String AUTHORIZATION_CODE = "authorization_code";
private final Logger logger = LoggerFactory.getLogger(SalesforceSecretServlet.class);
@Reference
private CryptoSupport cryptoSupport;
private void refreshAccessToken(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
try {
String customerKey = request.getParameter("customer_key");
String customerSecret = request.getParameter("customer_secret");
String refreshToken = request.getParameter("refresh_token");
String instanceUrl = request.getParameter("instance_url");
SalesforceClient salesforceRequest = new SalesforceClient();
if (this.cryptoSupport.isProtected(refreshToken)) {
String decryptedRefreshToken = this.cryptoSupport.unprotect(refreshToken);
salesforceRequest.setRefreshToken(decryptedRefreshToken);
} else {
salesforceRequest.setRefreshToken(refreshToken);
}
salesforceRequest.setClientId(customerKey);
if (this.cryptoSupport.isProtected(customerSecret)) {
String decryptedClientSecret = this.cryptoSupport.unprotect(customerSecret);
salesforceRequest.setClientSecret(decryptedClientSecret);
} else {
salesforceRequest.setClientSecret(customerSecret);
}
salesforceRequest.setInstanceURL(instanceUrl);
TidyJSONWriter out = new TidyJSONWriter((Writer)response.getWriter());
out.setTidy("true".equals(request.getParameter("tidy")));
out.object();
try {
SalesforceResponse salesforceResponse = salesforceRequest.refreshAccessToken();
out.key("success").value(true);
out.key("error").value(false);
out.key("accessToken").value((Object)salesforceRequest.getAccessToken());
out.key("instanceUrl").value((Object)salesforceRequest.getInstanceURL());
}
catch (SalesforceException e) {
out.key("success").value(false);
out.key("error").value(true);
out.key("errorMessage").value((Object)e.getMessage());
this.logger.error("Exception in refreshing access token", (Throwable)e);
}
out.endObject();
}
catch (JSONException e) {
this.logger.error("JSON Exception while validating user credentials: " + e.getMessage());
throw new ServletException("JSON Exception while validating user credentials: " + e.getMessage());
}
catch (CryptoException e) {
this.logger.error("Crypto Exception while validating user credentials: " + e.getMessage());
throw new ServletException("Crypto Exception while validating user credentials: " + e.getMessage());
}
}
private void getAccessToken(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
try {
String customerKey = request.getParameter("customer_key");
String customerSecret = request.getParameter("customer_secret");
String redirectUri = request.getParameter("redirect_uri");
String authorizationCode = request.getParameter("code");
String authorizationUrl = request.getParameter("authorization_url");
SalesforceClient salesforceClient = new SalesforceClient();
salesforceClient.setInstanceURL(authorizationUrl);
salesforceClient.setClientId(customerKey);
if (this.cryptoSupport.isProtected(customerSecret)) {
String decryptedClientSecret = this.cryptoSupport.unprotect(customerSecret);
salesforceClient.setClientSecret(decryptedClientSecret);
} else {
salesforceClient.setClientSecret(customerSecret);
}
salesforceClient.setMethod(SalesforceClient.AvailableMethods.POST);
salesforceClient.setContentType("application/x-www-form-urlencoded");
salesforceClient.addData("code", authorizationCode);
salesforceClient.addData("grant_type", "authorization_code");
salesforceClient.addData("redirect_uri", redirectUri);
SalesforceResponse salesforceResponse = salesforceClient.executeRequest();
JSONObject responseBody = salesforceResponse.getBodyAsJSON();
responseBody.write((Writer)response.getWriter());
}
catch (SalesforceException e) {
e.printStackTrace();
this.logger.error("JSON Exception while validating authorization code: " + e.getMessage());
throw new ServletException("JSON Exception while validating authorization code: " + e.getMessage());
}
catch (JSONException ex) {
this.logger.error("JSON Exception while validating authorization code: " + ex.getMessage());
throw new ServletException("JSON Exception while validating authorization code: " + ex.getMessage());
}
catch (CryptoException cex) {
this.logger.error("Crypto Exception while validating authorization code: " + cex.getMessage());
throw new ServletException("Crypto Exception while validating authorization code: " + cex.getMessage());
}
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
if (request.getParameter("checkType") != null && "authorize".equals(request.getParameter("checkType"))) {
this.getAccessToken(request, response);
} else {
this.refreshAccessToken(request, response);
}
}
protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
}
protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
if (this.cryptoSupport == cryptoSupport) {
this.cryptoSupport = null;
}
}
}