MemoryTokenServiceImpl.java
4.65 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.google.common.cache.Cache
* com.google.common.cache.CacheBuilder
* javax.jcr.Session
* org.apache.felix.jaas.LoginModuleFactory
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
*/
package com.adobe.cq.dam.s7imaging.impl.auth;
import com.adobe.cq.dam.s7imaging.impl.auth.MemoryTokenLoginModule;
import com.adobe.cq.dam.s7imaging.impl.auth.MemoryTokenService;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.util.Dictionary;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.jcr.Session;
import javax.security.auth.spi.LoginModule;
import org.apache.felix.jaas.LoginModuleFactory;
import org.apache.felix.scr.annotations.Activate;
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.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
@Component(metatype=1, label="Adobe CQ ImageServer Memory Token Service", description="Adobe CQ ImageServer Memory Token Service")
@Service(value={MemoryTokenService.class, LoginModuleFactory.class})
@Properties(value={@Property(name="service.description", value={"Adobe CQ ImageServer Memory Token Service"}), @Property(name="jaas.controlFlag", value={"sufficient"}, propertyPrivate=1), @Property(name="jaas.realmName", value={"jackrabbit.oak"}, propertyPrivate=1), @Property(name="jaas.ranking", intValue={1000}, propertyPrivate=1)})
public class MemoryTokenServiceImpl
implements MemoryTokenService,
LoginModuleFactory {
private static final String DEFAULT_HTTP_HEADER = "X-CQ-ImageServer-Token";
private static final long DEFAULT_EXPIRY_TIME = 3600;
private static final long DEFAULT_MAX_SIZE = 100000;
@Property(value={"X-CQ-ImageServer-Token"}, label="HTTP Header", description="Name of the HTTP header holding the token")
public static final String HTTP_HEADER = "http.header";
@Property(longValue={3600}, label="Expiration", description="Token expiration time in seconds")
public static final String EXPIRY_TIME = "tokens.expirytime";
@Property(longValue={100000}, label="Max Tokens", description="Maximum number of tokens hold in memory (LRU tokens will be evicted first)")
public static final String MAX_SIZE = "tokens.maxsize";
private Cache<String, Token> tokens;
private String httpHeader;
public MemoryTokenServiceImpl() {
this.tokens = this.createCache(3600, 100000);
this.httpHeader = "X-CQ-ImageServer-Token";
}
@Activate
protected void activate(ComponentContext ctx) {
long expiryTime = PropertiesUtil.toLong(ctx.getProperties().get("tokens.expirytime"), (long)3600);
long maxSize = PropertiesUtil.toLong(ctx.getProperties().get("tokens.maxsize"), (long)100000);
this.tokens = this.createCache(expiryTime, maxSize);
this.httpHeader = PropertiesUtil.toString(ctx.getProperties().get("http.header"), (String)"X-CQ-ImageServer-Token");
}
private Cache<String, Token> createCache(long expiryTime, long maxSize) {
return CacheBuilder.newBuilder().maximumSize(maxSize).expireAfterWrite(expiryTime, TimeUnit.SECONDS).build();
}
@Override
public String createToken(Session session) {
if (session == null) {
return null;
}
Token token = new Token(UUID.randomUUID().toString(), session.getUserID());
this.tokens.put((Object)token.key, (Object)token);
return token.key;
}
@Override
public String getUser(String token) {
if (token == null) {
return null;
}
Token t = (Token)this.tokens.getIfPresent((Object)token);
if (t == null) {
return null;
}
return t.userId;
}
@Override
public void removeToken(String token) {
this.tokens.invalidate((Object)token);
}
@Override
public String getHttpHeaderName() {
return this.httpHeader;
}
public LoginModule createLoginModule() {
return new MemoryTokenLoginModule(this);
}
protected class Token {
final String key;
final String userId;
private Token(String key, String userId) {
this.key = key;
this.userId = userId;
}
}
}