|
5 | 5 | */ |
6 | 6 | package io.jooby; |
7 | 7 |
|
| 8 | +import io.jooby.internal.CookieSessionStore; |
8 | 9 | import io.jooby.internal.MemorySessionStore; |
9 | 10 |
|
10 | 11 | import javax.annotation.Nonnull; |
11 | 12 | import javax.annotation.Nullable; |
12 | | -import java.time.Duration; |
13 | 13 | import java.time.Instant; |
14 | 14 |
|
15 | 15 | /** |
|
20 | 20 | */ |
21 | 21 | public interface SessionStore { |
22 | 22 |
|
23 | | - Cookie SID = new Cookie("jooby.sid") |
24 | | - .setMaxAge(Duration.ofSeconds(-1)) |
25 | | - .setHttpOnly(true) |
26 | | - .setPath("/"); |
27 | | - |
28 | | - @Nonnull SessionToken getSessionToken(); |
29 | | - |
30 | 23 | /** |
31 | 24 | * Creates a new session. This method must: |
32 | 25 | * |
33 | 26 | * - Set session as new {@link Session#setNew(boolean)} |
34 | | - * - Set session creation time {@link Session#setCreationTime(Instant)} |
35 | | - * - Set session last accessed time {@link Session#setLastAccessedTime(Instant)} |
| 27 | + * - Optionally, set session creation time {@link Session#setCreationTime(Instant)} |
| 28 | + * - Optionally, set session last accessed time {@link Session#setLastAccessedTime(Instant)} |
36 | 29 | * |
37 | | - * @param id Session ID. |
| 30 | + * @param ctx Web context. |
38 | 31 | * @return A new session. |
39 | 32 | */ |
40 | 33 | @Nonnull Session newSession(@Nonnull Context ctx); |
41 | 34 |
|
42 | 35 | /** |
43 | 36 | * Find an existing session by ID. For existing session this method must: |
44 | 37 | * |
45 | | - * - Retrieve/restore session creation time |
46 | | - * - Set session last accessed time {@link Session#setLastAccessedTime(Instant)} |
| 38 | + * - Optionally, Retrieve/restore session creation time |
| 39 | + * - Optionally, Set session last accessed time {@link Session#setLastAccessedTime(Instant)} |
47 | 40 | * |
48 | | - * @param id Session ID. |
| 41 | + * @param ctx Web context. |
49 | 42 | * @return An existing session or <code>null</code>. |
50 | 43 | */ |
51 | 44 | @Nullable Session findSession(@Nonnull Context ctx); |
52 | 45 |
|
53 | 46 | /** |
54 | 47 | * Delete a session from store. This method must NOT call {@link Session#destroy()}. |
55 | 48 | * |
56 | | - * @param id Session ID. |
| 49 | + * @param ctx Web context. |
| 50 | + * @param session Current session. |
57 | 51 | */ |
58 | | - void deleteSession(@Nonnull Context ctx); |
| 52 | + void deleteSession(@Nonnull Context ctx, @Nonnull Session session); |
| 53 | + |
| 54 | + /** |
| 55 | + * Session attributes/state has changed. Every time a session attribute is put or removed it, |
| 56 | + * this method is executed as notification callback. |
| 57 | + * |
| 58 | + * @param ctx Web context. |
| 59 | + * @param session Current session. |
| 60 | + */ |
| 61 | + void touchSession(@Nonnull Context ctx, @Nonnull Session session); |
59 | 62 |
|
60 | 63 | /** |
61 | 64 | * Save a session. This method must save: |
62 | 65 | * |
63 | 66 | * - Session attributes/data |
64 | | - * - Session metadata like: creationTime, lastAccessed time, etc. |
| 67 | + * - Optionally set Session metadata like: creationTime, lastAccessed time, etc. |
| 68 | + * |
| 69 | + * This method is call after response is send to client, so context and response shouldn't be |
| 70 | + * modified. |
65 | 71 | * |
66 | | - * @param session Session to save. |
| 72 | + * @param ctx Web context. |
| 73 | + * @param session Current session. |
67 | 74 | */ |
68 | | - void save(@Nonnull Context ctx); |
| 75 | + void saveSession(@Nonnull Context ctx, @Nonnull Session session); |
69 | 76 |
|
70 | | - static SessionStore memory() { |
71 | | - return memory(SID); |
| 77 | + /** |
| 78 | + * Creates a cookie based session and store data in memory. Session data is not keep after |
| 79 | + * restart. |
| 80 | + * |
| 81 | + * It uses the default session cookie: {@link SessionToken#SID}. |
| 82 | + * |
| 83 | + * @return Session store. |
| 84 | + */ |
| 85 | + static @Nonnull SessionStore memory() { |
| 86 | + return memory(SessionToken.SID); |
72 | 87 | } |
73 | 88 |
|
74 | | - static SessionStore memory(Cookie cookie) { |
| 89 | + /** |
| 90 | + * Creates a cookie based session and store data in memory. Session data is not keep after |
| 91 | + * restart. |
| 92 | + * |
| 93 | + * @param cookie Cookie to use. |
| 94 | + * @return Session store. |
| 95 | + */ |
| 96 | + static @Nonnull SessionStore memory(@Nonnull Cookie cookie) { |
75 | 97 | return memory(SessionToken.cookie(cookie)); |
76 | 98 | } |
77 | 99 |
|
78 | | - static SessionStore memory(SessionToken token) { |
| 100 | + /** |
| 101 | + * Creates a session store that save data in memory. Session data is not keep after restart. |
| 102 | + * |
| 103 | + * @param token Session token. |
| 104 | + * @return Session store. |
| 105 | + */ |
| 106 | + static @Nonnull SessionStore memory(@Nonnull SessionToken token) { |
79 | 107 | return new MemorySessionStore(token); |
80 | 108 | } |
| 109 | + |
| 110 | + /** |
| 111 | + * Creates a session store that save data into Cookie. Cookie data is signed it using |
| 112 | + * <code>HMAC_SHA256</code>. See {@link Cookie#sign(String, String)}. |
| 113 | + * |
| 114 | + * @param secret Secret token to signed data. |
| 115 | + * @param cookie Cookie to use. |
| 116 | + * @return A browser session store. |
| 117 | + */ |
| 118 | + static @Nonnull SessionStore cookie(@Nonnull String secret, @Nonnull Cookie cookie) { |
| 119 | + return new CookieSessionStore(secret, cookie); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Creates a session store that save data into Cookie. Cookie data is signed it using |
| 124 | + * <code>HMAC_SHA256</code>. See {@link Cookie#sign(String, String)}. |
| 125 | + * |
| 126 | + * It uses the default session cookie: {@link SessionToken#SID}. |
| 127 | + * |
| 128 | + * @param secret Secret token to signed data. |
| 129 | + * @return A browser session store. |
| 130 | + */ |
| 131 | + static @Nonnull SessionStore cookie(@Nonnull String secret) { |
| 132 | + return cookie(secret, SessionToken.SID); |
| 133 | + } |
81 | 134 | } |
0 commit comments