AccessTokenCleanupTask.java
10.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.oauth.jwt.JwsValidator
* javax.jcr.InvalidItemStateException
* javax.jcr.LoginException
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.security.AccessControlEntry
* javax.jcr.security.AccessControlList
* javax.jcr.security.AccessControlManager
* javax.jcr.security.AccessControlPolicy
* 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.User
* org.apache.jackrabbit.oak.spi.security.ConfigurationParameters
* org.apache.jackrabbit.oak.spi.security.user.UserConfiguration
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.oauth.server.impl;
import com.adobe.granite.oauth.jwt.JwsValidator;
import com.adobe.granite.oauth.server.OAuth2ResourceServer;
import com.adobe.granite.oauth.server.impl.helper.OAuth2Helper;
import java.security.Principal;
import javax.jcr.InvalidItemStateException;
import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.security.AccessControlEntry;
import javax.jcr.security.AccessControlList;
import javax.jcr.security.AccessControlManager;
import javax.jcr.security.AccessControlPolicy;
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.User;
import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Adobe OAuth2 Access Token Authentication Handler: Cleanup Task", description="Task to regularly purge expired access tokens from the repository")
@Service(value={Runnable.class})
@Properties(value={@Property(name="scheduler.expression", value={"23 07 * * * ?"}, label="Schedule", description="Cron expression scheudling this job. Default is hourly 07m23s after the hour. See http://www.docjar.com/docs/api/org/quartz/CronTrigger.html for a description of the format for this value."), @Property(name="scheduler.runOn", value={"LEADER"}, propertyPrivate=1), @Property(name="service.description", value={"Periodic Cleanup Job"}, propertyPrivate=1)})
public class AccessTokenCleanupTask
implements Runnable {
private final Logger log;
private static final String JWT_HEADER = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.";
@Reference
private SlingRepository repository;
@Reference
private JwsValidator jwsValidator;
@Reference
private OAuth2ResourceServer oAuth2ResourceServer;
@Reference
private UserConfiguration userConfiguration;
public AccessTokenCleanupTask() {
this.log = LoggerFactory.getLogger(this.getClass());
}
public void run() {
this.log.debug("AccessTokenCleanupTask: Starting cleanup");
this.cleanup();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void cleanup() {
long start;
int numLive;
int numRemoved;
block12 : {
start = System.currentTimeMillis();
numRemoved = 0;
numLive = 0;
Session oauthServiceSession = null;
try {
oauthServiceSession = this.repository.loginService(null, null);
String userRootPath = (String)this.userConfiguration.getParameters().getConfigValue("usersPath", (Object)"/rep:security/rep:authorizables/rep:users");
if (!oauthServiceSession.nodeExists(userRootPath + "/oauth")) break block12;
NodeIterator intermediateNodes = oauthServiceSession.getNode(userRootPath + "/oauth/").getNodes();
while (intermediateNodes.hasNext()) {
Node intermidiateNode = intermediateNodes.nextNode();
NodeIterator accessTokens = intermidiateNode.getNodes();
while (accessTokens.hasNext()) {
String principalName;
String jws;
Node accessTokenNode = accessTokens.nextNode();
String name = accessTokenNode.getProperty("rep:authorizableId").getString();
User user = OAuth2Helper.getUser(oauthServiceSession, name);
if (user == null || !this.isJwtToken(jws = OAuth2Helper.getJwtFromUserId(principalName = user.getPrincipal().getName()))) continue;
if (!this.jwsValidator.validate(jws)) {
this.removeAces(oauthServiceSession, jws, name);
this.log.debug("removing user node {} ", (Object)accessTokenNode.getPath());
accessTokenNode.remove();
++numRemoved;
continue;
}
++numLive;
}
if (intermidiateNode.hasNodes()) continue;
intermidiateNode.remove();
}
if (!oauthServiceSession.hasPendingChanges()) break block12;
try {
oauthServiceSession.save();
}
catch (InvalidItemStateException iise) {
this.log.info("AccessTokenCleanupTask: Concurrent modification to one or more of the tokens to be removed. Retrying later");
}
catch (RepositoryException re) {
this.log.info("AccessTokenCleanupTask: Failed persisting token removal. Retrying later");
}
}
catch (Throwable t) {
this.log.error("AccessTokenCleanupTask: General failure while trying to cleanup tokens", t);
}
finally {
if (oauthServiceSession != null) {
oauthServiceSession.logout();
}
}
}
long end = System.currentTimeMillis();
this.log.info("AccessTokenCleanupTask: Removed {} token(s) in {}ms ({} token(s) still active)", new Object[]{numRemoved, end - start, numLive});
}
private void removeAces(Session session, String jwt, String userId) throws LoginException, RepositoryException {
String scopes = OAuth2Helper.getScopes(jwt);
String subject = OAuth2Helper.getSubject(jwt);
User resourceOwner = OAuth2Helper.getUser(session, subject);
for (String scope : OAuth2Helper.getDefaultScopesResourcePathSet(this.oAuth2ResourceServer, scopes, resourceOwner)) {
this.removeAce(session, userId, scope);
}
}
private void removeAce(Session session, String userId, String absPath) {
try {
AccessControlManager accessControlManager = session.getAccessControlManager();
Principal principal = AccessTokenCleanupTask.getPrincipal(session, userId);
if (principal != null) {
AccessControlPolicy[] policies;
for (AccessControlPolicy plc : policies = accessControlManager.getPolicies(absPath)) {
if (!(plc instanceof AccessControlList)) continue;
boolean modified = false;
AccessControlList acl = (AccessControlList)plc;
for (AccessControlEntry ace : acl.getAccessControlEntries()) {
if (!principal.equals(ace.getPrincipal())) continue;
acl.removeAccessControlEntry(ace);
this.log.debug("removed {} ace for principal {} ", (Object)ace, (Object)userId);
modified = true;
}
if (!modified) continue;
accessControlManager.setPolicy(absPath, (AccessControlPolicy)acl);
}
if (session.hasPendingChanges()) {
session.save();
}
}
}
catch (Exception e) {
this.log.error("exception while removing ace", (Throwable)e);
}
}
private static Principal getPrincipal(Session session, String name) throws RepositoryException {
User user = OAuth2Helper.getUser(session, name);
if (user != null) {
return user.getPrincipal();
}
return null;
}
private boolean isJwtToken(String accessToken) {
if (accessToken.startsWith("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.")) {
return true;
}
return false;
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindJwsValidator(JwsValidator jwsValidator) {
this.jwsValidator = jwsValidator;
}
protected void unbindJwsValidator(JwsValidator jwsValidator) {
if (this.jwsValidator == jwsValidator) {
this.jwsValidator = null;
}
}
protected void bindOAuth2ResourceServer(OAuth2ResourceServer oAuth2ResourceServer) {
this.oAuth2ResourceServer = oAuth2ResourceServer;
}
protected void unbindOAuth2ResourceServer(OAuth2ResourceServer oAuth2ResourceServer) {
if (this.oAuth2ResourceServer == oAuth2ResourceServer) {
this.oAuth2ResourceServer = null;
}
}
protected void bindUserConfiguration(UserConfiguration userConfiguration) {
this.userConfiguration = userConfiguration;
}
protected void unbindUserConfiguration(UserConfiguration userConfiguration) {
if (this.userConfiguration == userConfiguration) {
this.userConfiguration = null;
}
}
}