ProviderConfigManagerImpl.java
9.7 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoSupport
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* 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.commons.osgi.OsgiUtil
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceRegistration
* org.osgi.service.cm.ConfigurationException
* org.osgi.service.cm.ManagedServiceFactory
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.auth.oauth.impl.helper;
import com.adobe.granite.auth.oauth.Provider;
import com.adobe.granite.auth.oauth.ProviderType;
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.oauth1a.Oauth1aHelper;
import com.adobe.granite.auth.oauth.impl.oauth2.Oauth2Helper;
import com.adobe.granite.crypto.CryptoSupport;
import java.util.Collection;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
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.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.commons.osgi.OsgiUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;
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, label="Adobe Granite ProviderConfigManager Service", description="Private service for managing Provider, OAuthHelper, and ProviderConfig caches.", name="com.adobe.granite.auth.oauth.impl.helper.ProviderConfigManager")
@Service(value={ProviderConfigManager.class})
@Reference(name="oauthProvider", referenceInterface=Provider.class, bind="bindOauthProvider", unbind="unbindOauthProvider", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
public class ProviderConfigManagerImpl
implements ProviderConfigManager {
private final Logger log;
private ServiceRegistration providerConfigService;
private final Map<String, OAuthHelper> helperCache;
private final Map<String, ProviderConfig> configurations;
private final Map<String, Provider> providerCache;
private int cookieMaxAge;
private int loginTimeout;
@Property
private static final String PROP_COOKIE_LOGIN_TIMEOUT = "oauth.cookie.login.timeout";
private static final int DEFAULT_COOKIE_LOGIN_TIMEOUT = 60;
@Property
private static final String PROP_COOKIE_MAX_AGE = "oauth.cookie.max.age";
private static final int DEFAULT_COOKIE_MAX_AGE = -1;
@Reference
private CryptoSupport cryptoSupport;
public ProviderConfigManagerImpl() {
this.log = LoggerFactory.getLogger(this.getClass());
this.helperCache = new ConcurrentHashMap<String, OAuthHelper>();
this.configurations = new HashMap<String, ProviderConfig>();
this.providerCache = new HashMap<String, Provider>();
}
@Activate
private void activate(BundleContext context, Map<String, Object> config) {
this.log.info("activating ProviderConfigManager");
this.cookieMaxAge = OsgiUtil.toInteger((Object)config.get("oauth.cookie.max.age"), (int)-1);
this.loginTimeout = OsgiUtil.toInteger((Object)config.get("oauth.cookie.login.timeout"), (int)60);
ProviderConfigService msf = new ProviderConfigService();
Hashtable<String, Object> props = new Hashtable<String, Object>();
props.put("service.pid", "com.adobe.granite.auth.oauth.provider");
props.put("service.vendor", config.get("service.vendor"));
props.put("service.description", "OAuth Provider Configurator");
this.providerConfigService = context.registerService(ManagedServiceFactory.class.getName(), (Object)msf, props);
}
@Deactivate
private void deactivate() {
this.log.debug("deactivating ProviderConfigManager");
this.configurations.clear();
this.providerCache.clear();
this.helperCache.clear();
if (this.providerConfigService != null) {
this.providerConfigService.unregister();
this.providerConfigService = null;
}
}
@Override
public OAuthHelper getHelper(String configId) {
return this.helperCache.get(configId);
}
@Override
public Provider getProvider(String configId) {
ProviderConfig config = this.configurations.get(configId);
return config == null ? null : this.providerCache.get(config.getProviderId());
}
@Override
public Iterable<ProviderConfig> getProviderConfigs() {
return this.configurations.values();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindOauthProvider(Provider newProvider, Map<String, Object> properties) {
Provider existingProvider = this.providerCache.get(newProvider.getId());
if (existingProvider != null) {
this.log.error("provider id was not unique:{}, matched id of existing provider {}", (Object)newProvider.getId(), (Object)existingProvider.toString());
return;
}
Map<String, Provider> map = this.providerCache;
synchronized (map) {
this.providerCache.put(newProvider.getId(), newProvider);
for (Map.Entry<String, ProviderConfig> entry : this.configurations.entrySet()) {
if (!entry.getValue().getProviderId().equals(newProvider.getId())) continue;
OAuthHelper helper = this.createHelper(newProvider.getType(), entry.getValue());
this.helperCache.put(entry.getKey(), helper);
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindOauthProvider(Provider provider, Map<String, Object> properties) {
Map<String, Provider> map = this.providerCache;
synchronized (map) {
this.providerCache.remove(provider.getId());
for (Map.Entry<String, ProviderConfig> entry : this.configurations.entrySet()) {
if (!entry.getValue().getProviderId().equals(provider.getId())) continue;
this.helperCache.remove(entry.getKey());
}
}
}
private OAuthHelper createHelper(ProviderType type, ProviderConfig config) {
switch (type) {
case OAUTH1A: {
return new Oauth1aHelper(config);
}
case OAUTH2: {
return new Oauth2Helper(config);
}
}
this.log.error("unsupported ProviderType:" + (Object)((Object)type));
return null;
}
protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
}
protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
if (this.cryptoSupport == cryptoSupport) {
this.cryptoSupport = null;
}
}
private class ProviderConfigService
implements ManagedServiceFactory {
private ProviderConfigService() {
}
public String getName() {
return "com.adobe.granite.auth.oauth.provider";
}
public void deleted(String pid) {
Iterator it = ProviderConfigManagerImpl.this.configurations.entrySet().iterator();
while (it.hasNext()) {
ProviderConfig config = (ProviderConfig)it.next().getValue();
if (!config.getPid().equals(pid)) continue;
String configId = config.getConfigId();
it.remove();
ProviderConfigManagerImpl.this.helperCache.remove(configId);
}
}
public void updated(String pid, Dictionary properties) throws ConfigurationException {
ProviderConfig config = new ProviderConfig(pid, ProviderConfigManagerImpl.this.loginTimeout, ProviderConfigManagerImpl.this.cookieMaxAge, properties, ProviderConfigManagerImpl.this.cryptoSupport);
String configId = config.getConfigId();
ProviderConfig existing = (ProviderConfig)ProviderConfigManagerImpl.this.configurations.get(configId);
if (existing != null && !config.getPid().equals(existing.getPid())) {
throw new ConfigurationException("oauth.config.id", "Config Id must be unique.");
}
ProviderConfig old = ProviderConfigManagerImpl.this.configurations.put(configId, config);
Provider provider = (Provider)ProviderConfigManagerImpl.this.providerCache.get(config.getProviderId());
if (provider != null) {
OAuthHelper helper = ProviderConfigManagerImpl.this.createHelper(provider.getType(), config);
ProviderConfigManagerImpl.this.helperCache.put(configId, helper);
} else if (old != null) {
ProviderConfigManagerImpl.this.helperCache.remove(configId);
}
}
}
}