feat(govai): 0617 优化首批 — 安全/私有化/深度研究/服务层/可观测性
借鉴 odysseus 的能力设计,全程净室实现、零 AGPL 代码、不引入 AGPL 依赖。
T1 提示注入防护: pkg/promptguard 包裹外部/知识库内容为不可信数据,buildMessages 移出 system 指令区。 T2 安全 CI: .github/workflows(ci+security: govulncheck/gitleaks/actionlint/hadolint/trivy)+dependabot+.hadolint.yaml;go.mod 加 toolchain go1.25.11 修复 20 个 stdlib CVE。 T3 管理员 2FA: 迁移 000016 + RFC6238 TOTP/备份码(pkg/auth, 零依赖) + 登录流程集成(后端)。 T4 本地模型: LLM/embedding 支持本地 vLLM/Ollama(OpenAI 兼容, 鉴权头条件发送, NoAuth) + docs/local-deploy.md。 T6 深度研究: 迁移 000017 + Python research-worker(净室多步流水线, 检索避开 SearXNG) + Go research 服务/handler/路由。 T7 service 层: 新增 internal/service/{research,twofa}, 2FA 业务逻辑从胖 handler 下沉, 接口注入可单测。 T10 缓存/可观测性: internal/cache(Redis+内存, 优雅降级) 接入 store 热点列表; Prometheus 指标+/metrics; docs/openapi.yaml。 验证: go build/vet/test ./... 全绿(8 包); research-worker 12 单测过; 真实 PG 应用迁移并烟测。
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/enterprise-ai-platform/server/internal/middleware"
|
||||
"github.com/enterprise-ai-platform/server/internal/response"
|
||||
"github.com/enterprise-ai-platform/server/internal/service/twofa"
|
||||
"github.com/enterprise-ai-platform/server/pkg/auth"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
@@ -16,16 +17,19 @@ import (
|
||||
type AuthHandler struct {
|
||||
pool *pgxpool.Pool
|
||||
jwtMgr *auth.JWTManager
|
||||
twofa *twofa.Service
|
||||
}
|
||||
|
||||
func NewAuthHandler(pool *pgxpool.Pool, jwtMgr *auth.JWTManager) *AuthHandler {
|
||||
return &AuthHandler{pool: pool, jwtMgr: jwtMgr}
|
||||
func NewAuthHandler(pool *pgxpool.Pool, jwtMgr *auth.JWTManager, twofaSvc *twofa.Service) *AuthHandler {
|
||||
return &AuthHandler{pool: pool, jwtMgr: jwtMgr, twofa: twofaSvc}
|
||||
}
|
||||
|
||||
type loginRequest struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
OrgID string `json:"org_id"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
OrgID string `json:"org_id"`
|
||||
TOTPCode string `json:"totp_code"`
|
||||
BackupCode string `json:"backup_code"`
|
||||
}
|
||||
|
||||
type orgInfo struct {
|
||||
@@ -135,12 +139,16 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
employeeID *string
|
||||
status string
|
||||
orgID *string
|
||||
totpEnabled bool
|
||||
totpSecret *string
|
||||
)
|
||||
|
||||
err := h.pool.QueryRow(r.Context(),
|
||||
`SELECT id, name, email, password_hash, avatar_url, role, employee_id, status, org_id::text
|
||||
`SELECT id, name, email, password_hash, avatar_url, role, employee_id, status, org_id::text,
|
||||
COALESCE(totp_enabled, false), totp_secret
|
||||
FROM users WHERE email = $1`, req.Email,
|
||||
).Scan(&id, &name, &email, &passwordHash, &avatarURL, &role, &employeeID, &status, &orgID)
|
||||
).Scan(&id, &name, &email, &passwordHash, &avatarURL, &role, &employeeID, &status, &orgID,
|
||||
&totpEnabled, &totpSecret)
|
||||
|
||||
if err != nil {
|
||||
response.Unauthorized(w, "邮箱或密码错误")
|
||||
@@ -157,6 +165,23 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// 两步验证(仅对已启用 2FA 的账号):密码通过后再校验 TOTP / 备份码。
|
||||
if totpEnabled {
|
||||
if req.TOTPCode == "" && req.BackupCode == "" {
|
||||
// 前端据此错误码弹出验证码输入框,再带 totp_code 重新登录。
|
||||
response.Error(w, http.StatusUnauthorized, codeNeed2FA, "需要两步验证码")
|
||||
return
|
||||
}
|
||||
secret := ""
|
||||
if totpSecret != nil {
|
||||
secret = *totpSecret
|
||||
}
|
||||
if !h.twofa.VerifyLogin(r.Context(), id, secret, req.TOTPCode, req.BackupCode) {
|
||||
response.Error(w, http.StatusUnauthorized, codeBad2FA, "验证码或备份码错误")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 平台管理员不绑定机构,可登录任意机构入口
|
||||
// 普通用户/机构管理员必须属于所选机构
|
||||
if role != "super_admin" && req.OrgID != "" && orgID != nil && *orgID != req.OrgID {
|
||||
|
||||
Reference in New Issue
Block a user