refactor(stage4): 集中配置与启动校验,权限守卫 fail-closed,凭据移出仓库
配置分层 - 新增 src/server/config.ts:环境变量集中读取 + 启动期校验。 删除公开兜底密钥 ln-bi-default-secret:SSO 模式下 JWT_SECRET 缺失或 短于 32 字符时 assertRuntimeConfig() 直接拒绝启动(已实测)。 - index.ts 不再自己读端口/环境;auth/config.ts 与 auth/login.ts 改为引用集中配置。 权限守卫 - energy / scheduling / hydrogen-heatmap 的守卫由 “user 存在才校验” 改为 “无角色即拒绝”:user 缺失时按无权限处理,不再静默放行(fail-closed)。 - /api/ele/* 此前完全无鉴权,任何已登录用户都能写入电费表;现按能源域 (BI-LEADER-ENERGY) 守卫。 接口契约 - 未匹配的 /api/* 由 200 text/html(SPA) 改为 404 application/json; 未认证时仍是 401,避免向未授权调用方暴露路由是否存在。 - 新增全局 onError 返回 JSON 500。 凭据治理(此前均为 git 跟踪文件中的明文) - Dockerfile 删除烧进镜像的 JWT_SECRET,改为必须运行时注入。 - docker-compose.yml 删除生产库口令/JWT 密钥/失效的 MILEAGE_DB_*, 改为强制注入写法;补齐 OSS_* 与 NODE_ENV/DEV_BYPASS_AUTH/BI_AUTH_*。 - woodpecker.yml 删除 Harbor base64 凭据改用 secret,pull_request 不再推镜像。 - 删除 scripts-tmp/(含生产库 root 口令)与已跟踪的 .DS_Store;.gitignore 补全。 - 文档中残留的里程库口令改为占位符。 lint / test(128) / build 全绿。
This commit is contained in:
@@ -5,13 +5,11 @@ import type { AuthUser, JwtPayload, PermissionLevel } from './types.js';
|
||||
import { FULL_ACCESS_ROLES, DEPT_ACCESS_ROLES } from './types.js';
|
||||
import { authMode, verifyAuthToken } from './config.js';
|
||||
import { passwordRouter } from './password.js';
|
||||
import { jwtSecret, serverConfig } from '../config.js';
|
||||
|
||||
const app = new Hono();
|
||||
app.route('/', passwordRouter());
|
||||
|
||||
const EXTERNAL_API_BASE = process.env.EXTERNAL_API_BASE || 'https://beta.lnh2e.com';
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'ln-bi-default-secret';
|
||||
|
||||
/** GET /api/auth/exchange?jumpToken=xxx — 一步完成:换取用户信息 + 签发 JWT */
|
||||
app.get('/exchange', async (c) => {
|
||||
if (authMode() !== 'sso') return c.json({ message: '当前使用固定密码登录' }, 403);
|
||||
@@ -20,7 +18,7 @@ app.get('/exchange', async (c) => {
|
||||
|
||||
try {
|
||||
const res = await fetch(
|
||||
`${EXTERNAL_API_BASE}/api/lingniu-manager-v1/v1/auth/issueTokenByJump?jumpToken=${encodeURIComponent(jumpToken)}`
|
||||
`${serverConfig.externalApiBase}/api/lingniu-manager-v1/v1/auth/issueTokenByJump?jumpToken=${encodeURIComponent(jumpToken)}`
|
||||
);
|
||||
const data = await res.json() as {
|
||||
code: number;
|
||||
@@ -77,7 +75,7 @@ app.get('/exchange', async (c) => {
|
||||
roles: roleNames,
|
||||
};
|
||||
|
||||
const token = jwt.sign(payload, JWT_SECRET, { expiresIn: '8h' });
|
||||
const token = jwt.sign(payload, jwtSecret(), { expiresIn: '8h' });
|
||||
const authUser: AuthUser = { ...payload };
|
||||
|
||||
return c.json({ token, user: authUser });
|
||||
|
||||
Reference in New Issue
Block a user