2019-03-24 21:15:49 +07:00
|
|
|
/*
|
|
|
|
* This is a script to fake the Duo API for push notifications.
|
2021-12-01 10:32:58 +07:00
|
|
|
*
|
|
|
|
* For Auth API access is allowed by default but one can change the
|
|
|
|
* behavior at runtime by POSTing to /allow or /deny. Then the /auth/v2/auth
|
|
|
|
* endpoint will act accordingly.
|
|
|
|
*
|
|
|
|
* For PreAuth API device selection is bypassed by default but one can
|
|
|
|
* change the behavior at runtime by POSTing to /preauth using the desired
|
|
|
|
* result parameters (and devices). Then the /auth/v2/preauth endpoint
|
|
|
|
* will act accordingly.
|
2019-03-24 21:15:49 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
const express = require("express");
|
|
|
|
const app = express();
|
|
|
|
const port = 3000;
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
app.use(express.json());
|
|
|
|
app.set("trust proxy", true);
|
2019-03-24 21:15:49 +07:00
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
// Auth API
|
|
|
|
let permission = "allow";
|
2019-03-24 21:15:49 +07:00
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
app.post("/allow", (req, res) => {
|
|
|
|
permission = "allow";
|
|
|
|
console.log("auth set allowed!");
|
|
|
|
res.send("ALLOWED");
|
2019-03-24 21:15:49 +07:00
|
|
|
});
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
app.post("/deny", (req, res) => {
|
|
|
|
permission = "deny";
|
|
|
|
console.log("auth set denied!");
|
|
|
|
res.send("DENIED");
|
2019-03-24 21:15:49 +07:00
|
|
|
});
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
app.post("/auth/v2/auth", (req, res) => {
|
2019-11-30 23:49:52 +07:00
|
|
|
setTimeout(() => {
|
|
|
|
let response;
|
2021-12-01 10:32:58 +07:00
|
|
|
if (permission == "allow") {
|
2019-11-30 23:49:52 +07:00
|
|
|
response = {
|
|
|
|
response: {
|
2021-12-01 10:32:58 +07:00
|
|
|
result: "allow",
|
|
|
|
status: "allow",
|
|
|
|
status_msg: "The user allowed access.",
|
2019-11-30 23:49:52 +07:00
|
|
|
},
|
2021-12-01 10:32:58 +07:00
|
|
|
stat: "OK",
|
2019-11-30 23:49:52 +07:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
response = {
|
|
|
|
response: {
|
2021-12-01 10:32:58 +07:00
|
|
|
result: "deny",
|
|
|
|
status: "deny",
|
|
|
|
status_msg: "The user denied access.",
|
2019-11-30 23:49:52 +07:00
|
|
|
},
|
2021-12-01 10:32:58 +07:00
|
|
|
stat: "OK",
|
2019-11-30 23:49:52 +07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
res.json(response);
|
2021-12-01 10:32:58 +07:00
|
|
|
console.log("Auth API responded with %s", permission);
|
|
|
|
}, 2000);
|
|
|
|
});
|
|
|
|
|
|
|
|
// PreAuth API
|
|
|
|
let preauth = {
|
|
|
|
result: "allow",
|
|
|
|
status_msg: "Allowing unknown user",
|
|
|
|
};
|
|
|
|
|
|
|
|
app.post("/preauth", (req, res) => {
|
|
|
|
preauth = req.body;
|
|
|
|
console.log("set result to: %s", preauth);
|
|
|
|
res.json(preauth);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post("/auth/v2/preauth", (req, res) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
let response;
|
|
|
|
response = {
|
|
|
|
response: preauth,
|
|
|
|
stat: "OK",
|
|
|
|
};
|
|
|
|
|
|
|
|
res.json(response);
|
|
|
|
console.log("PreAuth API responded with %s", preauth);
|
2019-11-30 23:49:52 +07:00
|
|
|
}, 2000);
|
2019-03-24 21:15:49 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(port, () => console.log(`Duo API listening on port ${port}!`));
|
|
|
|
|
|
|
|
// The signals we want to handle
|
|
|
|
// NOTE: although it is tempting, the SIGKILL signal (9) cannot be intercepted and handled
|
|
|
|
var signals = {
|
2021-12-01 10:32:58 +07:00
|
|
|
SIGHUP: 1,
|
|
|
|
SIGINT: 2,
|
|
|
|
SIGTERM: 15,
|
2019-03-24 21:15:49 +07:00
|
|
|
};
|
|
|
|
// Create a listener for each of the signals that we want to handle
|
|
|
|
Object.keys(signals).forEach((signal) => {
|
|
|
|
process.on(signal, () => {
|
|
|
|
console.log(`process received a ${signal} signal`);
|
|
|
|
process.exit(128 + signals[signal]);
|
|
|
|
});
|
2021-12-01 10:32:58 +07:00
|
|
|
});
|