-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
95 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,42 @@ | ||
import logger from "../utils/logger.js"; | ||
import configManager from "../utils/configManager.js"; | ||
|
||
/** | ||
* 需要登录的中间件 | ||
* @param {import("express").Request} req | ||
* @param {import("express").Response} res | ||
* @param {import("express").NextFunction} next | ||
*/ | ||
async function needlogin(req, res, next) { | ||
if (!res.locals.login) { | ||
// 未登录,返回401 Unauthorized状态码 | ||
logger.info(`[needlogin] - ${req.ip} - 未登录,返回401 Unauthorized状态码`); | ||
return res.status(401).send({ status: "0", msg: "请先登录以继续操作" }); | ||
} | ||
next(); // 已登录,继续处理请求 | ||
} | ||
|
||
/** | ||
* 需要管理员权限的中间件 | ||
* @param {import("express").Request} req | ||
* @param {import("express").Response} res | ||
* @param {import("express").NextFunction} next | ||
*/ | ||
async function needadmin(req, res, next) { | ||
if (!res.locals.login) { | ||
// 未登录,返回401 Unauthorized状态码 | ||
logger.info(`[needadmin] - ${req.ip} - 未登录,返回401 Unauthorized状态码`); | ||
return res.status(401).send({ status: "0", msg: "请先登录以继续操作" }); | ||
} | ||
if (res.locals.email !==await configManager.getConfig("security.adminuser")) { | ||
// 未登录,返回401 Unauthorized状态码 | ||
|
||
const adminEmail = await configManager.getConfig("security.adminuser"); | ||
if (res.locals.email !== adminEmail) { | ||
logger.info(`[needadmin] - ${req.ip} - 权限不足,返回401 Unauthorized状态码`); | ||
return res.status(401).send({ status: "0", msg: "权限不足" }); | ||
} | ||
next(); // 已登录,继续处理请求 | ||
} | ||
|
||
export { | ||
needlogin, | ||
needadmin | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
import { error as _error, debug } from "../logger.js"; | ||
import { error as loggerError, debug } from "../logger.js"; | ||
import { getConfig } from "../configManager.js"; | ||
import axios from "axios"; | ||
import { URL } from "url"; | ||
|
||
import express from "express"; | ||
|
||
const app = express(); | ||
import { post } from "request"; | ||
|
||
app.use(async (req, res, next) => { | ||
const recaptcha = | ||
req.body.recaptcha || req.body.re || req.query.recaptcha || req.query.re; | ||
const captchaMiddleware = async (req, res, next) => { | ||
const recaptcha = req.body.recaptcha || req.query.recaptcha; | ||
|
||
if (!recaptcha) { | ||
return res.status(200).send({ message: "请完成验证码" }); | ||
return res.status(400).send({ message: "请完成验证码" }); | ||
} | ||
|
||
post( | ||
{ | ||
url: await getConfig('captcha.reverify'), | ||
form: { secret: await getConfig('captcha.resecret'), response: recaptcha }, | ||
}, | ||
function (error, httpResponse, body) { | ||
if (error) { | ||
_error("Error verifying recaptcha:", error); | ||
res.status(200).send({ message: "验证码验证失败", error: error }); | ||
} | ||
try { | ||
const { url, secret } = await getConfig("captcha"); | ||
|
||
const response = JSON.parse(body); | ||
debug(response); | ||
if (response.success) { | ||
next(); | ||
} else { | ||
res.status(200).send({ message: "验证码无效", response: response }); | ||
const response = await axios.post( | ||
new URL("/siteverify", url), | ||
null, | ||
{ | ||
params: { | ||
secret, | ||
response: recaptcha, | ||
}, | ||
} | ||
); | ||
|
||
if (response.data.success) { | ||
next(); | ||
} else { | ||
res.status(400).send({ message: "验证码无效", response: response.data }); | ||
} | ||
); | ||
}); | ||
} catch (error) { | ||
loggerError("Error verifying recaptcha:", error); | ||
res.status(500).send({ message: "验证码验证失败", error: error.message }); | ||
} | ||
}; | ||
|
||
export default captchaMiddleware; | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters