3825859da6
build / build (push) Successful in 2m20s
PP 增加受控凭据更新、真实登录与目录检查,待验证修改不会替换生效配置;扫描与取件共享生效修订。 钉钉来源向导、校验与登记共用 DWS 授权,支持中途恢复与换账号保护,取消旧扫码自动回落。 认证审计复用已有来源审计页,配置响应不回传密码;补齐运行镜像脚本与权限。 验证:make verify 退出 0;PP/DWS/httpapi 目标 race 回归通过;隔离 HTTP 服务验证 PP 协议及扫描/取件消费者。 尚未部署;真实 PP 凭据与目录可用性在测试站部署后验证。
84 lines
3.3 KiB
Go
84 lines
3.3 KiB
Go
package httpapi
|
||
|
||
import (
|
||
"errors"
|
||
"log"
|
||
"net/http"
|
||
"net/url"
|
||
|
||
"knowledge-governance/internal/dingtalkdiscovery"
|
||
)
|
||
|
||
func registerDWSLoginRoutes(mux *http.ServeMux, config Config) {
|
||
mux.HandleFunc("POST /api/v1/dws-login", func(w http.ResponseWriter, r *http.Request) { handleDWSLogin(w, r, config, "start") })
|
||
mux.HandleFunc("GET /api/v1/dws-login/{id}", func(w http.ResponseWriter, r *http.Request) { handleDWSLogin(w, r, config, "get") })
|
||
mux.HandleFunc("DELETE /api/v1/dws-login/{id}", func(w http.ResponseWriter, r *http.Request) { handleDWSLogin(w, r, config, "cancel") })
|
||
}
|
||
|
||
func handleDWSLogin(w http.ResponseWriter, r *http.Request, config Config, action string) {
|
||
w.Header().Set("Cache-Control", "no-store")
|
||
identity, ok := requireSessionIdentity(w, r)
|
||
if !ok {
|
||
return
|
||
}
|
||
if identity.Role != roleAdmin {
|
||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "需要管理员身份才能管理 DWS 授权"})
|
||
return
|
||
}
|
||
if action != "get" {
|
||
originOK := true
|
||
if raw := r.Header.Get("Origin"); raw != "" {
|
||
origin, err := url.Parse(raw)
|
||
originOK = err == nil && origin.Host == r.Host && (origin.Scheme == "http" || origin.Scheme == "https")
|
||
}
|
||
if !originOK || r.Header.Get("Sec-Fetch-Site") == "cross-site" || r.Header.Get("X-Governance-Action") != "dws-login" {
|
||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "请从本平台的数据源配置页发起授权"})
|
||
return
|
||
}
|
||
}
|
||
if config.DWSLogin == nil {
|
||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "DWS 登录服务未接通,请联系管理员"})
|
||
return
|
||
}
|
||
// Bind the challenge to this authenticated administrator and browser session.
|
||
owner := identity.UserID + "/" + identity.SessionID
|
||
var view dingtalkdiscovery.DeviceLoginView
|
||
var err error
|
||
switch action {
|
||
case "start":
|
||
view, err = config.DWSLogin.Start(owner)
|
||
case "get":
|
||
view, err = config.DWSLogin.Get(owner, r.PathValue("id"))
|
||
case "cancel":
|
||
view, err = config.DWSLogin.Cancel(owner, r.PathValue("id"))
|
||
}
|
||
if err != nil {
|
||
switch {
|
||
case errors.Is(err, dingtalkdiscovery.ErrLoginNotFound):
|
||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "授权会话已结束或服务已重启,请重新发起"})
|
||
case errors.Is(err, dingtalkdiscovery.ErrLoginBusy):
|
||
writeJSON(w, http.StatusConflict, map[string]string{"error": "另一位管理员正在授权,请稍后再试"})
|
||
default:
|
||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "无法发起 DWS 授权,请重试"})
|
||
}
|
||
return
|
||
}
|
||
if config.PPConnection != nil && (action != "get" || view.State == "succeeded" || view.State == "failed" || view.State == "expired") {
|
||
if err := config.PPConnection.AuditOnce(r.Context(), "dws-"+view.ID+"-"+string(view.State), "钉钉账号(DWS)", "dws_authorization", identity.Username, "DWS 授权状态:"+string(view.State)); err != nil {
|
||
if action == "start" {
|
||
_, _ = config.DWSLogin.Cancel(owner, view.ID)
|
||
}
|
||
writeJSON(w, 503, map[string]string{"error": "授权审计写入失败,请重试"})
|
||
return
|
||
}
|
||
}
|
||
if action != "get" {
|
||
log.Printf("DWS authorization action=%s id=%s actor=%s", action, view.ID, identity.Username)
|
||
}
|
||
status := http.StatusOK
|
||
if action == "start" {
|
||
status = http.StatusAccepted
|
||
}
|
||
writeJSON(w, status, map[string]any{"data": view})
|
||
}
|