-
Notifications
You must be signed in to change notification settings - Fork 3
/
express-session-hapi.js
144 lines (109 loc) · 3.59 KB
/
express-session-hapi.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
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
'use strict';
var Boom = require('boom');
var Hoek = require('hoek');
var Joi = require('joi');
var redis = require('redis');
var signature = require('cookie-signature');
var internals = {};
exports.register = function (server, options, next) {
server.auth.scheme('cookie', internals.implementation);
next();
};
exports.register.attributes = {
pkg: require('./package.json')
};
internals.schema = Joi.object({
clearInvalid: Joi.boolean().default(false),
cookieName: Joi.string(),
cookieValuePrefix: Joi.string().default('s:'),
redirectTo: Joi.string().allow(false),
redis: Joi.object().keys({
host: Joi.string(),
port: Joi.number().default(6379)
}),
secret: Joi.string(),
sessionIDPrefix: Joi.string().default('sess:'),
userProp: Joi.string().default('user'),
}).required();
internals.implementation = function (server, options) {
var results = Joi.validate(options, internals.schema);
Hoek.assert(!results.error, results.error);
var settings = results.value;
if (typeof settings.appendNext === 'boolean') {
settings.appendNext = (settings.appendNext ? 'next' : '');
}
var redisClient = redis.createClient(settings.redis.port, settings.redis.host);
function decodeCookieValue(val) {
val = decodeURIComponent(val).trim();
// quoted values
if ('"' == val[0]) {
val = val.slice(1, -1);
}
return val;
}
var scheme = {
authenticate: function (request, reply) {
var validate = function () {
var rawCookieValue = request.state[settings.cookieName];
if (!rawCookieValue) {
return unauthenticated(Boom.unauthorized(null, 'cookie'));
}
rawCookieValue = decodeCookieValue(rawCookieValue);
var sessionID;
if (rawCookieValue.substr(0, 2) === settings.cookieValuePrefix) {
sessionID = signature.unsign(rawCookieValue.slice(2), settings.secret);
if (sessionID === false) {
// cookie signature invalid
sessionID = undefined;
if (settings.clearInvalid) {
reply.unstate(settings.cookieName);
}
return unauthenticated(Boom.unauthorized('Invalid cookie'));
}
} else {
return unauthenticated(Boom.unauthorized(null, 'cookie'));
}
redisClient.get(settings.sessionIDPrefix + sessionID, function (err, data) {
if (err) {
return unauthenticated(Boom.unauthorized('Server error'));
}
if (!data) {
return unauthenticated(Boom.unauthorized(null, 'cookie'));
}
data = data.toString();
try {
data = JSON.parse(data);
} catch (er) {
return unauthenticated(Boom.unauthorized('Invalid session data'));
}
if (!data[settings.userProp]) {
return unauthenticated(Boom.unauthorized(null, 'cookie'));
}
return reply.continue({
artifacts: data,
credentials: data,
});
});
};
var unauthenticated = function (err, result) {
var redirectTo = settings.redirectTo;
if (!redirectTo) {
return reply(err, null, result);
}
var uri = redirectTo;
if (settings.appendNext) {
if (uri.indexOf('?') !== -1) {
uri += '&';
}
else {
uri += '?';
}
uri += settings.appendNext + '=' + encodeURIComponent(request.url.path);
}
return reply('You are being redirected...', null, result).redirect(uri);
};
validate();
}
};
return scheme;
};