我用 SpeakEasy 做了一个 TOTP(基于时间的唯一密码),一切正常,我设置了一个条件(在代码末尾)来验证令牌,然后将其重定向到隐藏页面,但它不起作用,并且我不知道为什么。感谢您的帮助
意见/验证.ejs
<form method='post' action='/hidden'>
<input type="text" name="TokenClient" placeholder="your password">
<button type="submit">submit</button>
</form>
<p><strong>remain :</strong><%= remaining %></p>
<p>
<strong>token client :</strong><%= user %></br>
</p>
<strong>validation :</strong><%= valid %></br>
意见/hidden.ejs
<h1>Hidden Page</h1>
应用程序.js
const Express = require("express");
const BodyParser = require("body-parser");
const Speakeasy = require("speakeasy");
const app = Express();
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));
// EJS /////////////////////////
app.set("views", "./views");
app.set("view engine", "ejs");
// Menu = OK ///////////////////
app.get("/", (req, res) => {
res.render("index", {});
});
// Secret Key = OK /////////////
app.get("/totp-secret", (req, res) => {
//let secret = Speakeasy.generateSecret({ length: 20 }).base32;
let secret = "azerty";
res.render("secret", { secret });
});
// TOKEN = OK //////////////////
app.get("/totp-generate-token", (req, res) => {
let token = Speakeasy.totp({
secret: req.body.secret,
encoding: "base32",
});
let remaining = 30 - Math.floor((new Date().getTime() / 1000.0) % 30);
res.render("token", { token, remaining });
});
//Validation/////////////////////
app.get("/totp-validate", (req, res) => {
let remaining = 30 - Math.floor((new Date().getTime() / 1000.0) % 30);
///////////////////////////////
let user = req.body.TokenClient;
///////////////////////////////
let valid = Speakeasy.totp.verify({
secret: req.body.secret,
encoding: "base32",
token: req.body.TokenClient, // Token Client
window: 0,
});
res.render("validate", { remaining, user, valid });
});
app.post("/totp-validate", (req, res) => {
let remaining = 30 - Math.floor((new Date().getTime() / 1000.0) % 30);
let user = req.body.TokenClient;
let valid = Speakeasy.totp.verify({
secret: req.body.secret,
encoding: "base32",
token: req.body.TokenClient, // Token Client
window: 0,
});
// Probleme here !!! ////////////////////////////
if (valid) {
res.redirect("/hidden");
console.log("valide");
} else {
res.redirect("/totp-validate");
}
res.render("validate", { remaining, user, valid });
});
app.listen(3000, () => {
console.log("Listening at :3000...");
});
变量“有效”是一个布尔值