summaryrefslogtreecommitdiff
path: root/modules/$common.js
blob: fa29c50a99b66d55f55bce7b4631b158d04dddab (plain)
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
var path=require("path"),
    basicAuth=require("basic-auth");

var cwd=process.cwd();
var globalAccounts=require(cwd+"/globalAccounts.json");

module.exports={
	"rootdir":path.dirname(cwd),
	"serverdir":cwd,
	"simpleHTMLescape":function simpleHTMLescape(str){
		return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
	},
	"authgen":function authgen(accounts){ //omit `accounts` to use the globalAccounts list
		if(!accounts)accounts=globalAccounts;
		return function (req,res,next){
			function unauth(res){
				res.set("WWW-Authenticate","Basic realm=Authorization required");
				return res.sendStatus(401);
			};
			var user=basicAuth(req);
			if(!user||!user.name||!user.pass){
				return unauth(res);
			}
			var i;
			for(i=0;i<accounts.length;i++){
				if(accounts[i][0]==user.name&&accounts[i][1]==user.pass){
					return next();
				}
			}
			return unauth(res);
		};
	},
};