ClientCertAuthHandler.java
10.4 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.keystore.KeyStoreService
* com.day.crx.security.token.TokenUtil
* javax.jcr.RepositoryException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* 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.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.auth.core.spi.AuthenticationHandler
* org.apache.sling.auth.core.spi.AuthenticationInfo
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.auth.cert.impl;
import com.adobe.granite.auth.cert.UserCertificateMapping;
import com.adobe.granite.auth.cert.UserCertificateMappingException;
import com.adobe.granite.keystore.KeyStoreService;
import com.day.crx.security.token.TokenUtil;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.auth.core.spi.AuthenticationHandler;
import org.apache.sling.auth.core.spi.AuthenticationInfo;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(specVersion="1.1", label="%auth.clientcerthandler.name", description="%auth.clientcerthandler.description", metatype=1)
@Service
@Properties(value={@Property(name="path", value={"/"}), @Property(name="service.ranking", intValue={0}, propertyPrivate=0)})
public class ClientCertAuthHandler
implements AuthenticationHandler,
UserCertificateMapping {
private final Logger log;
@Property(name="service.description")
private static final String DESCRIPTION = "Granite Client Certificate Authentication Handler";
@Property(name="authtype", propertyPrivate=1)
public static final String CERT_AUTHENTICATED = "certAuthenticated";
public static final String CERT = "cert";
public static final String SEPARATOR = "#";
@Reference
private SlingRepository repository;
@Reference
private ResourceResolverFactory rrf;
@Reference
private KeyStoreService keyStoreService;
public ClientCertAuthHandler() {
this.log = LoggerFactory.getLogger((String)this.getClass().getName());
}
public AuthenticationInfo extractCredentials(HttpServletRequest request, HttpServletResponse response) {
X509Certificate[] certs = (X509Certificate[])request.getAttribute("javax.servlet.request.X509Certificate");
AuthenticationInfo authInfo = null;
if (certs != null && certs.length > 0) {
String mappedUser = null;
X509Certificate cert = certs[0];
try {
mappedUser = this.findMappedUsers(cert);
if (mappedUser != null) {
authInfo = TokenUtil.createCredentials((HttpServletRequest)request, (HttpServletResponse)response, (SlingRepository)this.repository, (String)mappedUser, (boolean)true);
authInfo.put("cert", (Object)cert);
}
}
catch (Exception e) {
this.log.warn("Unable to create token credentials, setting cert for uid " + mappedUser, (Throwable)e);
authInfo = new AuthenticationInfo("certAuthenticated", mappedUser);
authInfo.put("cert", (Object)cert);
}
}
return authInfo;
}
private String findMappedUsers(X509Certificate cert) throws UserCertificateMappingException {
String user;
user = null;
ResourceResolver servicesResolver = null;
try {
int index;
servicesResolver = this.rrf.getServiceResourceResolver(null);
KeyStore trustStore = this.keyStoreService.getTrustStore(servicesResolver);
String alias = trustStore.getCertificateAlias(cert);
if (alias != null && (index = alias.lastIndexOf("#")) > -1) {
user = alias.substring(0, index);
}
}
catch (LoginException e) {
throw new UserCertificateMappingException((Throwable)e);
}
catch (KeyStoreException e) {
throw new UserCertificateMappingException(e);
}
finally {
if (servicesResolver != null) {
servicesResolver.close();
}
}
return user;
}
public boolean requestCredentials(HttpServletRequest request, HttpServletResponse response) throws IOException {
return false;
}
public void dropCredentials(HttpServletRequest request, HttpServletResponse response) throws IOException {
}
@Override
public void mapCertificate(ResourceResolver resolver, String userId, X509Certificate certificate) throws UserCertificateMappingException {
block6 : {
String alias = userId + "#" + System.currentTimeMillis();
try {
UserManager userManager = (UserManager)resolver.adaptTo(UserManager.class);
if (userManager != null) {
Authorizable user = userManager.getAuthorizable(userId);
if (user != null && !user.isGroup()) {
KeyStore trustStore = this.keyStoreService.getTrustStore(resolver);
String oldAlias = trustStore.getCertificateAlias(certificate);
if (oldAlias != null) {
trustStore.deleteEntry(oldAlias);
}
trustStore.setCertificateEntry(alias, certificate);
break block6;
}
throw new UserCertificateMappingException("Failed to obtain a user using given userId " + userId);
}
throw new UserCertificateMappingException("Failed to adapt given resolver to a user manager.");
}
catch (RepositoryException e) {
throw new UserCertificateMappingException((Throwable)e);
}
catch (KeyStoreException e) {
throw new UserCertificateMappingException(e);
}
}
}
@Override
public void unmapCertificate(ResourceResolver resolver, String alias) throws UserCertificateMappingException {
block3 : {
try {
KeyStore trustStore = this.keyStoreService.getTrustStore(resolver);
if (trustStore.containsAlias(alias)) {
trustStore.deleteEntry(alias);
break block3;
}
throw new UserCertificateMappingException("Alias " + alias + " doesn't exist.");
}
catch (KeyStoreException e) {
throw new UserCertificateMappingException(e);
}
}
}
@Override
public Map<String, X509Certificate> listCertificates(ResourceResolver resolver, String userId) throws UserCertificateMappingException {
HashMap<String, X509Certificate> certificatesMap;
block6 : {
certificatesMap = null;
try {
UserManager userManager = (UserManager)resolver.adaptTo(UserManager.class);
if (userManager != null) {
Authorizable user = userManager.getAuthorizable(userId);
if (user != null && !user.isGroup()) {
KeyStore trustStore = this.keyStoreService.getTrustStore(resolver);
certificatesMap = new HashMap<String, X509Certificate>();
Enumeration<String> aliases = trustStore.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (!alias.startsWith(userId + "#")) continue;
certificatesMap.put(alias, (X509Certificate)trustStore.getCertificate(alias));
}
break block6;
}
throw new UserCertificateMappingException("Failed to obtain a user using given userId " + userId);
}
throw new UserCertificateMappingException("Failed to adapt given resolver to a user manager.");
}
catch (RepositoryException e) {
throw new UserCertificateMappingException((Throwable)e);
}
catch (KeyStoreException e) {
throw new UserCertificateMappingException(e);
}
}
return certificatesMap;
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindRrf(ResourceResolverFactory resourceResolverFactory) {
this.rrf = resourceResolverFactory;
}
protected void unbindRrf(ResourceResolverFactory resourceResolverFactory) {
if (this.rrf == resourceResolverFactory) {
this.rrf = null;
}
}
protected void bindKeyStoreService(KeyStoreService keyStoreService) {
this.keyStoreService = keyStoreService;
}
protected void unbindKeyStoreService(KeyStoreService keyStoreService) {
if (this.keyStoreService == keyStoreService) {
this.keyStoreService = null;
}
}
}