2020-03-24 00:52:27 +09:00
|
|
|
const express = require("express");
|
2020-03-24 18:21:28 +09:00
|
|
|
const passport = require("passport");
|
|
|
|
const { BasicStrategy } = require("passport-http");
|
|
|
|
const { timingSafeEqual } = require("crypto");
|
|
|
|
const { HTTP_USERNAME, HTTP_PASSWORD } = process.env;
|
|
|
|
|
|
|
|
passport.use(
|
|
|
|
new BasicStrategy(function(username, password, done) {
|
|
|
|
return done(
|
|
|
|
null,
|
|
|
|
HTTP_USERNAME != null &&
|
|
|
|
username.length === HTTP_USERNAME.length &&
|
|
|
|
timingSafeEqual(Buffer.from(username), Buffer.from(HTTP_USERNAME)) &&
|
|
|
|
password.length === HTTP_PASSWORD.length &&
|
|
|
|
timingSafeEqual(Buffer.from(password), Buffer.from(HTTP_PASSWORD))
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-03-24 00:52:27 +09:00
|
|
|
const app = express();
|
|
|
|
|
|
|
|
app.use(express.json());
|
|
|
|
app.use(express.urlencoded({ extended: false }));
|
|
|
|
app.use(express.raw({ type: "*/*" }));
|
2020-03-24 14:12:03 +09:00
|
|
|
app.set("json spaces", 2);
|
2020-03-24 18:21:28 +09:00
|
|
|
app.all("/basic-auth", passport.authenticate("basic", { session: false }));
|
2020-03-24 00:52:27 +09:00
|
|
|
app.all("/*", function(req, res) {
|
|
|
|
res.json({
|
|
|
|
headers: req.headers,
|
|
|
|
body: req.body.length > 0 ? req.body.toString() : null,
|
|
|
|
form: req.is("urlencoded") ? req.body : null,
|
|
|
|
json: req.is("json") ? req.body : null,
|
|
|
|
method: req.method,
|
|
|
|
target: req.url,
|
|
|
|
host: req.headers.host
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(8080);
|