Files
seal-use/src/core/auth/token-store.ts
T
gitea 8c9f102931 feat: 原生 Seal 授权续期自愈与本地 SQLite 分歧待办闭环
- 授权自愈: 引入长效会话 Cookie 备份池,支持 Seal 原生协助授权到期与 401 报错时平滑自动换发与静默续期;
- 存储升级: 审核报告人工裁决底座迁移至 SQLite (~/.config/seal-use/state/reviews.sqlite),支持历史 JSONL 自动平滑导入与向后兼容双写;
- 分歧待办: 新增 report review unreviewed 与 dismiss 命令,收集人机不一致单据并在日常企业命令完成后于 stderr 注入 Agent 动作指引;
- 精密频控: 实现 30 天单据时效窗口过滤(陈年单据自动归档)、动态 5 小时滑动窗口频控与 24 小时人工拒审静音抑制协议;
- 透明认知: 全面补充 Skill 路由与帮助手册,消除黑盒,透传数据边界与安全红线。
2026-11-02 15:22:47 +08:00

117 lines
2.8 KiB
TypeScript

import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { writeSecureFile } from "../secure-file.js";
import { SEAL_CONFIG_DIR } from "../paths.js";
import {
CorpTokenCache,
HoseAuthCache,
SealDelegateAuthCache,
SealSession,
TokenStore,
TokenStoreSchema
} from "./types.js";
const PRIMARY_TOKEN_STORE = join(SEAL_CONFIG_DIR, "tokens.json");
const MIGRATION_TOKEN_STORE = join(SEAL_CONFIG_DIR, "sealTokens.json");
function resolveReadStorePath(): string {
const configured = (process.env.SEAL_USE_TOKEN_STORE ?? process.env.SEAL_HOME_TOKEN_STORE)?.trim();
if (configured) return configured;
if (existsSync(PRIMARY_TOKEN_STORE)) return PRIMARY_TOKEN_STORE;
if (existsSync(MIGRATION_TOKEN_STORE)) return MIGRATION_TOKEN_STORE;
return PRIMARY_TOKEN_STORE;
}
function resolveWriteStorePath(): string {
const configured = (process.env.SEAL_USE_TOKEN_STORE ?? process.env.SEAL_HOME_TOKEN_STORE)?.trim();
if (configured) return configured;
return PRIMARY_TOKEN_STORE;
}
function readStore(): TokenStore {
const path = resolveReadStorePath();
if (!existsSync(path)) {
return {};
}
try {
return TokenStoreSchema.parse(JSON.parse(readFileSync(path, "utf-8")));
} catch {
return {};
}
}
function writeStore(store: TokenStore): void {
const path = resolveWriteStorePath();
writeSecureFile(path, JSON.stringify(store, null, 2));
}
export function getSealSession(corpId: string): SealSession | undefined {
return readStore()[corpId]?.seal;
}
export function setSealSession(corpId: string, session: SealSession): void {
const store = readStore();
writeStore({
...store,
[corpId]: {
...store[corpId],
seal: session
}
});
}
export function clearSealSession(corpId: string): void {
const store = readStore();
const current = store[corpId];
if (!current) return;
writeStore({
...store,
[corpId]: {
...current,
seal: undefined
}
});
}
export function getCorpTokenCache(corpId: string): CorpTokenCache {
return readStore()[corpId] ?? {};
}
export function getHoseAuthCache(corpId: string): HoseAuthCache {
return readStore()[corpId]?.hose ?? {};
}
export function setHoseAuthCache(corpId: string, hose: HoseAuthCache): void {
const store = readStore();
writeStore({
...store,
[corpId]: {
...store[corpId],
hose
}
});
}
export function getSealDelegateAuthCache(corpId: string): SealDelegateAuthCache {
return readStore()[corpId]?.sealDelegate ?? {};
}
export function setSealDelegateAuthCache(corpId: string, sealDelegate: SealDelegateAuthCache): void {
const store = readStore();
writeStore({
...store,
[corpId]: {
...store[corpId],
sealDelegate
}
});
}
export function clearCorpTokenCache(corpId: string): void {
const store = readStore();
delete store[corpId];
writeStore(store);
}