Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Nov 17, 2024
1 parent 0b4bb67 commit 6091fa1
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 82 deletions.
46 changes: 45 additions & 1 deletion server/lib/totpUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,58 @@ async function removeTotpToken(userId, totp_id) {

return { status: "success", message: "验证器已删除", totp: result };
} catch (error) {
return { status: "0", message: "无法删除验证器",error:error };
return { status: "0", message: "无法删除验证器", error: error };
}
}

// TOTP validation middleware
async function validateTotpToken(req, res, next) {
try {
// Extract TOTP token from query, body, or headers
const token =
req.query.totp_token ||
req.body.totp_token ||
req.headers["x-totp-token"];
console.log(token);
if (!res.locals.login) {
// 未登录,返回401 Unauthorized状态码
return res.status(401).send({ status: "0", msg: "请先登录以继续操作" });
}
if (!token) {
// If no token is provided, return a failure response
return res.status(400).json({
status: "error",
message: "令牌未提供",
});
}

// Check if the TOTP token is valid
const userId = res.locals.userid; // Assuming the user ID is available in the request (e.g., from authentication middleware)
const isValid = await isTotpTokenValid(userId, token);

if (isValid.valid === false) {
// If the token is invalid, return a failure response
return res.status(400).json({
status: "error",
message: "无法处理请求:" + isValid.message,
});
}

// If valid, move to the next middleware or route handler
next();
} catch (error) {
console.error("Error in TOTP validation middleware:", error);
return res.status(500).json({
status: "error",
message: "Internal server error during TOTP validation.",
});
}
}
module.exports = {
isTotpTokenValid,
isTotpTokenValidById,
createTotpTokenForUser,
enableTotpToken,
removeTotpToken,
validateTotpToken,
};
45 changes: 0 additions & 45 deletions server/lib/validateTotpToken.js

This file was deleted.

14 changes: 5 additions & 9 deletions server/lib/needlogin.js → server/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
const express = require("express");

const app = express();

// 登录检查中间件
app.use((req, res, next) => {
async function needlogin(req, res, next) {
if (!res.locals.login) {
// 未登录,返回401 Unauthorized状态码
return res.status(401).send({ status: "0", msg: "请先登录以继续操作" });
}
next(); // 已登录,继续处理请求
});

module.exports = app;
}
module.exports = {
needlogin,
};
4 changes: 2 additions & 2 deletions server/router_account.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ const {
registrationTemplate,
passwordResetTemplate,
} = require("./services/emailTemplates");
const needlogin = require("./lib/needlogin.js");
const { needlogin } = require("./middleware/auth.js");

const {
isTotpTokenValid,
createTotpTokenForUser,
enableTotpToken,
removeTotpToken,
validateTotpToken
} = require("./lib/totpUtils.js");
const validateTotpToken = require("./lib/validateTotpToken.js"); // Import the middleware
router.all("*", function (req, res, next) {
next();
});
Expand Down
2 changes: 1 addition & 1 deletion server/router_projectlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const configManager = require("./configManager");

const express = require("express");
const router = express.Router();
const needlogin = require("./lib/needlogin.js");
const { needlogin } = require("./middleware/auth.js");

const {
userProjectlistAdd,
Expand Down
4 changes: 0 additions & 4 deletions server/router_user.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const configManager = require("./configManager.js");
var express = require("express");
var router = express["Router"]();
var fs = require("fs");
var jwt = require("jsonwebtoken");
var DB = require("./lib/database.js");
var I = require("./lib/global.js");
let cryptojs = require("crypto-js");
const needlogin = require("./lib/needlogin.js");


router.all("*", function (req, res, next) {
Expand Down
35 changes: 15 additions & 20 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,22 @@
<link href="<%= global.config['urls.static'] %>/font1.css" rel="stylesheet" />

<mdui-layout-main>



<mdui-list>
<% const config = JSON.parse(JSON.stringify(global.publicconfig)); %>
<% Object.entries(config).forEach(([key, value]) => { %>
<mdui-list-item
rounded
headline="<%= key %>"
description="<%= typeof value === 'object' ? JSON.stringify(value) : value %>"
></mdui-list-item>
<% }); %>
</mdui-list>

<mdui-button variant="filled" href="<%= global.config['urls.frontend'] %>">立刻加入</mdui-button>
<mdui-button variant="elevated" href="#">了解更多</mdui-button><mdui-list>
<mdui-list-item icon="people" description="qq群号964979747" href="https://qm.qq.com/q/wn3TkPoZ5C" active
rounded>ZeroCat现由厚浪云提供支持</mdui-list-item>
</mdui-list>

</div>
<% const config=JSON.parse(JSON.stringify(global.publicconfig)); %>
<% Object.entries(config).forEach(([key, value])=> { %>
<mdui-list-item rounded headline="<%= key %>"
description="<%= typeof value === 'object' ? JSON.stringify(value) : value %>"></mdui-list-item>
<% }); %>
</mdui-list>
<mdui-button variant="filled" href="<%= global.config['urls.frontend'] %>">立刻加入</mdui-button>
<mdui-button variant="elevated" href="#">了解更多</mdui-button><mdui-list>
<!-- 判断feedback.qqgroup是否存在 -->
<% if (global.publicconfig['feedback.qq.group']) { %>
<mdui-list-item icon="people" description="qq群<%= global.config['feedback.qq.group'] %>" <% if (global.publicconfig['feedback.qq.link']) { %>href="<%= global.config['feedback.qq.link'] %>"<% } %> active
rounded><%= global.config['feedback.qq.description'] %></mdui-list-item>
<% } %>
</mdui-list>
</div>
</mdui-layout-main>
</mdui-layout>
</body>
Expand Down

0 comments on commit 6091fa1

Please sign in to comment.