ProviderBase.java
11.2 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Property
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.User
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.osgi.OsgiUtil
* org.apache.sling.commons.osgi.ServiceUtil
* org.osgi.service.component.ComponentContext
* org.scribe.builder.api.Api
* org.scribe.model.OAuthRequest
* org.scribe.model.Response
* org.scribe.model.Verb
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.auth.oauth.impl;
import com.adobe.granite.auth.oauth.Provider;
import com.adobe.granite.auth.oauth.ProviderExtension;
import com.adobe.granite.auth.oauth.ProviderType;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.commons.osgi.ServiceUtil;
import org.osgi.service.component.ComponentContext;
import org.scribe.builder.api.Api;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Verb;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public abstract class ProviderBase
implements Provider {
@Property
protected static final String PROP_OAUTH_PROVIDER_ID = "oauth.provider.id";
private final Logger log;
private String name;
private String id;
private ProviderType type;
private Api api;
private String detailsURL;
private String[] extendedDetailsURLs;
private static final String[] DEFAULT_EXTENDED_URLS = new String[0];
private final ReadWriteLock lock;
private SortedMap<Comparable<Object>, ProviderExtension> registeredProviderExtensionHandlers;
protected ProviderBase(ProviderType type, Api api, String detailsURL) {
this.log = LoggerFactory.getLogger(this.getClass());
this.lock = new ReentrantReadWriteLock();
this.registeredProviderExtensionHandlers = new TreeMap(Collections.reverseOrder());
this.type = type;
this.api = api;
this.detailsURL = detailsURL;
}
@Override
public ProviderType getType() {
return this.type;
}
@Override
public Api getApi() {
return this.api;
}
@Override
public String getDetailsURL() {
return this.detailsURL;
}
@Override
public String[] getExtendedDetailsURLs(String scope) {
return this.extendedDetailsURLs;
}
@Override
public String[] getExtendedDetailsURLs(String scope, String userId, Map<String, Object> props) {
return DEFAULT_EXTENDED_URLS;
}
@Override
public String getId() {
return this.id;
}
@Override
public String getName() {
return this.name;
}
public String mapProperty(String property) {
return "profile/" + property;
}
public Object mapValue(String key, String value) {
return value;
}
@Override
public String mapUserId(String userId, Map<String, Object> props) {
ProviderExtension providerExtension = this.getProviderExtension();
if (providerExtension != null) {
String delegatedUserId = providerExtension.mapUserId(userId, props);
return delegatedUserId != null ? delegatedUserId : this.getDefaultUserId(userId, props);
}
return this.getDefaultUserId(userId, props);
}
@Override
public String getUserFolderPath(String userId, String clientId, Map<String, Object> props) {
ProviderExtension providerExtension = this.getProviderExtension();
if (providerExtension != null) {
String userFolderPath = providerExtension.getUserFolderPath(userId, clientId, props);
return userFolderPath != null ? userFolderPath : this.getDefaultUserFolderPath(userId, clientId, props);
}
return this.getDefaultUserFolderPath(userId, clientId, props);
}
@Override
public Map<String, Object> mapProperties(String srcUrl, String clientId, Map<String, Object> existing, Map<String, String> newProperties) {
HashMap<String, Object> mapped = new HashMap<String, Object>();
mapped.putAll(existing);
for (Map.Entry<String, String> prop : newProperties.entrySet()) {
String mappedKey = this.mapProperty(prop.getKey());
Object mappedValue = this.mapValue(mappedKey, prop.getValue());
mapped.put(mappedKey, mappedValue);
}
if (!mapped.containsKey("profile/avatar")) {
Object mappedValue = this.mapValue("profile/avatar", this.id);
mapped.put("profile/avatar", mappedValue);
}
return mapped;
}
@Override
public String getAccessTokenPropertyPath(String clientId) {
return "profile/app-" + clientId;
}
@Override
public User getCurrentUser(SlingHttpServletRequest request) {
Authorizable authorizable = (Authorizable)request.adaptTo(Authorizable.class);
if (authorizable != null && !authorizable.isGroup()) {
return (User)authorizable;
}
return null;
}
@Override
public void onUserCreate(User user) {
ProviderExtension providerExtension = this.getProviderExtension();
if (providerExtension != null) {
providerExtension.onUserCreate(user);
}
}
@Override
public void onUserUpdate(User user) {
ProviderExtension providerExtension = this.getProviderExtension();
if (providerExtension != null) {
providerExtension.onUserUpdate(user);
}
}
@Override
public OAuthRequest getProtectedDataRequest(String url) {
return new OAuthRequest(Verb.GET, url);
}
@Override
public Map<String, String> parseProfileDataResponse(Response response) throws IOException {
String body = null;
try {
body = response.getBody();
JSONObject json = new JSONObject(body);
HashMap<String, String> newProps = new HashMap<String, String>();
Iterator keys = json.keys();
while (keys.hasNext()) {
String key = (String)keys.next();
newProps.put(key, json.optString(key));
}
return newProps;
}
catch (JSONException je) {
this.log.debug("problem parsing JSON; response body was: {}", (Object)body);
throw new IOException(je.toString());
}
catch (Exception e) {
this.log.error("Exception while parsing profile data");
throw new IOException(e.toString());
}
}
@Override
public String getUserIdProperty() {
return "id";
}
@Override
public String getOAuthIdPropertyPath(String clientId) {
return "oauth/oauthid-" + clientId;
}
@Activate
protected void activate(ComponentContext componentContext) throws Exception {
this.name = OsgiUtil.toString(componentContext.getProperties().get("service.description"), (String)"");
this.id = OsgiUtil.toString(componentContext.getProperties().get("oauth.provider.id"), (String)"");
this.log.debug("activating provider id {}", (Object)this.id);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Deactivate
protected void deactivate(ComponentContext componentContext) throws Exception {
this.log.debug("deactivating provider id {}", (Object)this.id);
this.lock.writeLock().lock();
try {
this.registeredProviderExtensionHandlers.clear();
}
finally {
this.lock.writeLock().unlock();
}
}
@Override
public String getValidateTokenUrl(String clientId, String token) {
this.log.info("This provider doesn't support the validation of a token");
return null;
}
@Override
public boolean isValidToken(String responseBody, String clientId, String tokenType) {
this.log.info("This provider doesn't support the validation of a token");
return false;
}
@Override
public String getUserIdFromValidateTokenResponseBody(String responseBody) {
this.log.info("This provider doesn't support the validation of a token");
return null;
}
@Override
public String getErrorDescriptionFromValidateTokenResponseBody(String responseBody) {
this.log.info("This provider doesn't support the validation of a token");
return null;
}
private String getDefaultUserId(String userId, Map<String, Object> props) {
return this.getId() + "-" + userId;
}
private String getDefaultUserFolderPath(String userId, String clientId, Map<String, Object> props) {
return this.getId() + "/" + userId.substring(0, 4);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindProviderExtension(ProviderExtension providerExtension, Map<String, Object> properties) {
this.lock.writeLock().lock();
try {
this.registeredProviderExtensionHandlers.put(ServiceUtil.getComparableForServiceRanking(properties), providerExtension);
}
finally {
this.lock.writeLock().unlock();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindProviderExtension(ProviderExtension providerExtension, Map<String, Object> properties) {
this.lock.writeLock().lock();
try {
this.registeredProviderExtensionHandlers.remove(ServiceUtil.getComparableForServiceRanking(properties));
}
finally {
this.lock.writeLock().unlock();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private ProviderExtension getProviderExtension() {
this.lock.readLock().lock();
try {
for (ProviderExtension pe : this.registeredProviderExtensionHandlers.values()) {
if (pe.getId() == null || !pe.getId().equals(this.getId())) continue;
ProviderExtension providerExtension = pe;
return providerExtension;
}
}
finally {
this.lock.readLock().unlock();
}
return null;
}
}