Skip to content

Commit dd491d4

Browse files
committed
feat(auth): 添加可配置的管理员Token过期时间
1 parent 0ef3753 commit dd491d4

File tree

6 files changed

+15
-4
lines changed

6 files changed

+15
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,5 @@ out
106106
.tern-port
107107

108108
# Stores VSCode versions used for testing VSCode extensions
109-
.vscode-test
109+
.vscode-test
110+
.spec-workflow

backend/docker-server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ server.use(async (req, res, next) => {
523523
req.env = {
524524
DB: sqliteAdapter,
525525
ENCRYPTION_SECRET: process.env.ENCRYPTION_SECRET || "default-encryption-key",
526+
ADMIN_TOKEN_EXPIRY_DAYS: process.env.ADMIN_TOKEN_EXPIRY_DAYS || "7",
526527
};
527528

528529
next();

backend/src/routes/adminRoutes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ adminRoutes.post("/api/admin/login", async (c) => {
1313
const { username, password } = await c.req.json();
1414

1515
try {
16-
const loginResult = await login(db, username, password);
16+
const loginResult = await login(db, username, password, c.env);
1717

1818
return c.json({
1919
code: ApiStatus.SUCCESS,

backend/src/services/adminService.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ export async function validateAdminToken(db, token) {
3838
* @param {D1Database} db - D1数据库实例
3939
* @param {string} username - 用户名
4040
* @param {string} password - 密码
41+
* @param {Object} env - 环境变量对象
4142
* @returns {Promise<Object>} 登录结果,包含token和过期时间
4243
*/
43-
export async function login(db, username, password) {
44+
export async function login(db, username, password, env = {}) {
4445
// 参数验证
4546
if (!username || !password) {
4647
throw new HTTPException(ApiStatus.BAD_REQUEST, { message: "用户名和密码不能为空" });
@@ -88,7 +89,10 @@ export async function login(db, username, password) {
8889
// 生成并存储令牌
8990
const token = generateRandomString(32);
9091
const expiresAt = new Date();
91-
expiresAt.setDate(expiresAt.getDate() + 1); // 1天过期
92+
93+
// 从环境变量读取token过期天数,默认7天
94+
const expiryDays = parseInt(env.ADMIN_TOKEN_EXPIRY_DAYS || "7", 10);
95+
expiresAt.setDate(expiresAt.getDate() + expiryDays);
9296

9397
// 使用 AdminRepository 创建令牌
9498
await adminRepository.createToken(admin.id, token, expiresAt);

backend/wrangler.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ compatibility_flags = ["nodejs_compat"]
77
[vars]
88
# 用于加密敏感配置的密钥
99
# ENCRYPTION_SECRET = "lzl123456789987654321"
10+
# 管理员Token过期天数,默认7天
11+
# ADMIN_TOKEN_EXPIRY_DAYS = "7"
1012

1113
# 绑定D1数据库
1214
# 需要替换为实际的数据库ID
@@ -20,6 +22,7 @@ database_id = "xxxxxxxxxxxxxxxxx"
2022
# 开发环境变量
2123
[env.dev.vars]
2224
ENCRYPTION_SECRET = "dev-encryption-secret-key"
25+
ADMIN_TOKEN_EXPIRY_DAYS = "7"
2326

2427
[[env.dev.d1_databases]]
2528
binding = "DB"

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ services:
2929
- LOG_LEVEL=2 # 日志级别
3030
# 重要: 请修改为您自己的安全密钥,用于加密数据
3131
- ENCRYPTION_SECRET=xxxxxxx
32+
# 管理员Token过期天数,默认7天
33+
- ADMIN_TOKEN_EXPIRY_DAYS=7
3234
volumes:
3335
- ./sql_data:/data # 将当前目录下的sql_data映射到容器的/data目录
3436
ports:

0 commit comments

Comments
 (0)