@adonisjs/session 6.3.0 → 7.0.0-0
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/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} +14 -10
- 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} +30 -3
- 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 +373 -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} +13 -18
- package/build/stubs/main.d.ts +1 -0
- package/build/{adonis-typings/tests.js → stubs/main.js} +2 -3
- package/package.json +96 -132
- 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/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,56 +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
|
-
/**
|
|
12
|
-
* Session provider for AdonisJS
|
|
13
|
-
*/
|
|
14
|
-
class SessionProvider {
|
|
15
|
-
constructor(app) {
|
|
16
|
-
this.app = app;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Register Session Manager
|
|
20
|
-
*/
|
|
21
|
-
register() {
|
|
22
|
-
this.app.container.singleton('Adonis/Addons/Session', () => {
|
|
23
|
-
const { SessionManager } = require('../src/SessionManager');
|
|
24
|
-
return new SessionManager(this.app, this.app.config.get('session', {}));
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Register bindings for tests
|
|
29
|
-
*/
|
|
30
|
-
registerTestsBindings() {
|
|
31
|
-
this.app.container.withBindings([
|
|
32
|
-
'Japa/Preset/ApiRequest',
|
|
33
|
-
'Japa/Preset/ApiResponse',
|
|
34
|
-
'Japa/Preset/ApiClient',
|
|
35
|
-
'Adonis/Addons/Session',
|
|
36
|
-
], (ApiRequest, ApiResponse, ApiClient, Session) => {
|
|
37
|
-
const { defineTestsBindings } = require('../src/Bindings/Tests');
|
|
38
|
-
defineTestsBindings(ApiRequest, ApiResponse, ApiClient, Session);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Register server bindings
|
|
43
|
-
*/
|
|
44
|
-
registerServerBindings() {
|
|
45
|
-
this.app.container.withBindings(['Adonis/Core/Server', 'Adonis/Core/HttpContext', 'Adonis/Addons/Session'], (Server, HttpContext, Session) => {
|
|
46
|
-
const { defineServerBindings } = require('../src/Bindings/Server');
|
|
47
|
-
defineServerBindings(HttpContext, Server, Session);
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
boot() {
|
|
51
|
-
this.registerServerBindings();
|
|
52
|
-
this.registerTestsBindings();
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
exports.default = SessionProvider;
|
|
56
|
-
SessionProvider.needsApplication = true;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/// <reference path="../../adonis-typings/index.d.ts" />
|
|
2
|
-
/// <reference types="@adonisjs/http-server/build/adonis-typings" />
|
|
3
|
-
import { ServerContract } from '@ioc:Adonis/Core/Server';
|
|
4
|
-
import { SessionManagerContract } from '@ioc:Adonis/Addons/Session';
|
|
5
|
-
import { HttpContextConstructorContract } from '@ioc:Adonis/Core/HttpContext';
|
|
6
|
-
/**
|
|
7
|
-
* Share "session" with the HTTP context. Define hooks to initiate and
|
|
8
|
-
* commit session when sessions are enabled.
|
|
9
|
-
*/
|
|
10
|
-
export declare function defineServerBindings(HttpContext: HttpContextConstructorContract, Server: ServerContract, Session: SessionManagerContract): void;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* @adonisjs/session
|
|
4
|
-
*
|
|
5
|
-
* (c) AdonisJS
|
|
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.defineServerBindings = void 0;
|
|
12
|
-
/**
|
|
13
|
-
* Share "session" with the HTTP context. Define hooks to initiate and
|
|
14
|
-
* commit session when sessions are enabled.
|
|
15
|
-
*/
|
|
16
|
-
function defineServerBindings(HttpContext, Server, Session) {
|
|
17
|
-
/**
|
|
18
|
-
* Sharing session with the context
|
|
19
|
-
*/
|
|
20
|
-
HttpContext.getter('session', function session() {
|
|
21
|
-
return Session.create(this);
|
|
22
|
-
}, true);
|
|
23
|
-
/**
|
|
24
|
-
* Do not register hooks when sessions are disabled
|
|
25
|
-
*/
|
|
26
|
-
if (!Session.isEnabled()) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Initiate session store
|
|
31
|
-
*/
|
|
32
|
-
Server.hooks.before(async (ctx) => {
|
|
33
|
-
await ctx.session.initiate(false);
|
|
34
|
-
});
|
|
35
|
-
/**
|
|
36
|
-
* Commit store mutations
|
|
37
|
-
*/
|
|
38
|
-
Server.hooks.after(async (ctx) => {
|
|
39
|
-
await ctx.session.commit();
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
exports.defineServerBindings = defineServerBindings;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/// <reference path="../../adonis-typings/index.d.ts" />
|
|
2
|
-
import { ContainerBindings } from '@ioc:Adonis/Core/Application';
|
|
3
|
-
import { SessionManagerContract } from '@ioc:Adonis/Addons/Session';
|
|
4
|
-
/**
|
|
5
|
-
* Define test bindings
|
|
6
|
-
*/
|
|
7
|
-
export declare function defineTestsBindings(ApiRequest: ContainerBindings['Japa/Preset/ApiRequest'], ApiResponse: ContainerBindings['Japa/Preset/ApiResponse'], ApiClient: ContainerBindings['Japa/Preset/ApiClient'], SessionManager: SessionManagerContract): void;
|
|
@@ -1,93 +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.SessionClient = void 0;
|
|
12
|
-
/// <reference path="../../adonis-typings/index.ts" />
|
|
13
|
-
const helpers_1 = require("@poppinss/utils/build/helpers");
|
|
14
|
-
const Store_1 = require("../Store");
|
|
15
|
-
/**
|
|
16
|
-
* SessionClient exposes the API to set session data as a client
|
|
17
|
-
*/
|
|
18
|
-
class SessionClient extends Store_1.Store {
|
|
19
|
-
constructor(config, driver, cookieClient, values) {
|
|
20
|
-
super(values);
|
|
21
|
-
this.config = config;
|
|
22
|
-
this.driver = driver;
|
|
23
|
-
this.cookieClient = cookieClient;
|
|
24
|
-
/**
|
|
25
|
-
* Each instance of client works on a single session id. Generate
|
|
26
|
-
* multiple client instances for a different session id
|
|
27
|
-
*/
|
|
28
|
-
this.sessionId = (0, helpers_1.cuid)();
|
|
29
|
-
/**
|
|
30
|
-
* Session key for setting flash messages
|
|
31
|
-
*/
|
|
32
|
-
this.flashMessagesKey = '__flash__';
|
|
33
|
-
/**
|
|
34
|
-
* Flash messages store. They are merged with the session data during
|
|
35
|
-
* commit
|
|
36
|
-
*/
|
|
37
|
-
this.flashMessages = new Store_1.Store({});
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Find if the sessions are enabled
|
|
41
|
-
*/
|
|
42
|
-
isEnabled() {
|
|
43
|
-
return this.config.enabled;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Load session from the driver
|
|
47
|
-
*/
|
|
48
|
-
async load(cookies) {
|
|
49
|
-
const sessionIdCookie = cookies[this.config.cookieName];
|
|
50
|
-
const sessionId = sessionIdCookie ? sessionIdCookie.value : this.sessionId;
|
|
51
|
-
const contents = await this.driver.read(sessionId);
|
|
52
|
-
const store = new Store_1.Store(contents);
|
|
53
|
-
const flashMessages = store.pull(this.flashMessagesKey, null);
|
|
54
|
-
return {
|
|
55
|
-
session: store.all(),
|
|
56
|
-
flashMessages,
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Commits the session data to the session store and returns
|
|
61
|
-
* the session id and cookie name for it to be accessible
|
|
62
|
-
* by the server
|
|
63
|
-
*/
|
|
64
|
-
async commit() {
|
|
65
|
-
this.set(this.flashMessagesKey, this.flashMessages.all());
|
|
66
|
-
await this.driver.write(this.sessionId, this.toJSON());
|
|
67
|
-
/**
|
|
68
|
-
* Clear from the session client memory
|
|
69
|
-
*/
|
|
70
|
-
this.clear();
|
|
71
|
-
this.flashMessages.clear();
|
|
72
|
-
return {
|
|
73
|
-
sessionId: this.sessionId,
|
|
74
|
-
signedSessionId: this.cookieClient.sign(this.config.cookieName, this.sessionId),
|
|
75
|
-
cookieName: this.config.cookieName,
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Clear the session store
|
|
80
|
-
*/
|
|
81
|
-
async forget() {
|
|
82
|
-
/**
|
|
83
|
-
* Clear from the session client memory
|
|
84
|
-
*/
|
|
85
|
-
this.clear();
|
|
86
|
-
this.flashMessages.clear();
|
|
87
|
-
/**
|
|
88
|
-
* Clear with the driver
|
|
89
|
-
*/
|
|
90
|
-
await this.driver.destroy(this.sessionId);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
exports.SessionClient = SessionClient;
|
|
@@ -1,73 +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.RedisDriver = void 0;
|
|
12
|
-
/// <reference path="../../adonis-typings/index.ts" />
|
|
13
|
-
const utils_1 = require("@poppinss/utils");
|
|
14
|
-
const helpers_1 = require("@poppinss/utils/build/helpers");
|
|
15
|
-
/**
|
|
16
|
-
* File driver to read/write session to filesystem
|
|
17
|
-
*/
|
|
18
|
-
class RedisDriver {
|
|
19
|
-
constructor(config, redis) {
|
|
20
|
-
this.config = config;
|
|
21
|
-
this.redis = redis;
|
|
22
|
-
/**
|
|
23
|
-
* Convert milliseconds to seconds
|
|
24
|
-
*/
|
|
25
|
-
this.ttl = Math.round((typeof this.config.age === 'string' ? helpers_1.string.toMs(this.config.age) : this.config.age) / 1000);
|
|
26
|
-
if (!this.config.redisConnection) {
|
|
27
|
-
throw new utils_1.Exception('Missing redisConnection for session redis driver inside "config/session" file', 500, 'E_INVALID_SESSION_DRIVER_CONFIG');
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Returns instance of the redis connection
|
|
32
|
-
*/
|
|
33
|
-
getRedisConnection() {
|
|
34
|
-
return this.redis.connection(this.config.redisConnection);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Returns file contents. A new file will be created if it's
|
|
38
|
-
* missing.
|
|
39
|
-
*/
|
|
40
|
-
async read(sessionId) {
|
|
41
|
-
const contents = await this.getRedisConnection().get(sessionId);
|
|
42
|
-
if (!contents) {
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
const verifiedContents = new helpers_1.MessageBuilder().verify(contents, sessionId);
|
|
46
|
-
if (typeof verifiedContents !== 'object') {
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
return verifiedContents;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Write session values to a file
|
|
53
|
-
*/
|
|
54
|
-
async write(sessionId, values) {
|
|
55
|
-
if (typeof values !== 'object') {
|
|
56
|
-
throw new Error('Session file driver expects an object of values');
|
|
57
|
-
}
|
|
58
|
-
await this.getRedisConnection().setex(sessionId, this.ttl, new helpers_1.MessageBuilder().build(values, undefined, sessionId));
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Cleanup session file by removing it
|
|
62
|
-
*/
|
|
63
|
-
async destroy(sessionId) {
|
|
64
|
-
await this.getRedisConnection().del(sessionId);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Updates the value expiry
|
|
68
|
-
*/
|
|
69
|
-
async touch(sessionId) {
|
|
70
|
-
await this.getRedisConnection().expire(sessionId, this.ttl);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
exports.RedisDriver = RedisDriver;
|
|
@@ -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;
|