-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (24 loc) · 913 Bytes
/
index.js
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
const regeneratorRuntime = require("regenerator-runtime");
var basicAuth = require('basic-auth');
module.exports = function (options = { username: 'username', password: 'password' }) {
return async function (req, res, next) {
const { username, password } = options;
let { authenticator } = options;
var credentials = basicAuth(req);
let authenticated;
async function basicAuthenticator(name, pass, username, password) {
return username === name && password === pass;
}
if (!authenticator) authenticator = basicAuthenticator;
try {
authenticated = await authenticator(credentials.name, credentials.pass, username, password);
} catch (err) {
console.error('Could not authenticate:', err);
}
if (!authenticated) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.sendStatus(401);
}
next();
};
};