webaccess: Simplify Express and express-session setup
parent
275e5c31c8
commit
b68969fbac
|
@ -49,8 +49,8 @@ exports.expressCreateServer = function (hook_name, args, cb) {
|
||||||
// check whether the user has authenticated, then any random person on the Internet can read,
|
// check whether the user has authenticated, then any random person on the Internet can read,
|
||||||
// modify, or create any pad (unless the pad is password protected or an HTTP API session is
|
// modify, or create any pad (unless the pad is password protected or an HTTP API session is
|
||||||
// required).
|
// required).
|
||||||
const cookieParserFn = util.promisify(cookieParser(webaccess.secret, {}));
|
const cookieParserFn = util.promisify(cookieParser(settings.sessionKey, {}));
|
||||||
const getSession = util.promisify(args.app.sessionStore.get).bind(args.app.sessionStore);
|
const getSession = util.promisify(webaccess.sessionStore.get).bind(webaccess.sessionStore);
|
||||||
io.use(async (socket, next) => {
|
io.use(async (socket, next) => {
|
||||||
const req = socket.request;
|
const req = socket.request;
|
||||||
if (!req.headers.cookie) {
|
if (!req.headers.cookie) {
|
||||||
|
|
|
@ -199,17 +199,12 @@ exports.checkAccess = (req, res, next) => {
|
||||||
step1PreAuthorize();
|
step1PreAuthorize();
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.secret = null;
|
|
||||||
|
|
||||||
exports.expressConfigure = (hook_name, args, cb) => {
|
exports.expressConfigure = (hook_name, args, cb) => {
|
||||||
// Measure response time
|
// Measure response time
|
||||||
args.app.use((req, res, next) => {
|
args.app.use((req, res, next) => {
|
||||||
const stopWatch = stats.timer('httpRequests').start();
|
const stopWatch = stats.timer('httpRequests').start();
|
||||||
const sendFn = res.send;
|
const sendFn = res.send.bind(res);
|
||||||
res.send = function() { // function, not arrow, due to use of 'arguments'
|
res.send = (...args) => { stopWatch.end(); sendFn(...args); };
|
||||||
stopWatch.end();
|
|
||||||
sendFn.apply(res, arguments);
|
|
||||||
};
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -224,22 +219,17 @@ exports.expressConfigure = (hook_name, args, cb) => {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Do not let express create the session, so that we can retain a
|
// Do not let express create the session, so that we can retain a reference to it for socket.io to
|
||||||
* reference to it for socket.io to use. Also, set the key (cookie
|
// use.
|
||||||
* name) to a javascript identifier compatible string. Makes code
|
exports.sessionStore = new ueberStore();
|
||||||
* handling it cleaner :) */
|
|
||||||
|
|
||||||
if (!exports.sessionStore) {
|
|
||||||
exports.sessionStore = new ueberStore();
|
|
||||||
exports.secret = settings.sessionKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
args.app.sessionStore = exports.sessionStore;
|
|
||||||
args.app.use(sessionModule({
|
args.app.use(sessionModule({
|
||||||
secret: exports.secret,
|
secret: settings.sessionKey,
|
||||||
store: args.app.sessionStore,
|
store: exports.sessionStore,
|
||||||
resave: false,
|
resave: false,
|
||||||
saveUninitialized: true,
|
saveUninitialized: true,
|
||||||
|
// Set the cookie name to a javascript identifier compatible string. Makes code handling it
|
||||||
|
// cleaner :)
|
||||||
name: 'express_sid',
|
name: 'express_sid',
|
||||||
proxy: true,
|
proxy: true,
|
||||||
cookie: {
|
cookie: {
|
||||||
|
|
Loading…
Reference in New Issue