diff options
author | tomsmeding <tom.smeding@gmail.com> | 2016-09-13 11:43:06 +0200 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2016-09-13 11:44:11 +0200 |
commit | f00ba92ed2cc1a9c24ad783e83525d1b5a85b857 (patch) | |
tree | 4d1c00a47c7f3842bcf3dece83d3c00ed3ae459f /modules/$common.js |
Initial
Diffstat (limited to 'modules/$common.js')
-rw-r--r-- | modules/$common.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/modules/$common.js b/modules/$common.js new file mode 100644 index 0000000..fa29c50 --- /dev/null +++ b/modules/$common.js @@ -0,0 +1,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,'&').replace(/</g,'<').replace(/>/g,'>'); + }, + "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); + }; + }, +}; |