112 lines
3.7 KiB
JavaScript
112 lines
3.7 KiB
JavaScript
// 平台账号凭据(HR/OA)按账号本机加密保存:AES-256-GCM,随机密钥落盘。
|
|
// 每人在壳里「个人中心→平台账号」填写自己的 HR/OA 账号密码;
|
|
// 登录时注入 Python 登录模块环境变量(hr_login_username / hr_login_password 等)。
|
|
"use strict";
|
|
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const crypto = require("crypto");
|
|
const config = require("./config");
|
|
const { makeLogger } = require("./logger");
|
|
|
|
const logger = makeLogger("platform-creds");
|
|
|
|
const DIR = path.join(config.APP_DATA, "platform-creds");
|
|
const KEY_FILE = path.join(DIR, ".key");
|
|
const DATA_FILE = path.join(DIR, "creds.json");
|
|
|
|
// 密文结构:{iv, tag, data}(base64)
|
|
function loadKey() {
|
|
try {
|
|
if (fs.existsSync(KEY_FILE)) return fs.readFileSync(KEY_FILE);
|
|
const key = crypto.randomBytes(32);
|
|
fs.mkdirSync(DIR, { recursive: true });
|
|
fs.writeFileSync(KEY_FILE, key, { mode: 0o600 });
|
|
return key;
|
|
} catch (e) {
|
|
logger.warning(`平台账号凭据密钥初始化失败: ${e.message}`);
|
|
return crypto.randomBytes(32); // 进程内可用;重启后不可解(仅兜底,几乎不会走到)
|
|
}
|
|
}
|
|
|
|
function encrypt(obj) {
|
|
const key = loadKey();
|
|
const iv = crypto.randomBytes(12);
|
|
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
|
|
const data = Buffer.from(JSON.stringify(obj), "utf-8");
|
|
const enc = Buffer.concat([cipher.update(data), cipher.final()]);
|
|
const tag = cipher.getAuthTag();
|
|
return { iv: iv.toString("base64"), tag: tag.toString("base64"), data: enc.toString("base64") };
|
|
}
|
|
|
|
function decrypt(payload) {
|
|
const key = loadKey();
|
|
const iv = Buffer.from(payload.iv, "base64");
|
|
const tag = Buffer.from(payload.tag, "base64");
|
|
const data = Buffer.from(payload.data, "base64");
|
|
const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
|
|
decipher.setAuthTag(tag);
|
|
return JSON.parse(Buffer.concat([decipher.update(data), decipher.final()]).toString("utf-8"));
|
|
}
|
|
|
|
function loadAll() {
|
|
if (!fs.existsSync(DATA_FILE)) return {};
|
|
try {
|
|
return decrypt(JSON.parse(fs.readFileSync(DATA_FILE, "utf-8")));
|
|
} catch (e) {
|
|
logger.warning(`平台账号凭据读取失败: ${e.message}`);
|
|
return {};
|
|
}
|
|
}
|
|
|
|
function saveAll(all) {
|
|
try {
|
|
fs.mkdirSync(DIR, { recursive: true });
|
|
fs.writeFileSync(DATA_FILE, JSON.stringify(encrypt(all)), "utf-8");
|
|
} catch (e) {
|
|
logger.warning(`平台账号凭据保存失败: ${e.message}`);
|
|
}
|
|
}
|
|
|
|
// account → {hr: {user, pass}, oa: {user, pass}}
|
|
function get(account) {
|
|
if (!account) return null;
|
|
return loadAll()[account] || null;
|
|
}
|
|
|
|
// 保存当前账号某平台的凭据;pass 为空串表示保留原密码(仅更新用户名/不清除)
|
|
function upsert(account, pid, creds) {
|
|
if (!account) throw new Error("缺少账号");
|
|
if (!creds) return false;
|
|
const all = loadAll();
|
|
const entry = all[account] || {};
|
|
const cur = entry[pid] || {};
|
|
const user = typeof creds.user === "string" ? creds.user.trim() : "";
|
|
let pass = typeof creds.pass === "string" ? creds.pass : "";
|
|
if (user === "" && pass === "") {
|
|
// 清空该平台凭据
|
|
delete entry[pid];
|
|
} else {
|
|
if (user === "") user = cur.user || ""; // 未填用户名:沿用
|
|
if (pass === "") pass = cur.pass || ""; // 未填密码:沿用
|
|
entry[pid] = { user, pass };
|
|
}
|
|
all[account] = entry;
|
|
saveAll(all);
|
|
return true;
|
|
}
|
|
|
|
// 供登录注入环境变量:{hr_login_username, hr_login_password};未配置返回 null
|
|
function envFor(account, pid) {
|
|
const c = get(account);
|
|
if (!c) return null;
|
|
const p = c[pid];
|
|
if (!p || !p.user) return null;
|
|
return {
|
|
[`${pid}_login_username`]: p.user,
|
|
[`${pid}_login_password`]: p.pass || "",
|
|
};
|
|
}
|
|
|
|
module.exports = { get, upsert, envFor, DIR };
|