EIOServiceImpl.java
7.52 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.oauth.jwt.JwsBuilder
* com.adobe.granite.oauth.jwt.JwsBuilderFactory
* com.adobe.granite.oauth.jwt.JwsValidator
* javax.annotation.Nonnull
* javax.servlet.http.HttpServletRequest
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.oltu.oauth2.jwt.ClaimsSet
* org.apache.oltu.oauth2.jwt.JWT
* org.apache.oltu.oauth2.jwt.io.JWTReader
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.socketio.impl.engine;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.oauth.jwt.JwsBuilder;
import com.adobe.granite.oauth.jwt.JwsBuilderFactory;
import com.adobe.granite.oauth.jwt.JwsValidator;
import com.adobe.granite.socketio.impl.engine.EIOListener;
import com.adobe.granite.socketio.impl.engine.EIOService;
import com.adobe.granite.socketio.impl.engine.EIOSocket;
import com.adobe.granite.socketio.impl.engine.EIOTransport;
import com.adobe.granite.socketio.impl.engine.EIOWebSocketTransport;
import java.io.IOException;
import java.security.Principal;
import java.util.Collection;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletRequest;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.oltu.oauth2.jwt.ClaimsSet;
import org.apache.oltu.oauth2.jwt.JWT;
import org.apache.oltu.oauth2.jwt.io.JWTReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
public class EIOServiceImpl
implements EIOService {
private static final Logger log = LoggerFactory.getLogger(EIOServiceImpl.class);
private final ConcurrentMap<EIOListener, Object> listeners = new ConcurrentHashMap<EIOListener, Object>();
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final ConcurrentHashMap<String, EIOSocket> sockets = new ConcurrentHashMap();
private long pingTimeout = 60000;
private long pingInterval = 25000;
private static long DEFAULT_TOKEN_EXPIRATION_TIME = 5000;
@Reference
private JwsBuilderFactory jwsBuilderFactory;
@Reference
private JwsValidator jwsValidator;
@Deactivate
private void deactivate() {
for (EIOSocket socket : this.sockets.values()) {
socket.close();
}
}
protected ScheduledFuture<?> schedulePingTimeout(Runnable timeoutHandler) {
return this.scheduler.schedule(timeoutHandler, this.pingTimeout + this.pingInterval, TimeUnit.MILLISECONDS);
}
protected void onConnect(EIOSocket socket) {
for (EIOListener l : this.listeners.keySet()) {
l.onConnect(socket);
}
}
protected void onDisconnect(EIOSocket socket) {
for (EIOListener l : this.listeners.keySet()) {
l.onDisconnect(socket);
}
this.sockets.remove(socket.getId());
log.debug("socket {} removed. size = {}", (Object)socket.getId(), (Object)this.sockets.size());
}
@Override
public EIOSocket getSocket(String id) {
return this.sockets.get(id);
}
@Override
public EIOSocket createSocket(String id, String transport, Principal userPrincipal) {
if (!this.isTransportSupported(transport)) {
throw new IllegalArgumentException("Unsupported transport: " + transport);
}
if (id == null) {
id = UUID.randomUUID().toString();
}
if (this.sockets.containsKey(id)) {
throw new IllegalArgumentException("Socket already exists: " + id);
}
EIOWebSocketTransport tsp = new EIOWebSocketTransport(id);
EIOSocket socket = new EIOSocket(id, this, tsp, userPrincipal);
this.sockets.put(id, socket);
log.debug("socket {} created. size = {}", (Object)id, (Object)this.sockets.size());
return socket;
}
@Override
public boolean isTransportSupported(String transport) {
return "websocket".equals(transport);
}
@Override
public long getPingTimeout() {
return this.pingTimeout;
}
@Override
public long getPingInterval() {
return this.pingInterval;
}
@Override
public void register(EIOListener listener) {
this.listeners.put(listener, listener);
}
@Override
public void unregister(EIOListener listener) {
this.listeners.remove(listener);
}
@Override
public String createSessionToken(@Nonnull HttpServletRequest req) throws IOException {
try {
String user = req.getUserPrincipal() == null ? null : req.getUserPrincipal().getName();
String token = this.jwsBuilderFactory.getInstance("HS256").setIssuer(user).setExpiresIn(DEFAULT_TOKEN_EXPIRATION_TIME).setCustomClaimsSetField("jti", (Object)UUID.randomUUID().toString()).build();
log.debug("Created new JWT token: {}", (Object)token);
return token;
}
catch (CryptoException e) {
log.error("Error while creating JWT token", (Throwable)e);
throw new IOException((Throwable)e);
}
}
@Override
public String validateToken(String token, HttpServletRequest req) {
if (!this.jwsValidator.validate(token)) {
log.info("provided token not valid: {}", (Object)token);
return null;
}
JWT t = (JWT)new JWTReader().read(token);
log.debug("received valid token: {}", (Object)t);
if (t.getClaimsSet().getExpirationTime() > System.currentTimeMillis()) {
log.info("provided token expired: {}", (Object)token);
return null;
}
String user = req.getUserPrincipal() == null ? "" : req.getUserPrincipal().getName();
String issuer = t.getClaimsSet().getIssuer();
if (issuer == null) {
issuer = "";
}
if (!user.equals(issuer)) {
log.info("provided token user mismatch: {} != {}", (Object)user, (Object)issuer);
return null;
}
String sid = t.getClaimsSet().getJwdId();
if (sid == null) {
log.info("provided token has no session id: {}", (Object)token);
return null;
}
if (this.sockets.containsKey(sid)) {
log.warn("provided session id ({}) in jwt token already in use: {}", (Object)sid, (Object)token);
return null;
}
return sid;
}
protected void bindJwsBuilderFactory(JwsBuilderFactory jwsBuilderFactory) {
this.jwsBuilderFactory = jwsBuilderFactory;
}
protected void unbindJwsBuilderFactory(JwsBuilderFactory jwsBuilderFactory) {
if (this.jwsBuilderFactory == jwsBuilderFactory) {
this.jwsBuilderFactory = null;
}
}
protected void bindJwsValidator(JwsValidator jwsValidator) {
this.jwsValidator = jwsValidator;
}
protected void unbindJwsValidator(JwsValidator jwsValidator) {
if (this.jwsValidator == jwsValidator) {
this.jwsValidator = null;
}
}
}