OAuthAuthenticationHandler.java
11.5 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoSupport
* com.day.crx.security.token.TokenUtil
* javax.jcr.RepositoryException
* 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.ConfigurationPolicy
* 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.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.auth.core.spi.AbstractAuthenticationHandler
* org.apache.sling.auth.core.spi.AuthenticationHandler
* org.apache.sling.auth.core.spi.AuthenticationInfo
* org.apache.sling.auth.core.spi.DefaultAuthenticationFeedbackHandler
* org.apache.sling.jcr.api.SlingRepository
* org.apache.sling.settings.SlingSettingsService
* org.osgi.framework.BundleContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.auth.oauth.impl;
import com.adobe.granite.auth.oauth.HandlerRedirect;
import com.adobe.granite.auth.oauth.OAuthManager;
import com.adobe.granite.auth.oauth.impl.ProfileValidators;
import com.adobe.granite.auth.oauth.impl.helper.OAuthHelper;
import com.adobe.granite.auth.oauth.impl.helper.ProviderConfig;
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 com.day.crx.security.token.TokenUtil;
import java.util.Map;
import javax.jcr.RepositoryException;
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.ConfigurationPolicy;
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.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.auth.core.spi.AbstractAuthenticationHandler;
import org.apache.sling.auth.core.spi.AuthenticationHandler;
import org.apache.sling.auth.core.spi.AuthenticationInfo;
import org.apache.sling.auth.core.spi.DefaultAuthenticationFeedbackHandler;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
@Component(metatype=1, policy=ConfigurationPolicy.REQUIRE)
@Service(value={AuthenticationHandler.class})
@Properties(value={@Property(name="path", value={"/"}), @Property(name="service.ranking", intValue={5000})})
public class OAuthAuthenticationHandler
extends AbstractAuthenticationHandler {
private static final String AUTH_TYPE = "OAUTH";
@Property(name="service.description")
private static final String DESCRIPTION = "OAuth Authentication Handler";
private final Logger log;
@Reference
private SlingRepository repository;
@Reference
private CryptoSupport cryptoSupport;
@Reference
private OAuthManager oauthManager;
@Reference
private SlingSettingsService settings;
@Reference
private ProfileValidators profileValidators;
private String repositoryId;
@Reference
private ProviderConfigManager providerConfigManager;
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
private volatile HandlerRedirect handlerRedirect;
public OAuthAuthenticationHandler() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Activate
private void activate(BundleContext context, Map<String, Object> config) {
this.repositoryId = RequestHelper.getRepositoryId(this.repository, this.settings);
this.log.info("activate: Supporting tokens bound to Repository (Cluster) {}", (Object)this.repositoryId);
}
@Deactivate
private void deactivate() {
}
/*
* Exception decompiling
*/
public AuthenticationInfo extractCredentials(HttpServletRequest request, HttpServletResponse response) {
// This method has failed to decompile. When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
// org.benf.cfr.reader.util.ConfusedCFRException: Tried to end blocks [2[TRYBLOCK]], but top level block is 7[CATCHBLOCK]
// org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.processEndingBlocks(Op04StructuredStatement.java:397)
// org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:449)
// org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:2877)
// org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:825)
// org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:217)
// org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:162)
// org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:95)
// org.benf.cfr.reader.entities.Method.analyse(Method.java:355)
// org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:768)
// org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:700)
// org.benf.cfr.reader.Main.doJar(Main.java:134)
// org.benf.cfr.reader.Main.main(Main.java:189)
throw new IllegalStateException("Decompilation failed");
}
public boolean requestCredentials(HttpServletRequest request, HttpServletResponse response) {
return false;
}
public void dropCredentials(HttpServletRequest request, HttpServletResponse response) {
this.setAuthorizedId(request, response, null);
RequestHelper.removeConfigId(request, response);
RequestHelper.removeAuthenticatedConfigId(request, response);
}
public boolean authenticationSucceeded(HttpServletRequest request, HttpServletResponse response, AuthenticationInfo authInfo) {
boolean result;
if (!RequestHelper.hasAuthzCode(request)) {
return false;
}
ResourceResolver resolver = (ResourceResolver)request.getAttribute("org.apache.sling.auth.core.ResourceResolver");
if (resolver != null) {
this.setAuthorizedId(request, response, resolver.getUserID());
} else {
this.log.warn("authenticationSucceeded: ResourceResolver missing from request, cannot cache user id");
}
if (DefaultAuthenticationFeedbackHandler.handleRedirect((HttpServletRequest)request, (HttpServletResponse)response)) {
result = false;
} else {
RequestHelper.handleRedirectAfterAuthentication(request, response);
result = true;
}
return result;
}
public void authenticationFailed(HttpServletRequest request, HttpServletResponse response, AuthenticationInfo authInfo) {
this.dropCredentials(request, response);
}
public String toString() {
return "OAuth Authentication Handler";
}
private void setupAuthenticationFailure(HttpServletRequest request, HttpServletResponse response) {
OAuthAuthenticationHandler.setLoginResourceAttribute((HttpServletRequest)request, (String)null);
this.dropCredentials(request, response);
request.setAttribute("j_reason", (Object)"Authentication Failed");
}
private AuthenticationInfo createAuthenticationInfo(HttpServletRequest request, HttpServletResponse response, String userId) {
try {
return TokenUtil.createCredentials((HttpServletRequest)request, (HttpServletResponse)response, (SlingRepository)this.repository, (String)userId, (boolean)true);
}
catch (RepositoryException e) {
this.log.error("Unable to create token credentials", (Throwable)e);
return new AuthenticationInfo("OAUTH", userId);
}
}
private void setAuthorizedId(HttpServletRequest request, HttpServletResponse response, String userId) {
OAuthHelper helper;
Object attribute = request.getAttribute("oauth-configId");
String configId = attribute instanceof String ? (String)attribute : RequestHelper.getConfigId(request);
OAuthHelper oAuthHelper = helper = configId == null ? null : this.providerConfigManager.getHelper(configId);
if (helper != null) {
helper.setAuthorizedId(request, response, userId);
RequestHelper.storeAuthenticatedConfigId(configId, helper.getProviderConfig().getCookieMaxAge(), request, response);
} else {
this.log.debug("No OauthHelper found under configId {}", (Object)configId);
}
}
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 bindOauthManager(OAuthManager oAuthManager) {
this.oauthManager = oAuthManager;
}
protected void unbindOauthManager(OAuthManager oAuthManager) {
if (this.oauthManager == oAuthManager) {
this.oauthManager = null;
}
}
protected void bindSettings(SlingSettingsService slingSettingsService) {
this.settings = slingSettingsService;
}
protected void unbindSettings(SlingSettingsService slingSettingsService) {
if (this.settings == slingSettingsService) {
this.settings = null;
}
}
protected void bindProfileValidators(ProfileValidators profileValidators) {
this.profileValidators = profileValidators;
}
protected void unbindProfileValidators(ProfileValidators profileValidators) {
if (this.profileValidators == profileValidators) {
this.profileValidators = null;
}
}
protected void bindProviderConfigManager(ProviderConfigManager providerConfigManager) {
this.providerConfigManager = providerConfigManager;
}
protected void unbindProviderConfigManager(ProviderConfigManager providerConfigManager) {
if (this.providerConfigManager == providerConfigManager) {
this.providerConfigManager = null;
}
}
protected void bindHandlerRedirect(HandlerRedirect handlerRedirect) {
this.handlerRedirect = handlerRedirect;
}
protected void unbindHandlerRedirect(HandlerRedirect handlerRedirect) {
if (this.handlerRedirect == handlerRedirect) {
this.handlerRedirect = null;
}
}
}