Authorization
js
Node.js
// curl -u toto:password http://localhost:8000/
require('http').createServer((req, res) => {
if (req.headers.authorization) {
res.write(req.headers.authorization + "\n");
let userPass = Buffer
.from(req.headers.authorization.match(/Basic\s(.*)/i)[1], 'base64')
.toString('ascii')
.match(/(.*):(.*)/i);
res.write(userPass[0] + "\n");
res.write(userPass[1] + "\n");
res.write(userPass[2] + "\n");
res.end();
} else {
res.setHeader("WWW-Authenticate", "Basic realm=\"Authorization Required\", charset=\"UTF-8\"");
res.writeHead(401, { 'Content-Type': 'application/json' });
res.end('{ msg: "Authorization Required" }');
}
}).listen(8000);
Basic dG90bzpwYXNzd29yZA==
toto:password
toto
password
toto:password
toto
password