forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
297 lines (271 loc) · 7.02 KB
/
index.js
File metadata and controls
297 lines (271 loc) · 7.02 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
'use strict'
/**
* adonis-framework
* Copyright(c) 2015-2016 Harminder Virk
* MIT Licensed
*/
/**
* @typedef {SessionDriver}
* @type {Class}
*/
const _ = require('lodash')
const uuid = require('node-uuid')
const Store = require('./Store')
const CookieManager = require('./CookieManager')
const CE = require('../Exceptions')
const util = require('../../lib/util')
let sessionManagerDriver = null
let sessionManagerConfig = {}
/**
* Session class to read/write values to the session
* store, using one of the available drivers.
*
* @class
*/
class Session {
/**
* @constructor
*/
constructor (request, response) {
this.request = request
this.response = response
this._instantiate()
this._setDriverRequest()
}
/**
* Returns the active driver instance.
*
* @return {SessionDriver}
*/
static get driver () {
return sessionManagerDriver
}
/**
* Set session driver instance as a static property.
* This needs to be done before initiating the
* class so that session instance has a
* valid driver.
*
* @param {SessionDriver} driver
*/
static set driver (driver) {
sessionManagerDriver = driver
}
/**
* Returns config provider in use.
*
* @return {Object}
*/
static get config () {
return sessionManagerConfig
}
/**
* Sets config driver to use for reading
* session configuration.
*
* @param {Object} config
*/
static set config (config) {
sessionManagerConfig = config
}
/**
* Sets current HTTP request and response on the active
* driver. Some drivers like Cookie driver needs current
* request refrence to read/write cookies. It can be
* left unimplemented for other driver.s
*
* @private
*/
_setDriverRequest () {
if (this.constructor.driver.setRequest) {
this.constructor.driver.setRequest(this.request, this.response)
}
}
/**
* Instantiates the class properly by setting some
* options on the instance
*
* @private
*/
_instantiate () {
this.cookieManager = new CookieManager(this.constructor.config)
this.sessionCookieName = this.constructor.config.get('session.cookie', 'adonis-session')
this.sessionExpiryMs = this.constructor.config.get('session.age', 120) * 60 * 1000
this.sessionId = null
this.sessionPayload = null
}
/**
* Returns session id by reading it from the cookies. It
* session id is not set, it will create a new uid to
* be used as a session id.
*
* @return {String}
*/
getSessionId () {
if (this.sessionId) {
return this.sessionId
}
const sessionId = this.cookieManager.read(this.request, this.sessionCookieName)
this.sessionId = (!sessionId || typeof (sessionId) !== 'string') ? uuid.v1() : sessionId
return this.sessionId
}
/**
* Writes session id on the request as a cookie. It will
* be encrypted if appKey is defined inside the config
* file.
*
* @param {String} sessionId
*/
setSessionId (sessionId) {
this.sessionId = sessionId
this.cookieManager.set(this.request, this.response, this.sessionCookieName, sessionId)
}
/**
* Returns values for a given session id. Values returned
* from the driver are unpacked to be used to mutations.
*
* @param {String} sessionId
*
* @return {Object}
*
* @private
*/
* _getSessionValues (sessionId) {
if (this.sessionPayload) {
return this.sessionPayload
}
const sessionValues = yield this.constructor.driver.read(sessionId)
this.sessionPayload = Store.unPackValues(sessionValues)
return this.sessionPayload
}
/**
* Writes values to the session driver by packing them
* into a safe object.
*
* @method _setSessionValues
*
* @param {String} sessionId
* @see Session.put
*
* @return {Boolean}
*
* @private
*/
* _setSessionValues (sessionId, key, value) {
const sessionValues = yield this._getSessionValues(sessionId)
if (_.isObject(key)) {
_.assign(sessionValues, key)
} else {
_.set(sessionValues, key, value)
}
this.sessionPayload = sessionValues
return yield this.constructor.driver.write(sessionId, Store.packValues(this.sessionPayload))
}
/**
* Returns a deep clone of session values.
*
* @return {Object}
*
* @example
* yield session.all()
*/
* all () {
const values = yield this._getSessionValues(this.getSessionId())
return _.cloneDeep(values)
}
/**
* Saves key/value pair inside the session store.
*
* @param {Mixed} key - Key can be a normal key/value pair key or
* it can be a self contained object
* @param {Mixed} [value] - Value to save next to key. Must not be passed
* when key itself is an object
*
* @return {Boolean}
*
* @example
* yield session.put('name', 'doe')
* yield session.put({name: 'doe'})
*
* @throws {InvalidArgumentException} If parameters are not defined as intended
*/
* put (key, value) {
if (key && typeof (value) === 'undefined' && !_.isObject(key)) {
throw CE.InvalidArgumentException.invalidParameter('Session.put expects a key/value pair or an object of keys and values')
}
const sessionId = this.getSessionId()
this.setSessionId(sessionId)
return yield this._setSessionValues(sessionId, key, value)
}
/**
* @see this.put
* @alias this.put
*/
set (key, value) {
return this.put(key, value)
}
/**
* Removes value set next to a key from the session store.
*
* @param {String} key
*
* @return {Boolean}
*
* @example
* yield session.forget('name')
*/
* forget (key) {
return yield this.put(key, null)
}
/**
* Get value for a given key from the session store.
*
* @param {String} key
* @param {Mixed} defaultValue - default value when actual value is
* undefined for null
* @return {Mixed}
*
* @example
* yield session.get('name')
* yield session.get('name', 'defaultName')
*/
* get (key, defaultValue) {
const sessionValues = yield this._getSessionValues(this.getSessionId())
defaultValue = util.existy(defaultValue) ? defaultValue : null
const value = _.get(sessionValues, key)
return util.existy(value) ? value : defaultValue
}
/**
* Combination of get and forget under single method
*
* @see this.get
*
* @return {Mixed}
*
* @example
* yield session.pull('name')
* yield session.pull('name', 'defaultValue')
*
* @public
*/
* pull (key, defaultValue) {
const value = yield this.get(key, defaultValue)
yield this.forget(key)
return value
}
/**
* Flush the user session by dropping the cookie and notifying
* the driver to destroy values.
*
* @method flush
*
* @return {Boolean}
*/
* flush () {
yield this.constructor.driver.destroy(this.getSessionId())
this.sessionPayload = null
this.sessionId = null
return this.cookieManager.remove(this.request, this.response, this.sessionCookieName)
}
}
module.exports = Session