Skip to content

Instantly share code, notes, and snippets.

@undoZen
Created January 27, 2014 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save undoZen/8644827 to your computer and use it in GitHub Desktop.
Save undoZen/8644827 to your computer and use it in GitHub Desktop.
'use strict';
exports.prependHost = prependHost;
function prependHost(url, host) {
host = host || 'backend';
if ('/' != url[0]) url = '/' + url;
return config.url[host] + url;
}
exports.getSession = getSession;
function getSession(req, res, next) {
req.uest(prependHost('/session'), function (err, r, b) {
req.user = false;
try {
req.user = JSON.parse(b || '{}').user || false;
} catch (e) {}
next();
});
}
exports.loginRequired = loginRequired;
function loginRequired(req, res, next) {
if ('undefined' == typeof req.user) {
return getSession(req, res, function () {
loginRequired(req, res, next);
});
}
if (!req.user) res.redirect('/login?redirect=' + encodeURIComponent(req.url));
else next();
}
exports.forwardCookie = forwardCookie;
function forwardCookie(r, res) {
var cookies = r.headers['set-cookie'];
if (cookies) {
cookies.forEach(function (cstr) {
res.setHeader('set-cookie', cstr.replace(/; *Path=\/([^;]*);/i, '; Path=/;'));
});
}
}
exports.verificationRequired = verificationRequired;
function verificationRequired(req, res, next) {
if ('undefined' == typeof req.user) {
return loginRequired(req, res, function () {
verificationRequired(req, res, next);
});
}
if (!req.user.verified) res.redirect('/verification/required');
else next();
}
exports.authorizationRequired = authorizationRequired;
function authorizationRequired(req, res, next) {
if ('undefined' == typeof req.user) {
return loginRequired(req, res, function () {
authorizationRequired(req, res, next);
});
}
if (!~['ADMIN', 'OPERATOR'].indexOf(req.user.type)) res.redirect('/authorizationRequired?url=' + encodeURIComponent(req.url));
else next();
}
var purl = function (req, url) {
return url.replace(/\#{([^\}]+)\}/g, function (all, k) {
return eval('req.' + k);
});
};
exports.fetchData = fetchData;
function fetchData(list) {
if (arguments.length > 1) list = Array.prototype.slice.apply(arguments);
else if (arguments.length == 0) list = [];
else if (!_.isArray(list)) list = [list];
return function (req, res, next) {
req._data = Q.all(list.map(function (url) {
if ('string' == typeof url) {
return req.qGet(purl(req, url));
} else {
return url;
}
}));
next();
};
}
exports.pickPost = pickPost;
function pickPost(url, keys, fn) {
function middleware(req, res, next) {
if (req.body) console.log(_.pick(req.body, keys))
if (!req.body) return app.urlEncodedParser(req, res, middleware.bind(this, req, res, next));
req.uest.post({
uri: prependHost(purl(req, url)),
json: true,
form: 'string' == typeof keys && req.body[keys] ?
req.body[keys] :
_.pick(req.body, keys)
},
fn.bind(GLOBAL, req, res, next));
}
return middleware;
}
exports.pipePost = pipePost;
function pipePost(url, fn) {
return function (req, res, next) {
req.pipe(req.uest.post(prependHost(url), fn.bind(GLOBAL, req, res, next)));
}
}
exports.pipePostPipeRes = pipePostPipeRes;
function pipePostPipeRes(url) {
return function (req, res, next) {
req.pipe(req.uest.post(prependHost(url), allonges.unary(next))).pipe(res);
}
}
exports.auth = auth;
function auth(fn) {
return function (req, res, next) {
fn(req, function (result) {
if (result) next();
else res.redirect('/403?redirect='+encodeURIComponent(req.url));
});
}
}
exports.authOwner = authOwner;
function authOwner(req, cb) {
req.spread(function (entity) {
cb(~['OPERATOR', 'ADMIN'].indexOf(req.user.type) ||
req.user.id == entity.owner.id);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment