52 lines
2.0 KiB
JavaScript
52 lines
2.0 KiB
JavaScript
const express = require("express");
|
|
const { NetHelperModule } = global.moduleApi;
|
|
|
|
const HTTPServer = require("../components/_http");
|
|
|
|
module.exports = class CustomAuthPage extends NetHelperModule {
|
|
constructor () {
|
|
super();
|
|
this.router = express.Router();
|
|
|
|
this.cauth = new this.statics.Operator("cauth", (argv) => {
|
|
return argv.length === 2;
|
|
}, (argv, self, syntaxTree) => {
|
|
if (!(syntaxTree.Type.prototype instanceof HTTPServer)) {
|
|
throw new SyntaxError("Unsupported type of server");
|
|
}
|
|
const [ login, password ] = argv;
|
|
const router = this.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>');
|
|
});
|
|
});
|
|
|
|
this.operators.push(this.cauth);
|
|
|
|
this.emit("runned", () => {
|
|
this.serverType.app.use(router);
|
|
});
|
|
}
|
|
|
|
bind (type) {
|
|
super.bind(type);
|
|
this.serverType.routelist.push(this.router);
|
|
}
|
|
};
|