b2cea6ed84
build / build (push) Successful in 1m52s
数据源配置可发起钉钉官方设备授权、重新授权和取消,完成后刷新共享账号状态并独立检查目录权限。 授权会话限定发起的管理员登录会话,令牌由现有 CLI 存储,取消等待子进程退出。 验证:完整 make verify 通过;前端 582 项、Go 1545 项通过,487 项声明的可选集成未运行。 授权处理器与 HTTP 边界通过 race 检查;本人钉钉授权仍由管理员在官方页面完成。
75 lines
2.7 KiB
Go
75 lines
2.7 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 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})
|
|
}
|