Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Nov 9, 2024
1 parent fac08f9 commit 4d47d57
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
18 changes: 9 additions & 9 deletions server/lib/totpUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ async function isTotpTokenValid(userId, token) {
}

// Function to check if the TOTP token is valid by specific TOTP ID
async function isTotpTokenValidById(userId, token, totpId) {
async function isTotpTokenValidById(userId, token, totp_id) {
try {
const userTotp = await I.prisma.ow_users_totp.findUnique({
where: { user_id: Number(userId), id: Number(totpId) },
where: { user_id: Number(userId), id: Number(totp_id) },
select: {
totp_secret: true,
totp_algorithm: true,
Expand Down Expand Up @@ -126,7 +126,7 @@ async function createTotpTokenForUser(userId) {
message: "验证器已创建",
secret: result.totp_secret,
otpauth_url: otpauthUrl,
totpid: result.id,
totp_id: result.id,
};
} catch (error) {
return {
Expand All @@ -146,23 +146,23 @@ function generateTotpUrlForUser(userId, secret) {
}

// Function to enable (activate) a TOTP token after validating the token
async function enableTotpToken(userId, totpId, token) {
async function enableTotpToken(userId, totp_id, token) {
try {
const isValid = await isTotpTokenValidById(userId, token, totpId);
const isValid = await isTotpTokenValidById(userId, token, totp_id);

if (!isValid.valid) {
return { status: "0", message: "无法激活令牌:" + isValid.message };
}
const needupdatedTotp = await I.prisma.ow_users_totp.findUnique({
where: {
id: Number(totpId),
id: Number(totp_id),
user_id: Number(userId),
},
});
if (needupdatedTotp.status === "unverified" && isValid.valid) {
const updatedTotp = await I.prisma.ow_users_totp.update({
where: {
id: Number(totpId),
id: Number(totp_id),
user_id: Number(userId),
},
data: {
Expand All @@ -184,10 +184,10 @@ async function enableTotpToken(userId, totpId, token) {
}

// Function to remove a TOTP token
async function removeTotpToken(userId, totpId) {
async function removeTotpToken(userId, totp_id) {
try {
const result = await I.prisma.ow_users_totp.delete({
where: { id: Number(totpId), user_id: Number(userId) },
where: { id: Number(totp_id), user_id: Number(userId) },
select: { id: true, user_id: true, name: true, type: true, status: true },
});

Expand Down
26 changes: 13 additions & 13 deletions server/router_account.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,16 @@ router.get("/totp/list", needlogin, async (req, res) => {
}
});
router.post("/totp/rename", needlogin, async (req, res) => {
const { totpId, name } = req.body;
if (!totpId || !name) {
const { totp_id, name } = req.body;
if (!totp_id || !name) {
return res.status(400).json({
status: "error",
message: "TOTP ID 和名称是必需的",
});
}
try {
var renamedTotp = await I.prisma.ow_users_totp.update({
where: { id: Number(totpId) },
where: { id: Number(totp_id) },
data: { name: name },
select: {
id: true,
Expand All @@ -325,16 +325,16 @@ router.post("/totp/rename", needlogin, async (req, res) => {
}
});
router.post("/totp/check", async (req, res) => {
const { totptoken, userId } = req.body;
if (!totptoken || !userId) {
const { totp_token, userId } = req.body;
if (!totp_token || !userId) {
return res.status(400).json({
status: "error",
message: "验证器令牌和用户 ID 是必需的",
});
}

try {
const isValid = await isTotpTokenValid(userId, totptoken);
const isValid = await isTotpTokenValid(userId, totp_token);
return res.json({
status: "success",
message: "令牌验证结果",
Expand All @@ -350,15 +350,15 @@ router.post("/totp/check", async (req, res) => {
}
});
router.post("/totp/delete", needlogin, async (req, res) => {
const { totpId } = req.body;
if (!totpId) {
const { totp_id } = req.body;
if (!totp_id) {
return res.status(400).json({
status: "error",
message: "验证器 ID 是必需的",
});
}
try {
const deletedTotp = await removeTotpToken(res.locals.userid, totpId);
const deletedTotp = await removeTotpToken(res.locals.userid, totp_id);
return res.json({
status: "success",
message: "验证器已删除",
Expand Down Expand Up @@ -392,9 +392,9 @@ router.post("/totp/generate", needlogin, async (req, res) => {
});

router.post("/totp/activate", needlogin, async (req, res) => {
const { totpId, totptoken } = req.body;
const { totp_id, totp_token } = req.body;

if (!totpId || !totptoken) {
if (!totp_id || !totp_token) {
return res.status(400).json({
status: "error",
message: "验证器ID和令牌是必需的",
Expand All @@ -404,8 +404,8 @@ router.post("/totp/activate", needlogin, async (req, res) => {
try {
const activatedTotp = await enableTotpToken(
res.locals.userid,
totpId,
totptoken
totp_id,
totp_token
);
return res.json({
status: "success",
Expand Down

0 comments on commit 4d47d57

Please sign in to comment.