forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionStore.java
More file actions
372 lines (328 loc) · 11.2 KB
/
SessionStore.java
File metadata and controls
372 lines (328 loc) · 11.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import io.jooby.internal.MemorySessionStore;
import io.jooby.internal.SignedSessionStore;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
/**
* Load and save sessions from store (memory, database, etc.).
*
* @author edgar
* @since 2.0.0
*/
public interface SessionStore {
/** Default session timeout in minutes. */
int DEFAULT_TIMEOUT = 30;
/**
* Base class for in-memory session store.
*
* @author edgar.
* @since 2.0.0
*/
abstract class InMemory implements SessionStore {
protected static class Data {
private Instant lastAccessedTime;
private Instant creationTime;
private Map hash;
public Data(Instant creationTime, Instant lastAccessedTime, Map hash) {
this.creationTime = creationTime;
this.lastAccessedTime = lastAccessedTime;
this.hash = hash;
}
public boolean isExpired(Duration timeout) {
Duration timeElapsed = Duration.between(lastAccessedTime, Instant.now());
return timeElapsed.compareTo(timeout) > 0;
}
}
private SessionToken token;
/**
* Creates a new in-memory session store.
*
* @param token Token.
*/
protected InMemory(@Nonnull SessionToken token) {
this.token = token;
}
@Override public @Nonnull Session newSession(@Nonnull Context ctx) {
String sessionId = token.newToken();
Data data = getOrCreate(sessionId,
sid -> new Data(Instant.now(), Instant.now(), new ConcurrentHashMap()));
Session session = restore(ctx, sessionId, data);
token.saveToken(ctx, sessionId);
return session;
}
/**
* Session token.
*
* @return Session token. Uses a cookie by default: {@link SessionToken#SID}.
*/
public @Nonnull SessionToken getToken() {
return token;
}
/**
* Set custom session token.
*
* @param token Session token.
* @return This store.
*/
public @Nonnull SessionStore setToken(@Nonnull SessionToken token) {
this.token = token;
return this;
}
protected abstract @Nonnull Data getOrCreate(@Nonnull String sessionId,
@Nonnull Function<String, Data> factory);
protected abstract @Nullable Data getOrNull(@Nonnull String sessionId);
protected abstract @Nullable Data remove(@Nonnull String sessionId);
protected abstract void put(@Nonnull String sessionId, @Nonnull Data data);
@Override public Session findSession(Context ctx) {
String sessionId = token.findToken(ctx);
if (sessionId == null) {
return null;
}
Data data = getOrNull(sessionId);
if (data != null) {
Session session = restore(ctx, sessionId, data);
token.saveToken(ctx, sessionId);
return session;
}
return null;
}
@Override public void deleteSession(@Nonnull Context ctx, @Nonnull Session session) {
String sessionId = session.getId();
remove(sessionId);
token.deleteToken(ctx, sessionId);
}
@Override public void touchSession(@Nonnull Context ctx, @Nonnull Session session) {
saveSession(ctx, session);
token.saveToken(ctx, session.getId());
}
@Override public void saveSession(Context ctx, @Nonnull Session session) {
String sessionId = session.getId();
put(sessionId, new Data(session.getCreationTime(), Instant.now(), session.toMap()));
}
@Override public void renewSessionId(@Nonnull Context ctx, @Nonnull Session session) {
String oldId = session.getId();
Data data = remove(oldId);
if (data != null) {
String newId = token.newToken();
session.setId(newId);
put(newId, data);
}
}
private Session restore(Context ctx, String sessionId, Data data) {
return Session.create(ctx, sessionId, data.hash)
.setLastAccessedTime(data.lastAccessedTime)
.setCreationTime(data.creationTime);
}
}
/**
* Creates a new session. This method must:
*
* - Set session as new {@link Session#setNew(boolean)}
* - Optionally, set session creation time {@link Session#setCreationTime(Instant)}
* - Optionally, set session last accessed time {@link Session#setLastAccessedTime(Instant)}
*
* @param ctx Web context.
* @return A new session.
*/
@Nonnull Session newSession(@Nonnull Context ctx);
/**
* Find an existing session by ID. For existing session this method must:
*
* - Optionally, Retrieve/restore session creation time
* - Optionally, Set session last accessed time {@link Session#setLastAccessedTime(Instant)}
*
* @param ctx Web context.
* @return An existing session or <code>null</code>.
*/
@Nullable Session findSession(@Nonnull Context ctx);
/**
* Delete a session from store. This method must NOT call {@link Session#destroy()}.
*
* @param ctx Web context.
* @param session Current session.
*/
void deleteSession(@Nonnull Context ctx, @Nonnull Session session);
/**
* Session attributes/state has changed. Every time a session attribute is put or removed it,
* this method is executed as notification callback.
*
* @param ctx Web context.
* @param session Current session.
*/
void touchSession(@Nonnull Context ctx, @Nonnull Session session);
/**
* Save a session. This method must save:
*
* - Session attributes/data
* - Optionally set Session metadata like: creationTime, lastAccessed time, etc.
*
* This method is call after response is send to client, so context and response shouldn't be
* modified.
*
* @param ctx Web context.
* @param session Current session.
*/
void saveSession(@Nonnull Context ctx, @Nonnull Session session);
/**
* Renew Session ID. This operation might or might not be implemented by a Session Store.
*
* @param ctx Web Context.
* @param session Session.
*/
void renewSessionId(@Nonnull Context ctx, @Nonnull Session session);
/**
* Creates a cookie based session and store data in memory. Session data is not keep after
* restart.
*
* It uses the default session cookie: {@link SessionToken#SID}.
*
* - Session data is not keep after restart.
*
* @param timeout Timeout in seconds. Use <code>-1</code> for no timeout.
* @return Session store.
*/
static @Nonnull SessionStore memory(int timeout) {
return memory(SessionToken.SID, Duration.ofSeconds(timeout));
}
/**
* Creates a cookie based session and store data in memory. Session data is not keep after
* restart.
*
* It uses the default session cookie: {@link SessionToken#SID}.
*
* - Session expires after 30 minutes of inactivity.
* - Session data is not keep after restart.
*
* @return Session store.
*/
static @Nonnull SessionStore memory() {
return memory(SessionToken.SID);
}
/**
* Creates a cookie based session and store data in memory. Session data is not keep after
* restart.
*
* It uses the default session cookie: {@link SessionToken#SID}.
*
* @param timeout Expires session after amount of inactivity time.
* @return Session store.
*/
static @Nonnull SessionStore memory(@Nonnull Duration timeout) {
return memory(SessionToken.SID, timeout);
}
/**
* Creates a cookie based session and store data in memory.
*
* - Session expires after 30 minutes of inactivity.
* - Session data is not keep after restart.
*
* @param cookie Cookie to use.
* @return Session store.
*/
static @Nonnull SessionStore memory(@Nonnull Cookie cookie) {
return memory(SessionToken.cookieId(cookie));
}
/**
* Creates a cookie based session and store data in memory. Session data is not keep after
* restart.
*
* @param cookie Cookie to use.
* @param timeout Expires session after amount of inactivity time.
* @return Session store.
*/
static @Nonnull SessionStore memory(@Nonnull Cookie cookie, @Nonnull Duration timeout) {
return memory(SessionToken.cookieId(cookie), timeout);
}
/**
* Creates a session store that save data in memory.
* - Session expires after 30 minutes of inactivity.
* - Session data is not keep after restart.
*
* @param token Session token.
* @return Session store.
*/
static @Nonnull SessionStore memory(@Nonnull SessionToken token) {
return new MemorySessionStore(token, Duration.ofMinutes(DEFAULT_TIMEOUT));
}
/**
* Creates a session store that save data in memory. Session data is not keep after restart.
*
* @param token Session token.
* @param timeout Expires session after amount of inactivity time.
* @return Session store.
*/
static @Nonnull SessionStore memory(@Nonnull SessionToken token, @Nonnull Duration timeout) {
return new MemorySessionStore(token, timeout);
}
/**
* Creates a session store that uses (un)signed data. Session data is signed it using
* <code>HMAC_SHA256</code>.
*
* See {@link Cookie#sign(String, String)} and {@link Cookie#unsign(String, String)}.
*
* @param secret Secret token to signed data.
* @return A browser session store.
*/
static @Nonnull SessionStore signed(@Nonnull String secret) {
return signed(secret, SessionToken.SID);
}
/**
* Creates a session store that uses (un)signed data. Session data is signed it using
* <code>HMAC_SHA256</code>.
*
* See {@link Cookie#sign(String, String)} and {@link Cookie#unsign(String, String)}.
*
* @param secret Secret token to signed data.
* @param cookie Cookie to use.
* @return A browser session store.
*/
static @Nonnull SessionStore signed(@Nonnull String secret, @Nonnull Cookie cookie) {
return signed(secret, SessionToken.signedCookie(cookie));
}
/**
* Creates a session store that uses (un)signed data. Session data is signed it using
* <code>HMAC_SHA256</code>.
*
* See {@link Cookie#sign(String, String)} and {@link Cookie#unsign(String, String)}.
*
* @param secret Secret token to signed data.
* @param token Session token to use.
* @return A browser session store.
*/
static @Nonnull SessionStore signed(@Nonnull String secret, @Nonnull SessionToken token) {
SneakyThrows.Function<String, Map<String, String>> decoder = value -> {
String unsign = Cookie.unsign(value, secret);
if (unsign == null) {
return null;
}
return Cookie.decode(unsign);
};
SneakyThrows.Function<Map<String, String>, String> encoder = attributes ->
Cookie.sign(Cookie.encode(attributes), secret);
return signed(token, decoder, encoder);
}
/**
* Creates a session store that save data into Cookie. Cookie data is (un)signed it using the given
* decoder and encoder.
*
* @param token Token to use.
* @param decoder Decoder to use.
* @param encoder Encoder to use.
* @return Cookie session store.
*/
static @Nonnull SessionStore signed(@Nonnull SessionToken token,
@Nonnull Function<String, Map<String, String>> decoder,
@Nonnull Function<Map<String, String>, String> encoder) {
return new SignedSessionStore(token, decoder, encoder);
}
}