OAuthProfileImportServlet.java
8.58 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoSupport
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.Servlet
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* 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.jackrabbit.api.security.user.User
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.auth.oauth.impl.servlet;
import com.adobe.granite.auth.oauth.OAuthIdInUseException;
import com.adobe.granite.auth.oauth.OAuthManager;
import com.adobe.granite.auth.oauth.Provider;
import com.adobe.granite.auth.oauth.impl.helper.OAuthHelper;
import com.adobe.granite.auth.oauth.impl.helper.OAuthUser;
import com.adobe.granite.auth.oauth.impl.helper.ProviderConfigManager;
import com.adobe.granite.auth.oauth.impl.helper.RequestHelper;
import com.adobe.granite.crypto.CryptoSupport;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
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.jackrabbit.api.security.user.User;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1)
@Service(value={Servlet.class})
@Properties(value={@Property(name="service.description", value={"OAuth profile importing Servlet"}), @Property(name="sling.servlet.resourceTypes", value={"social/oauth/connect"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class OAuthProfileImportServlet
extends SlingAllMethodsServlet {
private Logger log;
private static final String PARAM_CONFIG_ID = "configid";
private String repositoryId;
@Reference
private OAuthManager oauthManager;
@Reference
private SlingRepository repository;
@Reference
private CryptoSupport cryptoSupport;
@Reference
private ProviderConfigManager providerConfigManager;
public OAuthProfileImportServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Activate
private void activate() throws Exception {
}
@Deactivate
private void deactivate() throws Exception {
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
if (RequestHelper.isInitialCall((HttpServletRequest)request, false)) {
this.requestAuthorization(request, response);
} else if (RequestHelper.isAuthzCode((HttpServletRequest)request, false)) {
this.requestAccessToken(request, response);
} else {
response.getWriter().println("Connected.");
}
}
protected void requestAuthorization(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
String configId = request.getParameter("configid");
this.log.debug("requestAuthorization: configid is: {}", (Object)configId);
String state = "";
OAuthHelper helper = this.providerConfigManager.getHelper(configId);
Provider provider = this.oauthManager.getProvider(configId);
if (helper != null && provider != null) {
helper.requestAuthorization((HttpServletRequest)request, (HttpServletResponse)response, provider, "", false);
} else {
this.log.error("requestAuthorization: invalid config: helper is {} provider is {} ", (Object)helper, (Object)provider);
}
}
protected void requestAccessToken(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
String configId = RequestHelper.getConfigId((HttpServletRequest)request);
OAuthHelper helper = this.providerConfigManager.getHelper(configId);
Provider provider = this.oauthManager.getProvider(configId);
Object error = null;
if (helper != null && provider != null) {
OAuthUser oauthUser = helper.requestAccessCode(provider, (HttpServletRequest)request, (HttpServletResponse)response, false, true);
this.updateUserDataAndRedirect(oauthUser, provider, helper, request, response);
} else {
this.log.error("requestAuthorization: invalid config: helper is {} provider is {} ", (Object)helper, (Object)provider);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void updateUserDataAndRedirect(OAuthUser oauthUser, Provider provider, OAuthHelper helper, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
String error;
block11 : {
error = null;
if (oauthUser != null) {
Session serviceSession = null;
try {
serviceSession = this.repository.loginService("", null);
User user = provider.getCurrentUser(request);
user = helper.createOrUpdateCRXUser(serviceSession, this.cryptoSupport, provider, (HttpServletRequest)request, oauthUser, user);
if (user != null) {
helper.setAuthorizedId((HttpServletRequest)request, (HttpServletResponse)response, user.getID());
break block11;
}
error = "User " + oauthUser.getId() + " cannot be created/updated.";
}
catch (RepositoryException e) {
this.log.error("requestAccessToken: failed to assign user profile data", (Throwable)e);
IOException ex = new IOException("Failed requesting access token");
ex.initCause((Throwable)e);
throw ex;
}
catch (OAuthIdInUseException e) {
error = e.getMessage();
}
finally {
if (serviceSession != null && serviceSession.isLive()) {
serviceSession.logout();
}
}
}
this.log.error("requestAccessToken: could not retrieve user");
}
if (error != null) {
request.setAttribute("com.adobe.granite.auth.oauth.OAuthImportError", (Object)error);
} else {
RequestHelper.handleRedirectAfterAuthentication((HttpServletRequest)request, (HttpServletResponse)response);
}
}
protected void bindOauthManager(OAuthManager oAuthManager) {
this.oauthManager = oAuthManager;
}
protected void unbindOauthManager(OAuthManager oAuthManager) {
if (this.oauthManager == oAuthManager) {
this.oauthManager = null;
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
}
protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
if (this.cryptoSupport == cryptoSupport) {
this.cryptoSupport = null;
}
}
protected void bindProviderConfigManager(ProviderConfigManager providerConfigManager) {
this.providerConfigManager = providerConfigManager;
}
protected void unbindProviderConfigManager(ProviderConfigManager providerConfigManager) {
if (this.providerConfigManager == providerConfigManager) {
this.providerConfigManager = null;
}
}
}