net-helper/modules/example_custom_auth.js

44 lines
1.8 KiB
JavaScript

const express = require("express");
const NetProxierModule = require("./");
const HTTPReverse = require("../components/http-reverse");
module.exports = class CustomAuthPage extends NetProxierModule {
constructor () {
super();
const cauth = new this.statics.Operator("cauth", (argv) => {
return argv.length === 2;
}, (argv, self, syntaxTree) => {
if (![HTTPReverse].includes(syntaxTree.Type)) {
throw new SyntaxError("Unsupported type of server");
}
const [ login, password ] = argv;
const router = express.Router();
router.get("/auth/:redirectTo", (req, res, next) => {
if (req.cookies.auth === login + ":" + password)
return next();
const endless = req.query.login + ":" + req.query.password;
const redirectTo = Buffer.from(req.params.redirectTo, "hex").toString("utf8");
//console.log("redirect to:", redirectTo);
if (endless === login + ":" + password) {
res.cookie("auth", endless);
}
res.redirect(redirectTo);
});
router.use((req, res, next) => {
//console.log('Cookies: ', req.cookies);
if (req.cookies.auth === login + ":" + password)
return next();
res.send('<form action=\"/auth/'+ Buffer.from(req.originalUrl).toString("hex") +'\"><h4>Needs auth</h4><hr/><p>Логин: <input type="text" size="40" name="login"></p><p>Пароль: <input type="password" size="40" name="password"></p><p><input type="submit"></p></form>');
});
syntaxTree.routelist.push(router);
});
this.operators.push(cauth);
}
};