forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultSessionCodec.java
More file actions
252 lines (234 loc) · 8.45 KB
/
DefaultSessionCodec.java
File metadata and controls
252 lines (234 loc) · 8.45 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
251
252
package act.session;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import static org.osgl.http.H.Session.KEY_EXPIRATION;
import static org.osgl.http.H.Session.KEY_EXPIRE_INDICATOR;
import act.conf.AppConfig;
import act.crypto.AppCrypto;
import act.crypto.RotateSecretCrypto;
import act.util.DestroyableBase;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.util.C;
import org.osgl.util.Charsets;
import org.osgl.util.Codec;
import org.osgl.util.S;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class DefaultSessionCodec extends DestroyableBase implements SessionCodec {
private final boolean sessionWillExpire;
private final boolean encryptSession;
private final int ttlInMillis;
private final String pingPath;
private RotateSecretCrypto crypto;
@Inject
public DefaultSessionCodec(AppConfig conf, RotateSecretCrypto crypto) {
ttlInMillis = conf.sessionTtl() * 1000;
sessionWillExpire = ttlInMillis > 0;
pingPath = conf .pingPath();
encryptSession = conf.encryptSession();
this.crypto = $.requireNotNull(crypto);
}
@Override
protected void releaseResources() {
crypto = null;
}
@Override
public String encodeSession(H.Session session) {
if (null == session) {
return null;
}
boolean sessionChanged = session.changed();
if (!sessionChanged && (session.empty() || !sessionWillExpire)) {
// Nothing changed and no cookie-expire or empty, consequently send nothing back.
return null;
}
session.id(); // ensure session ID is generated
if (sessionWillExpire && !session.contains(KEY_EXPIRATION)) {
// session get cleared before
session.put(KEY_EXPIRATION, $.ms() + ttlInMillis);
}
return dissolveIntoCookieContent(session, true);
}
@Override
public String encodeFlash(H.Flash flash) {
if (null == flash || flash.isEmpty()) {
return null;
}
return dissolveIntoCookieContent(flash.out(), false);
}
@Override
public H.Session decodeSession(String encodedSession, H.Request request) {
H.Session session = new H.Session();
boolean newSession = true;
if (S.notBlank(encodedSession)) {
resolveFromCookieContent(session, encodedSession, true);
newSession = false;
}
session = processExpiration(session, $.ms(), newSession, sessionWillExpire, ttlInMillis, pingPath, request);
return session;
}
@Override
public H.Flash decodeFlash(String encodedFlash) {
H.Flash flash = new H.Flash();
if (S.notBlank(encodedFlash)) {
resolveFromCookieContent(flash, encodedFlash, false);
flash.discard(); // prevent cookie content from been output to response again
}
return flash;
}
private void resolveFromCookieContent(H.KV<?> kv, String content, boolean isSession) {
String data = Codec.decodeUrl(content, Charsets.UTF_8);
if (isSession) {
if (encryptSession) {
try {
data = crypto.decrypt(data);
} catch (Exception e) {
return;
}
}
int firstDashIndex = data.indexOf("-");
if (firstDashIndex < 0) {
return;
}
String sign = data.substring(0, firstDashIndex);
data = data.substring(firstDashIndex + 1);
if (!crypto.verifySignature(data, sign)) {
return;
}
}
List<char[]> pairs = split(data.toCharArray(), '\u0000');
if (pairs.isEmpty()) return;
for (char[] pair: pairs) {
List<char[]> kAndV = split(pair, '\u0001');
int sz = kAndV.size();
if (sz != 2) {
S.Buffer sb = S.newBuffer();
for (int i = 0; i < sz; ++i) {
if (i > 0) sb.append(":");
sb.append(Arrays.toString(kAndV.get(i)));
}
warn("unexpected KV string: %S", sb.toString());
} else {
kv.put(new String(kAndV.get(0)), new String(kAndV.get(1)));
}
}
}
private List<char[]> split(char[] content, char separator) {
int len = content.length;
if (0 == len) {
return C.list();
}
List<char[]> l = new ArrayList<>();
int start = 0;
for (int i = 0; i < len; ++i) {
char c = content[i];
if (c == separator) {
if (i == start) {
start++;
continue;
}
char[] ca = new char[i - start];
System.arraycopy(content, start, ca, 0, i - start);
l.add(ca);
start = i + 1;
}
}
if (start == 0) {
l.add(content);
} else {
char[] ca = new char[len - start];
System.arraycopy(content, start, ca, 0, len - start);
l.add(ca);
}
return l;
}
private String dissolveIntoCookieContent(H.KV<?> kv, boolean isSession) {
S.Buffer sb = S.buffer();
int i = 0;
for (Map.Entry<String, String> entry : kv.entrySet()) {
if (i > 0) {
sb.append("\u0000");
}
String k = entry.getKey();
String v = entry.getValue();
sb.append(k);
sb.append("\u0001");
sb.append(v);
i++;
}
String data = sb.toString();
if (isSession) {
String sign = crypto.sign(data);
data = S.concat(sign, "-", data);
if (encryptSession) {
data = crypto.encrypt(data);
}
}
data = Codec.encodeUrl(data, Charsets.UTF_8);
return data;
}
static H.Session processExpiration(H.Session session, long now, boolean newSession, boolean sessionWillExpire, int ttlInMillis, String pingPath, H.Request request) {
if (!sessionWillExpire) return session;
long expiration = now + ttlInMillis;
if (newSession) {
// no previous cookie to restore; but we need to set the timestamp in the new cookie
// note we use `load` API instead of `put` because we don't want to set the dirty flag
// in this case
session.load(KEY_EXPIRATION, String.valueOf(expiration));
} else {
String s = session.get(KEY_EXPIRATION);
long oldTimestamp = null == s ? -1 : Long.parseLong(s);
long newTimestamp = expiration;
// Verify that the session contains a timestamp, and that it's not expired
if (oldTimestamp < 0) {
// invalid session, reset it
session = new H.Session();
} else {
if (oldTimestamp < now) {
// Session expired
session = new H.Session();
session.put(KEY_EXPIRE_INDICATOR, true);
} else {
session.remove(KEY_EXPIRE_INDICATOR);
boolean skipUpdateExpiration = S.eq(pingPath, request.url());
if (skipUpdateExpiration) {
newTimestamp = oldTimestamp;
}
}
}
session.put(KEY_EXPIRATION, newTimestamp);
}
return session;
}
public static void main(String[] args) {
AppCrypto crypto = new AppCrypto("abc");
String s = "hello world";
String se = crypto.encrypt(s);
System.out.println(crypto.decrypt(se));
crypto = new AppCrypto("abc-213411253");
System.out.println(crypto.decrypt(se));
}
}