-
Notifications
You must be signed in to change notification settings - Fork 3
/
cookie.ts
179 lines (169 loc) · 4.87 KB
/
cookie.ts
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
import { CookieSerializeOptions, serialize as cookieSerialize } from "cookie";
import { JWTOptions } from "../services/cryptoservice";
import { WebContext } from "./context";
import { HttpContext } from "./httpcontext";
/**
* Cookie cannot be more than 4096, so we split them by this constant
* @hidden
*/
const SPLIT = 4096;
/**
* Cookie Options
*/
export class CookieOptions implements Omit<CookieSerializeOptions, "domain"> {
/**
* @default lax
*/
sameSite?: "none" | "strict" | "lax";
/**
* if true domain will be set to the request hostname
* if undefined no domain will be output (browser will use the current domain and only this one)
* if a string is provided it will be used as the domain
*
* When provided a domain is setting the cookie to be available to all subdomains
*/
domain?: string | true;
/**
* @minimum 1
* @default 86400 * 7
*/
maxAge?: number;
/**
* @default /
*/
path?: string;
/**
* @default true
*/
httpOnly?: boolean;
/**
* If not set will be true if https request and false otherwise
* If defined it will be set to the value
*/
secure?: boolean;
/**
* Name of the cookie
*/
name?: string;
/**
* Load with default value
* @param options
* @param httpContext
*/
constructor(options: Partial<CookieOptions>, httpContext?: HttpContext) {
Object.assign(this, options);
this.httpOnly ??= true;
this.path ??= "/";
this.maxAge ??= 86400 * 7;
this.sameSite ??= "lax";
this.name ??= "webda";
if (httpContext) {
if (this.domain === true) {
this.domain = httpContext.getHostName();
}
this.secure ??= httpContext.getProtocol() === "https:";
}
}
}
/**
* Object that handle the session
*
* To get stateless server the session is encrypted inside a cookie, so you should not store large amount of data in it
* If you need big session then i would suggest to use a Memcache store with the user.uuid as a key
*
* It is part of the core framework implementation, you should not rely on any method of this object, as the implementation can change if needed
* An object session is exposed by the framework, so use this one ( for now it is a SecureCookie.getProxy() but can evolve )
*
* The object use Object.observe if available or try Proxy in other case, so old JS VM won't run it
*
* You cannot store variables starting with _ in the session if you do use them they won't be persist, so it can be used to add some context
*
* @category CoreFeatures
*/
export class SecureCookie {
/**
* Load cookie
*
* It manages the split of the cookie to 4096 if required
*
* @param name cookie name
* @param options
* @param context
* @returns
*/
static async load(name: string, context: WebContext, options?: JWTOptions) {
let cookies = {};
let raw = "";
let session = await context.newSession();
name ??= "webda";
// No http context
if (!context.getHttpContext()) {
return session;
}
cookies = context.getHttpContext().getCookies();
if (cookies) {
raw = cookies[name] || "";
let j = 2;
let cookieName = `${name}${j++}`;
while (cookies[cookieName]) {
raw += cookies[cookieName];
cookieName = `${name}${j++}`;
}
}
// No cookie found or empty
if (raw === "") {
return session;
}
try {
return Object.assign(session, await context.getWebda().getCrypto().jwtVerify(raw, options));
} catch (err) {
context.log("WARN", "Ignoring bad cookie", `'${raw}'`, "from", context.getHttpContext());
return session;
}
}
/**
* Send a cookie, split it if required
*
* @param name
* @param context
* @param data
* @param options
*/
static async save(
name: string,
context: WebContext,
data: any,
options?: JWTOptions,
cookieOptions?: Partial<CookieOptions>
) {
let value = await context.getWebda().getCrypto().jwtSign(Object.assign({}, data), options);
this.sendCookie(context, name, value, new CookieOptions(cookieOptions, context.getHttpContext()));
}
/**
* Will send the cookie to context and split it
* if needed as max length for cookies are 4096
* including the params (the whole Set-Cookie header)
*
* @param ctx to send cookie to
* @param name name of the cookie
* @param value of the cookie
* @param params for the cookie
*/
static sendCookie(ctx: WebContext, name: string, value: string, params: CookieOptions) {
let j = 1;
name ??= "webda";
let cookieName = name;
let limit;
const mapLength = cookieSerialize(name, "", <CookieSerializeOptions>params).length;
for (let i = 0; i < value.length; ) {
limit = SPLIT - mapLength;
if (j > 1) {
cookieName = `${name}${j}`;
limit--;
}
ctx.cookie(cookieName, value.substring(i, i + limit), params);
j++;
i += limit;
}
}
}