1
0
Fork 0
mirror of https://github.com/kou029w/http-echo.git synced 2025-01-18 08:05:06 +00:00
http-echo/index.js

37 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2020-03-24 00:52:27 +09:00
const express = require("express");
2020-05-16 20:08:33 +09:00
const cookieParser = require("cookie-parser");
const morgan = require("morgan");
2020-03-24 18:21:28 +09:00
const passport = require("passport");
2020-05-16 20:08:33 +09:00
2020-03-24 18:21:28 +09:00
const { BasicStrategy } = require("passport-http");
const { timingSafeEqual } = require("crypto");
const { HTTP_USERNAME, HTTP_PASSWORD } = process.env;
passport.use(
2020-05-16 20:08:33 +09:00
new BasicStrategy(function (username, password, done) {
2020-03-24 18:21:28 +09:00
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();
2020-05-16 20:08:33 +09:00
app.use(cookieParser());
app.use(morgan("combined"));
2020-03-24 00:52:27 +09:00
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-05-16 20:44:25 +09:00
app.all("/*", require("./api/echo.js"));
2020-03-24 00:52:27 +09:00
2020-05-16 20:44:25 +09:00
if (require.main === module) app.listen(process.env.PORT || 8080);
module.exports = app;