@adonisjs/session 6.4.0 → 7.0.0-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -42
- package/build/configure.d.ts +5 -0
- package/build/configure.js +18 -0
- package/build/index.d.ts +12 -0
- package/build/index.js +12 -0
- package/build/providers/session_provider.d.ts +13 -0
- package/build/providers/session_provider.js +43 -0
- package/build/src/bindings/api_client.d.ts +2 -0
- package/build/src/{Bindings/Tests.js → bindings/api_client.js} +8 -13
- package/build/src/bindings/http_context.d.ts +5 -0
- package/build/src/bindings/http_context.js +17 -0
- package/build/{adonis-typings/tests.d.ts → src/bindings/types.d.ts} +26 -5
- package/build/{adonis-typings/session.js → src/bindings/types.js} +2 -1
- package/build/src/{Client/index.d.ts → client.d.ts} +7 -15
- package/build/src/client.js +100 -0
- package/build/src/define_config.d.ts +5 -0
- package/build/src/define_config.js +13 -0
- package/build/src/{Drivers/Cookie.d.ts → drivers/cookie.d.ts} +4 -6
- package/build/src/{Drivers/Cookie.js → drivers/cookie.js} +10 -12
- package/build/src/{Drivers/File.d.ts → drivers/file.d.ts} +3 -8
- package/build/src/{Drivers/File.js → drivers/file.js} +20 -23
- package/build/src/{Drivers/Memory.d.ts → drivers/memory.d.ts} +2 -3
- package/build/src/{Drivers/Memory.js → drivers/memory.js} +3 -7
- package/build/src/{Drivers/Redis.d.ts → drivers/redis.d.ts} +5 -15
- package/build/src/drivers/redis.js +74 -0
- package/build/src/{Session/index.d.ts → session.d.ts} +6 -67
- package/build/src/session.js +371 -0
- package/build/src/session_manager.d.ts +38 -0
- package/build/src/session_manager.js +149 -0
- package/build/src/session_middleware.d.ts +5 -0
- package/build/src/session_middleware.js +20 -0
- package/build/src/{Store/index.d.ts → store.d.ts} +3 -7
- package/build/src/{Store/index.js → store.js} +18 -18
- package/build/src/types.d.ts +61 -0
- package/build/src/types.js +1 -0
- package/build/{templates/session.txt → stubs/config.stub} +12 -15
- package/build/stubs/main.d.ts +1 -0
- package/build/{adonis-typings/tests.js → stubs/main.js} +2 -3
- package/package.json +96 -134
- package/build/adonis-typings/container.d.ts +0 -14
- package/build/adonis-typings/container.js +0 -8
- package/build/adonis-typings/context.d.ts +0 -14
- package/build/adonis-typings/context.js +0 -8
- package/build/adonis-typings/index.d.ts +0 -4
- package/build/adonis-typings/index.js +0 -12
- package/build/adonis-typings/session.d.ts +0 -265
- package/build/config.d.ts +0 -13
- package/build/config.js +0 -18
- package/build/instructions.md +0 -10
- package/build/providers/SessionProvider.d.ts +0 -31
- package/build/providers/SessionProvider.js +0 -56
- package/build/src/Bindings/Server.d.ts +0 -10
- package/build/src/Bindings/Server.js +0 -42
- package/build/src/Bindings/Tests.d.ts +0 -7
- package/build/src/Client/index.js +0 -93
- package/build/src/Drivers/Redis.js +0 -73
- package/build/src/Session/index.js +0 -352
- package/build/src/SessionManager/index.d.ts +0 -78
- package/build/src/SessionManager/index.js +0 -148
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* @adonisjs/session
|
|
4
|
-
*
|
|
5
|
-
* (c) Harminder Virk <virk@adonisjs.com>
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.Session = void 0;
|
|
12
|
-
/// <reference path="../../adonis-typings/session.ts" />
|
|
13
|
-
const utils_1 = require("@poppinss/utils");
|
|
14
|
-
const helpers_1 = require("@poppinss/utils/build/helpers");
|
|
15
|
-
const Store_1 = require("../Store");
|
|
16
|
-
/**
|
|
17
|
-
* Session class exposes the API to read/write values to the session for
|
|
18
|
-
* a given request.
|
|
19
|
-
*/
|
|
20
|
-
class Session {
|
|
21
|
-
constructor(ctx, config, driver) {
|
|
22
|
-
this.ctx = ctx;
|
|
23
|
-
this.config = config;
|
|
24
|
-
this.driver = driver;
|
|
25
|
-
/**
|
|
26
|
-
* Set to true inside the `initiate` method
|
|
27
|
-
*/
|
|
28
|
-
this.initiated = false;
|
|
29
|
-
/**
|
|
30
|
-
* A boolean to know if it's a fresh session or not. Fresh
|
|
31
|
-
* sessions are those, whose session id is not present
|
|
32
|
-
* in cookie
|
|
33
|
-
*/
|
|
34
|
-
this.fresh = false;
|
|
35
|
-
/**
|
|
36
|
-
* A boolean to know if store is initiated in readonly mode
|
|
37
|
-
* or not. This is done during Websocket requests
|
|
38
|
-
*/
|
|
39
|
-
this.readonly = false;
|
|
40
|
-
/**
|
|
41
|
-
* Session id for the given request. A new session id is only
|
|
42
|
-
* generated when the cookie for the session id is missing
|
|
43
|
-
*/
|
|
44
|
-
this.sessionId = this.getSessionId();
|
|
45
|
-
/**
|
|
46
|
-
* A copy of previously set flash messages
|
|
47
|
-
*/
|
|
48
|
-
this.flashMessages = new Store_1.Store({});
|
|
49
|
-
/**
|
|
50
|
-
* Session id for the current request. It will be different
|
|
51
|
-
* from the "this.sessionId" when regenerate is called.
|
|
52
|
-
*/
|
|
53
|
-
this.currentSessionId = this.sessionId;
|
|
54
|
-
/**
|
|
55
|
-
* Whether or not to re-generate the session id before comitting
|
|
56
|
-
* session values.
|
|
57
|
-
*/
|
|
58
|
-
this.regeneratedSessionId = false;
|
|
59
|
-
/**
|
|
60
|
-
* A copy of flash messages. The `input` messages
|
|
61
|
-
* are overwritten when any of the input related
|
|
62
|
-
* methods are used.
|
|
63
|
-
*
|
|
64
|
-
* The `others` object is expanded with each call.
|
|
65
|
-
*/
|
|
66
|
-
this.responseFlashMessages = new Store_1.Store({});
|
|
67
|
-
/**
|
|
68
|
-
* Session key for setting flash messages
|
|
69
|
-
*/
|
|
70
|
-
this.flashMessagesKey = '__flash__';
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Returns a merged copy of flash messages or null
|
|
74
|
-
* when nothing is set
|
|
75
|
-
*/
|
|
76
|
-
setFlashMessages() {
|
|
77
|
-
if (this.responseFlashMessages.isEmpty) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
const { input, ...others } = this.responseFlashMessages.all();
|
|
81
|
-
this.put(this.flashMessagesKey, { ...input, ...others });
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Returns the existing session id or creates one.
|
|
85
|
-
*/
|
|
86
|
-
getSessionId() {
|
|
87
|
-
const sessionId = this.ctx.request.cookie(this.config.cookieName);
|
|
88
|
-
if (sessionId) {
|
|
89
|
-
this.ctx.logger.trace('existing session found');
|
|
90
|
-
return sessionId;
|
|
91
|
-
}
|
|
92
|
-
this.fresh = true;
|
|
93
|
-
this.ctx.logger.trace('generating new session id');
|
|
94
|
-
return (0, helpers_1.cuid)();
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Ensures the session store is initialized
|
|
98
|
-
*/
|
|
99
|
-
ensureIsReady() {
|
|
100
|
-
if (!this.initiated) {
|
|
101
|
-
throw new utils_1.Exception('Session store is not initiated yet. Make sure you are using the session hook', 500, 'E_RUNTIME_EXCEPTION');
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Raises exception when session store is in readonly mode
|
|
106
|
-
*/
|
|
107
|
-
ensureIsMutable() {
|
|
108
|
-
if (this.readonly) {
|
|
109
|
-
throw new utils_1.Exception('Session store is in readonly mode and cannot be mutated', 500, 'E_RUNTIME_EXCEPTION');
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Touches the session cookie
|
|
114
|
-
*/
|
|
115
|
-
touchSessionCookie() {
|
|
116
|
-
this.ctx.logger.trace('touching session cookie');
|
|
117
|
-
this.ctx.response.cookie(this.config.cookieName, this.sessionId, this.config.cookie);
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Commits the session value to the store
|
|
121
|
-
*/
|
|
122
|
-
async commitValuesToStore() {
|
|
123
|
-
this.ctx.logger.trace('persist session store with driver');
|
|
124
|
-
await this.driver.write(this.sessionId, this.store.toJSON());
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Touches the driver to make sure the session values doesn't expire
|
|
128
|
-
*/
|
|
129
|
-
async touchDriver() {
|
|
130
|
-
this.ctx.logger.trace('touch driver for liveliness');
|
|
131
|
-
await this.driver.touch(this.sessionId);
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Reading flash messages from the last HTTP request and
|
|
135
|
-
* updating the flash messages bag
|
|
136
|
-
*/
|
|
137
|
-
readLastRequestFlashMessage() {
|
|
138
|
-
if (this.readonly) {
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
this.flashMessages.update(this.pull(this.flashMessagesKey, null));
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Share flash messages & read only session's functions with views
|
|
145
|
-
* (only when view property exists)
|
|
146
|
-
*/
|
|
147
|
-
shareLocalsWithView() {
|
|
148
|
-
if (!this.ctx['view'] || typeof this.ctx['view'].share !== 'function') {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
this.ctx['view'].share({
|
|
152
|
-
flashMessages: this.flashMessages,
|
|
153
|
-
session: {
|
|
154
|
-
get: this.get.bind(this),
|
|
155
|
-
has: this.has.bind(this),
|
|
156
|
-
all: this.all.bind(this),
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Initiating the session by reading it's value from the
|
|
162
|
-
* driver and feeding it to a store.
|
|
163
|
-
*
|
|
164
|
-
* Multiple calls to `initiate` results in a noop.
|
|
165
|
-
*/
|
|
166
|
-
async initiate(readonly) {
|
|
167
|
-
if (this.initiated) {
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
this.readonly = readonly;
|
|
171
|
-
/**
|
|
172
|
-
* Profiling the driver read method
|
|
173
|
-
*/
|
|
174
|
-
await this.ctx.profiler.profileAsync('session:initiate', { driver: this.config.driver }, async () => {
|
|
175
|
-
const contents = await this.driver.read(this.sessionId);
|
|
176
|
-
this.store = new Store_1.Store(contents);
|
|
177
|
-
});
|
|
178
|
-
this.initiated = true;
|
|
179
|
-
this.readLastRequestFlashMessage();
|
|
180
|
-
this.shareLocalsWithView();
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Re-generates the session id. This can is used to avoid
|
|
184
|
-
* session fixation attacks.
|
|
185
|
-
*/
|
|
186
|
-
regenerate() {
|
|
187
|
-
this.ctx.logger.trace('explicitly re-generating session id');
|
|
188
|
-
this.sessionId = (0, helpers_1.cuid)();
|
|
189
|
-
this.regeneratedSessionId = true;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Set/update session value
|
|
193
|
-
*/
|
|
194
|
-
put(key, value) {
|
|
195
|
-
this.ensureIsReady();
|
|
196
|
-
this.ensureIsMutable();
|
|
197
|
-
this.store.set(key, value);
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Find if the value exists in the session
|
|
201
|
-
*/
|
|
202
|
-
has(key) {
|
|
203
|
-
this.ensureIsReady();
|
|
204
|
-
return this.store.has(key);
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* Get value from the session. The default value is returned
|
|
208
|
-
* when actual value is `undefined`
|
|
209
|
-
*/
|
|
210
|
-
get(key, defaultValue) {
|
|
211
|
-
this.ensureIsReady();
|
|
212
|
-
return this.store.get(key, defaultValue);
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Returns everything from the session
|
|
216
|
-
*/
|
|
217
|
-
all() {
|
|
218
|
-
this.ensureIsReady();
|
|
219
|
-
return this.store.all();
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Remove value for a given key from the session
|
|
223
|
-
*/
|
|
224
|
-
forget(key) {
|
|
225
|
-
this.ensureIsReady();
|
|
226
|
-
this.ensureIsMutable();
|
|
227
|
-
this.store.unset(key);
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* The method is equivalent to calling `session.get` followed
|
|
231
|
-
* by `session.forget`
|
|
232
|
-
*/
|
|
233
|
-
pull(key, defaultValue) {
|
|
234
|
-
this.ensureIsReady();
|
|
235
|
-
this.ensureIsMutable();
|
|
236
|
-
return this.store.pull(key, defaultValue);
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Increment value for a number inside the session store. The
|
|
240
|
-
* method raises an error when underlying value is not
|
|
241
|
-
* a number
|
|
242
|
-
*/
|
|
243
|
-
increment(key, steps = 1) {
|
|
244
|
-
this.ensureIsReady();
|
|
245
|
-
this.ensureIsMutable();
|
|
246
|
-
this.store.increment(key, steps);
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Decrement value for a number inside the session store. The
|
|
250
|
-
* method raises an error when underlying value is not
|
|
251
|
-
* a number
|
|
252
|
-
*/
|
|
253
|
-
decrement(key, steps = 1) {
|
|
254
|
-
this.ensureIsReady();
|
|
255
|
-
this.ensureIsMutable();
|
|
256
|
-
this.store.decrement(key, steps);
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Remove everything from the session
|
|
260
|
-
*/
|
|
261
|
-
clear() {
|
|
262
|
-
this.ensureIsReady();
|
|
263
|
-
this.ensureIsMutable();
|
|
264
|
-
this.store.clear();
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Add a new flash message
|
|
268
|
-
*/
|
|
269
|
-
flash(key, value) {
|
|
270
|
-
this.ensureIsReady();
|
|
271
|
-
this.ensureIsMutable();
|
|
272
|
-
/**
|
|
273
|
-
* Update value
|
|
274
|
-
*/
|
|
275
|
-
if (typeof key === 'string') {
|
|
276
|
-
if (value) {
|
|
277
|
-
this.responseFlashMessages.set(key, value);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
else {
|
|
281
|
-
this.responseFlashMessages.merge(key);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* Flash all form values
|
|
286
|
-
*/
|
|
287
|
-
flashAll() {
|
|
288
|
-
this.ensureIsReady();
|
|
289
|
-
this.ensureIsMutable();
|
|
290
|
-
this.responseFlashMessages.set('input', this.ctx.request.original());
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* Flash all form values except mentioned keys
|
|
294
|
-
*/
|
|
295
|
-
flashExcept(keys) {
|
|
296
|
-
this.ensureIsReady();
|
|
297
|
-
this.ensureIsMutable();
|
|
298
|
-
this.responseFlashMessages.set('input', utils_1.lodash.omit(this.ctx.request.original(), keys));
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* Flash only defined keys from the form values
|
|
302
|
-
*/
|
|
303
|
-
flashOnly(keys) {
|
|
304
|
-
this.ensureIsReady();
|
|
305
|
-
this.ensureIsMutable();
|
|
306
|
-
this.responseFlashMessages.set('input', utils_1.lodash.pick(this.ctx.request.original(), keys));
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Reflash existing flash messages
|
|
310
|
-
*/
|
|
311
|
-
reflash() {
|
|
312
|
-
this.flash(this.flashMessages.all());
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Reflash selected keys from the existing flash messages
|
|
316
|
-
*/
|
|
317
|
-
reflashOnly(keys) {
|
|
318
|
-
this.flash(utils_1.lodash.pick(this.flashMessages.all(), keys));
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* Omit selected keys from the existing flash messages
|
|
322
|
-
* and flash the rest of values
|
|
323
|
-
*/
|
|
324
|
-
reflashExcept(keys) {
|
|
325
|
-
this.flash(utils_1.lodash.omit(this.flashMessages.all(), keys));
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* Writes value to the underlying session driver.
|
|
329
|
-
*/
|
|
330
|
-
async commit() {
|
|
331
|
-
await this.ctx.profiler.profileAsync('session:commit', { driver: this.config.driver }, async () => {
|
|
332
|
-
if (!this.initiated) {
|
|
333
|
-
this.touchSessionCookie();
|
|
334
|
-
await this.touchDriver();
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Cleanup old session and re-generate new session
|
|
339
|
-
*/
|
|
340
|
-
if (this.regeneratedSessionId) {
|
|
341
|
-
await this.driver.destroy(this.currentSessionId);
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* Touch the session cookie to keep it alive.
|
|
345
|
-
*/
|
|
346
|
-
this.touchSessionCookie();
|
|
347
|
-
this.setFlashMessages();
|
|
348
|
-
await this.commitValuesToStore();
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
exports.Session = Session;
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @adonisjs/session
|
|
3
|
-
*
|
|
4
|
-
* (c) Harminder Virk <virk@adonisjs.com>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
/// <reference types="@adonisjs/application/build/adonis-typings" />
|
|
10
|
-
import { ApplicationContract } from '@ioc:Adonis/Core/Application';
|
|
11
|
-
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
|
|
12
|
-
import { SessionConfig, ExtendCallback, SessionManagerContract, SessionClientContract } from '@ioc:Adonis/Addons/Session';
|
|
13
|
-
import { Session } from '../Session';
|
|
14
|
-
/**
|
|
15
|
-
* Session manager exposes the API to create session instance for a given
|
|
16
|
-
* request and also add new drivers.
|
|
17
|
-
*/
|
|
18
|
-
export declare class SessionManager implements SessionManagerContract {
|
|
19
|
-
application: ApplicationContract;
|
|
20
|
-
/**
|
|
21
|
-
* A private map of drivers added from outside in.
|
|
22
|
-
*/
|
|
23
|
-
private extendedDrivers;
|
|
24
|
-
/**
|
|
25
|
-
* Reference to session config
|
|
26
|
-
*/
|
|
27
|
-
private config;
|
|
28
|
-
constructor(application: ApplicationContract, config: SessionConfig);
|
|
29
|
-
/**
|
|
30
|
-
* Validates the config
|
|
31
|
-
*/
|
|
32
|
-
private validateConfig;
|
|
33
|
-
/**
|
|
34
|
-
* Processes the config and decides the `expires` option for the cookie
|
|
35
|
-
*/
|
|
36
|
-
private processConfig;
|
|
37
|
-
/**
|
|
38
|
-
* Returns an instance of cookie driver
|
|
39
|
-
*/
|
|
40
|
-
private createCookieDriver;
|
|
41
|
-
/**
|
|
42
|
-
* Returns an instance of the memory driver
|
|
43
|
-
*/
|
|
44
|
-
private createMemoryDriver;
|
|
45
|
-
/**
|
|
46
|
-
* Returns an instance of file driver
|
|
47
|
-
*/
|
|
48
|
-
private createFileDriver;
|
|
49
|
-
/**
|
|
50
|
-
* Returns an instance of redis driver
|
|
51
|
-
*/
|
|
52
|
-
private createRedisDriver;
|
|
53
|
-
/**
|
|
54
|
-
* Creates an instance of extended driver
|
|
55
|
-
*/
|
|
56
|
-
private createExtendedDriver;
|
|
57
|
-
/**
|
|
58
|
-
* Creates an instance of driver by looking at the config value `driver`.
|
|
59
|
-
* An hard exception is raised in case of invalid driver name
|
|
60
|
-
*/
|
|
61
|
-
private createDriver;
|
|
62
|
-
/**
|
|
63
|
-
* Find if the sessions are enabled
|
|
64
|
-
*/
|
|
65
|
-
isEnabled(): boolean;
|
|
66
|
-
/**
|
|
67
|
-
* Creates an instance of the session client
|
|
68
|
-
*/
|
|
69
|
-
client(): SessionClientContract;
|
|
70
|
-
/**
|
|
71
|
-
* Creates a new session instance for a given HTTP request
|
|
72
|
-
*/
|
|
73
|
-
create(ctx: HttpContextContract): Session;
|
|
74
|
-
/**
|
|
75
|
-
* Extend the drivers list by adding a new one.
|
|
76
|
-
*/
|
|
77
|
-
extend(driver: string, callback: ExtendCallback): void;
|
|
78
|
-
}
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @adonisjs/session
|
|
4
|
-
*
|
|
5
|
-
* (c) Harminder Virk <virk@adonisjs.com>
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.SessionManager = void 0;
|
|
12
|
-
const helpers_1 = require("@poppinss/utils/build/helpers");
|
|
13
|
-
const utils_1 = require("@poppinss/utils");
|
|
14
|
-
const Session_1 = require("../Session");
|
|
15
|
-
/**
|
|
16
|
-
* Session manager exposes the API to create session instance for a given
|
|
17
|
-
* request and also add new drivers.
|
|
18
|
-
*/
|
|
19
|
-
class SessionManager {
|
|
20
|
-
constructor(application, config) {
|
|
21
|
-
this.application = application;
|
|
22
|
-
/**
|
|
23
|
-
* A private map of drivers added from outside in.
|
|
24
|
-
*/
|
|
25
|
-
this.extendedDrivers = new Map();
|
|
26
|
-
this.validateConfig(config);
|
|
27
|
-
this.processConfig(config);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Validates the config
|
|
31
|
-
*/
|
|
32
|
-
validateConfig(config) {
|
|
33
|
-
const validator = new utils_1.ManagerConfigValidator(config, 'session', 'config/session');
|
|
34
|
-
validator.validateDefault('driver');
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Processes the config and decides the `expires` option for the cookie
|
|
38
|
-
*/
|
|
39
|
-
processConfig(config) {
|
|
40
|
-
/**
|
|
41
|
-
* Explicitly overwriting `cookie.expires` and `cookie.maxAge` from
|
|
42
|
-
* the user defined config
|
|
43
|
-
*/
|
|
44
|
-
const processedConfig = Object.assign({ enabled: true }, config, {
|
|
45
|
-
cookie: {
|
|
46
|
-
...config.cookie,
|
|
47
|
-
expires: undefined,
|
|
48
|
-
maxAge: undefined,
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
/**
|
|
52
|
-
* Set the max age when `clearWithBrowser = false`. Otherwise cookie
|
|
53
|
-
* is a session cookie
|
|
54
|
-
*/
|
|
55
|
-
if (!processedConfig.clearWithBrowser) {
|
|
56
|
-
const age = typeof processedConfig.age === 'string'
|
|
57
|
-
? Math.round(helpers_1.string.toMs(processedConfig.age) / 1000)
|
|
58
|
-
: processedConfig.age;
|
|
59
|
-
processedConfig.cookie.maxAge = age;
|
|
60
|
-
}
|
|
61
|
-
this.config = processedConfig;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Returns an instance of cookie driver
|
|
65
|
-
*/
|
|
66
|
-
createCookieDriver(ctx) {
|
|
67
|
-
const { CookieDriver } = require('../Drivers/Cookie');
|
|
68
|
-
return new CookieDriver(this.config, ctx);
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Returns an instance of the memory driver
|
|
72
|
-
*/
|
|
73
|
-
createMemoryDriver() {
|
|
74
|
-
const { MemoryDriver } = require('../Drivers/Memory');
|
|
75
|
-
return new MemoryDriver();
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Returns an instance of file driver
|
|
79
|
-
*/
|
|
80
|
-
createFileDriver() {
|
|
81
|
-
const { FileDriver } = require('../Drivers/File');
|
|
82
|
-
return new FileDriver(this.config);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Returns an instance of redis driver
|
|
86
|
-
*/
|
|
87
|
-
createRedisDriver() {
|
|
88
|
-
const { RedisDriver } = require('../Drivers/Redis');
|
|
89
|
-
if (!this.application.container.hasBinding('Adonis/Addons/Redis')) {
|
|
90
|
-
throw new Error('Install "@adonisjs/redis" in order to use the redis driver for storing sessions');
|
|
91
|
-
}
|
|
92
|
-
return new RedisDriver(this.config, this.application.container.use('Adonis/Addons/Redis'));
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Creates an instance of extended driver
|
|
96
|
-
*/
|
|
97
|
-
createExtendedDriver(ctx) {
|
|
98
|
-
if (!this.extendedDrivers.has(this.config.driver)) {
|
|
99
|
-
throw new utils_1.Exception(`"${this.config.driver}" is not a valid session driver`, 500, 'E_INVALID_SESSION_DRIVER');
|
|
100
|
-
}
|
|
101
|
-
return this.extendedDrivers.get(this.config.driver)(this, this.config, ctx);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Creates an instance of driver by looking at the config value `driver`.
|
|
105
|
-
* An hard exception is raised in case of invalid driver name
|
|
106
|
-
*/
|
|
107
|
-
createDriver(ctx) {
|
|
108
|
-
switch (this.config.driver) {
|
|
109
|
-
case 'cookie':
|
|
110
|
-
return this.createCookieDriver(ctx);
|
|
111
|
-
case 'file':
|
|
112
|
-
return this.createFileDriver();
|
|
113
|
-
case 'redis':
|
|
114
|
-
return this.createRedisDriver();
|
|
115
|
-
case 'memory':
|
|
116
|
-
return this.createMemoryDriver();
|
|
117
|
-
default:
|
|
118
|
-
return this.createExtendedDriver(ctx);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Find if the sessions are enabled
|
|
123
|
-
*/
|
|
124
|
-
isEnabled() {
|
|
125
|
-
return this.config.enabled;
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Creates an instance of the session client
|
|
129
|
-
*/
|
|
130
|
-
client() {
|
|
131
|
-
const { SessionClient } = require('../Client');
|
|
132
|
-
const CookieClient = this.application.container.resolveBinding('Adonis/Core/CookieClient');
|
|
133
|
-
return new SessionClient(this.config, this.createMemoryDriver(), CookieClient, {});
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Creates a new session instance for a given HTTP request
|
|
137
|
-
*/
|
|
138
|
-
create(ctx) {
|
|
139
|
-
return new Session_1.Session(ctx, this.config, this.createDriver(ctx));
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Extend the drivers list by adding a new one.
|
|
143
|
-
*/
|
|
144
|
-
extend(driver, callback) {
|
|
145
|
-
this.extendedDrivers.set(driver, callback);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
exports.SessionManager = SessionManager;
|