feat(auth): 新增站点登录(nginx auth_request + scrypt 密码 + 签名 Cookie),登录页与退出入口,API 路径保持 Key 鉴权

This commit is contained in:
sgp
2026-11-04 18:43:00 +08:00
parent ca75da8589
commit 28af756b82
6 changed files with 590 additions and 7 deletions
+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env node
/**
* 设置站点登录帐号(写入 config.json,密码只存 scrypt 哈希)
*
* 用法:
* node deploy/set-auth.js <username> <password>
* node deploy/set-auth.js admin 'admin123&aa'
*
* 说明:
* - 会保留现有 auth.secret(已有会话不失效);首次运行自动生成
* - 修改后需重启服务:sudo systemctl restart drawlogo-api
*/
'use strict';
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const username = process.argv[2];
const password = process.argv[3];
if (!username || !password) {
console.error('用法: node deploy/set-auth.js <username> <password>');
process.exit(1);
}
const file = path.join(__dirname, '..', 'config.json');
if (!fs.existsSync(file)) {
console.error('未找到 config.json,请先 cp config.example.json config.json');
process.exit(1);
}
const cfg = JSON.parse(fs.readFileSync(file, 'utf8'));
const salt = crypto.randomBytes(16).toString('hex');
const hash = crypto.scryptSync(password, salt, 64).toString('hex');
cfg.auth = cfg.auth || {};
cfg.auth.username = username;
cfg.auth.passwordHash = 'scrypt$' + salt + '$' + hash;
cfg.auth.secret = cfg.auth.secret || crypto.randomBytes(32).toString('hex');
cfg.auth.maxAgeDays = cfg.auth.maxAgeDays || 7;
fs.writeFileSync(file, JSON.stringify(cfg, null, 2));
try { fs.chmodSync(file, 0o600); } catch (e) { /* Windows 忽略 */ }
console.log('已设置登录帐号: ' + username + '(scrypt 哈希已写入 config.json)');