EncryptionPostProcessor.java
6.13 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.servlets.post.Modification
* org.apache.sling.servlets.post.ModificationType
* org.apache.sling.servlets.post.SlingPostProcessor
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.crypto.internal;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(specVersion="1.1")
@Service
public class EncryptionPostProcessor
implements SlingPostProcessor {
private static final String SUFFIX_ENCRYPTED = "@Encrypted";
private final Logger log;
@Reference
private CryptoSupport cryptoSupport;
public EncryptionPostProcessor() {
this.log = LoggerFactory.getLogger((String)this.getClass().getName());
}
void bindCryptoSupport(CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
}
void unbindCryptoSupport(CryptoSupport cryptoSupport) {
if (this.cryptoSupport == cryptoSupport) {
this.cryptoSupport = null;
}
}
public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
String basePath = this.getBasePath(request, changes);
Set<String> propsToEncrypt = this.getPropertiesWithSuffix(request, basePath, "@Encrypted");
if (propsToEncrypt.isEmpty()) {
return;
}
Session session = (Session)request.getResourceResolver().adaptTo(Session.class);
ArrayList<Modification> modsToRemove = new ArrayList<Modification>();
block3 : for (Modification mod : changes) {
switch (mod.getType()) {
case ORDER:
case MOVE:
case COPY:
case CREATE:
case MODIFY: {
String source = mod.getSource();
if (source.endsWith("@Encrypted")) {
if (!session.propertyExists(source)) continue block3;
session.getProperty(source).remove();
modsToRemove.add(mod);
continue block3;
}
if (!propsToEncrypt.contains(source) || !session.propertyExists(source)) break;
this.encryptProperty(session.getProperty(source));
}
}
}
changes.removeAll(modsToRemove);
}
private void encryptProperty(Property property) throws RepositoryException {
String propVal = property.getString();
if (!this.cryptoSupport.isProtected(propVal)) {
try {
property.setValue(this.cryptoSupport.protect(propVal));
}
catch (CryptoException e) {
this.log.error("Error on protecting property value", (Throwable)e);
}
}
}
private String getBasePath(SlingHttpServletRequest request, List<Modification> changes) {
Resource resource = request.getResource();
String basePath = resource.getPath();
if (basePath.endsWith("/*")) {
basePath = StringUtils.removeEnd((String)basePath, (String)"/*");
}
if (this.isNewResource(request)) {
for (Modification mod : changes) {
if (mod.getType() != ModificationType.CREATE) continue;
String source = mod.getSource();
if (source == null || !source.startsWith(basePath)) break;
basePath = mod.getSource();
break;
}
}
return basePath;
}
private boolean isNewResource(SlingHttpServletRequest request) {
Resource resource = request.getResource();
String suffix = ResourceUtil.isSyntheticResource((Resource)resource) ? resource.getPath() : request.getRequestPathInfo().getSuffix();
if (suffix == null) {
return false;
}
return suffix.endsWith("/") || suffix.endsWith("/*");
}
private Set<String> getPropertiesWithSuffix(SlingHttpServletRequest request, String basePath, String suffix) {
HashSet<String> props = new HashSet<String>();
Enumeration e = request.getParameterNames();
while (e.hasMoreElements()) {
String property;
String param = (String)e.nextElement();
if (!param.endsWith(suffix) || request.getParameter(property = StringUtils.removeEnd((String)param, (String)suffix)) == null) continue;
props.add(this.toAbsolutePath(basePath, property));
}
return props;
}
private String toAbsolutePath(String basePath, String path) {
if (path.startsWith("/")) {
return ResourceUtil.normalize((String)path);
}
return ResourceUtil.normalize((String)(basePath + '/' + path));
}
}