forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionToken.java
More file actions
246 lines (213 loc) · 6.68 KB
/
SessionToken.java
File metadata and controls
246 lines (213 loc) · 6.68 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
/**
* 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.MultipleSessionToken;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.security.SecureRandom;
import java.util.Base64;
/**
* Find, save and delete a session token (cookie, header, parameter, etc)
* into/from the web {@link Context}.
*
* @author edgar
*/
public interface SessionToken {
/**
* Looks for a session ID from request cookie headers. This strategy:
*
* - find a token from a request cookie.
* - on save, set a response cookie on new sessions or when cookie has a max-age value.
* - on destroy, expire the cookie.
*/
class CookieID implements SessionToken {
private final Cookie cookie;
/**
* Creates a Cookie ID.
*
* @param cookie Cookie to use.
*/
public CookieID(@Nonnull Cookie cookie) {
this.cookie = cookie;
}
@Nullable @Override public String findToken(@Nonnull Context ctx) {
return ctx.cookieMap().get(cookie.getName());
}
@Override public void saveToken(@Nonnull Context ctx, @Nonnull String token) {
// FIXME: Review, bc we don;t need this
// String existingId = findToken(ctx);
// write cookie for new or expiring session
//if (existingId == null || cookie.getMaxAge() > 0) {
ctx.setResponseCookie(cookie.clone().setValue(token));
//}
}
@Override public void deleteToken(@Nonnull Context ctx, @Nonnull String token) {
ctx.setResponseCookie(cookie.clone().setValue(token).setMaxAge(0));
}
}
/**
* Looks for a session ID from request headers. This strategy:
*
* - find a token from a request header.
* - on save, send the header back as response header.
* - on session destroy. don't send response header back.
*/
class HeaderID implements SessionToken {
private final String name;
/**
* Creates a new Header ID.
*
* @param name Header's name.
*/
public HeaderID(@Nonnull String name) {
this.name = name;
}
@Nullable @Override public String findToken(@Nonnull Context ctx) {
return ctx.headerMap().get(name);
}
@Override public void saveToken(@Nonnull Context ctx, @Nonnull String token) {
ctx.setResponseHeader(name, token);
}
@Override public void deleteToken(@Nonnull Context ctx, @Nonnull String token) {
ctx.removeResponseHeader(name);
}
}
/**
* Looks for a session token from request cookie. This strategy:
*
* - find a token from a request cookie.
* - on save, set a response cookie.
* - on destroy, expire the cookie.
*/
class SignedCookie implements SessionToken {
private final Cookie cookie;
/**
* Creates a Cookie ID.
*
* @param cookie Cookie to use.
*/
public SignedCookie(@Nonnull Cookie cookie) {
this.cookie = cookie;
}
@Nullable @Override public String findToken(@Nonnull Context ctx) {
return ctx.cookieMap().get(cookie.getName());
}
@Override public void saveToken(@Nonnull Context ctx, @Nonnull String token) {
ctx.setResponseCookie(cookie.clone().setValue(token));
}
@Override public void deleteToken(@Nonnull Context ctx, @Nonnull String token) {
ctx.setResponseCookie(cookie.clone().setMaxAge(0));
}
}
/**
* Default cookie for cookie based session stores.
* Uses <code>jooby.sid</code> as name. It never expires, use the root, only for HTTP.
*/
Cookie SID = new Cookie("jooby.sid")
.setMaxAge(-1)
.setHttpOnly(true)
.setPath("/");
/** Secure random for default session token generator. */
SecureRandom RND = new SecureRandom();
/** Size of default token generator. */
int ID_SIZE = 30;
/**
* Generate a new token. This implementation produces an url encoder ID using a secure random
* of {@link #ID_SIZE}.
*
* @return A new token.
*/
default @Nonnull String newToken() {
byte[] bytes = new byte[ID_SIZE];
RND.nextBytes(bytes);
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
}
/**
* Find session ID.
*
* @param ctx Web context.
* @return Session ID or <code>null</code>.
*/
@Nullable String findToken(@Nonnull Context ctx);
/**
* Save session ID in the web context.
*
* @param ctx Web context.
* @param token Token/data to save.
*/
void saveToken(@Nonnull Context ctx, @Nonnull String token);
/**
* Delete session ID in the web context.
*
* @param ctx Web context.
* @param token Token/data to delete.
*/
void deleteToken(@Nonnull Context ctx, @Nonnull String token);
/* **********************************************************************************************
* Factory methods
* **********************************************************************************************
*/
/**
* Create a cookie-based Session ID. This strategy:
*
* - find a token from a request cookie.
* - on save, set a response cookie on new sessions or when cookie has a max-age value.
* - on destroy, expire the cookie.
*
* @param cookie Cookie to use.
* @return Session Token.
*/
static @Nonnull SessionToken cookieId(@Nonnull Cookie cookie) {
return new CookieID(cookie);
}
/**
* Create a signed-cookie-based Session token. This strategy:
*
* - find a token from a request cookie.
* - on save, set a response cookie.
* - on destroy, expire the cookie.
*
* @param cookie Cookie to use.
* @return Session Token.
*/
static @Nonnull SessionToken signedCookie(@Nonnull Cookie cookie) {
return new SignedCookie(cookie);
}
/**
* Create a header-based Session Token. This strategy:
*
* - find a token from a request header.
* - on save, send the header back as response header.
* - on session destroy. don't send response header back.
*
* @param name Header name.
* @return Session Token.
*/
static @Nonnull SessionToken header(@Nonnull String name) {
return new HeaderID(name);
}
/**
* Combine/compose two or more session tokens. Example:
*
* <pre>{@code
* SessionToken token = SessionToken.combine(
* SessionToken.header("TOKEN"),
* SessionToken.cookie(SID)
* );
* }
* </pre>
*
* On new session, creates a response header and cookie.
* On save token, generates a response header or cookie based on best matches.
* On delete token, generates a response header or cookie based on best matches.
*
* @param tokens Tokens to use.
* @return A composed session token.
*/
static @Nonnull SessionToken combine(@Nonnull SessionToken... tokens) {
return new MultipleSessionToken(tokens);
}
}