From d1abe1bd49739b5da49e9499e95521e8382d7ab6 Mon Sep 17 00:00:00 2001 From: Stuart Gathman Date: Sat, 29 Jul 2017 13:04:16 -0400 Subject: Optional Basic authentication --- lib/serve.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib') diff --git a/lib/serve.js b/lib/serve.js index 6a0bb88..75356a9 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -66,6 +66,25 @@ Serve.prototype.go = function () { var self = this this.res.setTimeout(0) + var conf = self.app.config.patchfoo || {} + var authtok = conf.auth || null + if (authtok) { + var auth = this.req.headers['authorization'] + var tok = null + //console.log('Authorization: ',auth) + + if (auth) { + var a = auth.split(' ') + if (a[0] == 'Basic') { + tok = Buffer.from(a[1],'base64').toString('ascii') + } + } + if (tok != authtok) { + self.res.writeHead(401, {'WWW-Authenticate': 'Basic realm="Patchfoo"'}) + self.res.end('Not authorized') + return + } + } if (this.req.method === 'POST' || this.req.method === 'PUT') { if (/^multipart\/form-data/.test(this.req.headers['content-type'])) { -- cgit v1.2.3