-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcookieSession.js
73 lines (66 loc) · 2.08 KB
/
cookieSession.js
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
const Jar = require('@ostro/cookie/jar')
const session = require('./session')
const signature = require('cookie-signature');
const uid = require('uid-safe').sync
const tokens = require('csrf')({
saltLength: 30
})
const kConfig = Symbol('config')
const kStore = Symbol('store')
const kGetSessionId = Symbol('getSessionId')
const kCookie = Symbol('cookie')
const Cookie = require('@ostro/cookie/cookie')
class CookieSession {
constructor($app, $config, $store) {
this[kStore] = $store;
this[kConfig] = $config;
this[kCookie] = new Jar($app, $config);
}
start(request, response, next) {
let headerCookies = request.headers.cookie
let cookies = null
if (request.cookie instanceof Cookie) {
cookies = request.cookie.all()
} else {
cookies = this[kCookie].getCookies(headerCookies)
}
if (cookies) {
cookies = this[kCookie].getSignedCookies(cookies, this[kConfig]['key'])
}
let sid = this[kGetSessionId](cookies[this[kConfig]['cookie']])
this[kStore].read(sid, (err, data) => {
Object.defineProperty(request, 'session', {
value: new session(this, data, sid, response),
enumerable: true
})
next()
})
}
[kGetSessionId](id) {
if (!(typeof id === 'string' && /^[A-Za-z0-9-_]/.test(id))) {
id = uid(30);
}
return id;
}
generateSidToCookie(response, val = uid(30)) {
response.append('Set-Cookie', this[kCookie].createCookies(this[kConfig]['cookie'], String(val), {
signed: true
}, response.req.secure()));
}
structureobject() {
return {
__token: tokens.create(this[kConfig]['key']),
__flash: {
"__old": [],
"__new": []
}
}
}
write(sessionId, __attributes, cb) {
this[kStore].write(sessionId, __attributes, cb)
}
destroy(sessionId, cb) {
this[kStore].destroy(sessionId, cb)
}
}
module.exports = CookieSession