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

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

module.exports={
	"serverdir":cwd,
	"webfilesdir":cwd+"/web_files",
	"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){
					req.authuser=user.name;
					return next();
				}
			}
			return unauth(res);
		};
	},
};