数据源配置可发起钉钉官方设备授权、重新授权和取消,完成后刷新共享账号状态并独立检查目录权限。 授权会话限定发起的管理员登录会话,令牌由现有 CLI 存储,取消等待子进程退出。 验证:完整 make verify 通过;前端 582 项、Go 1545 项通过,487 项声明的可选集成未运行。 授权处理器与 HTTP 边界通过 race 检查;本人钉钉授权仍由管理员在官方页面完成。
This commit is contained in:
@@ -107,6 +107,9 @@ func classifyRole(method, path string) string {
|
||||
if strings.HasPrefix(path, "/api/v1/users") || strings.HasPrefix(path, "/api/v1/qa-ops/") {
|
||||
return roleAdmin
|
||||
}
|
||||
if strings.HasPrefix(path, "/api/v1/dws-login") {
|
||||
return roleAdmin
|
||||
}
|
||||
if method == http.MethodGet {
|
||||
return roleViewer
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
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})
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"knowledge-governance/internal/dingtalkdiscovery"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDWSLoginHTTPAuthenticationOwnershipAndOrigin(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
manager := dingtalkdiscovery.NewDeviceLoginManager(ctx, dingtalkdiscovery.ExecRunner{Binary: "/nonexistent/dws"})
|
||||
h, err := NewHandler(Config{Store: &fakeJobStore{}, DWSLogin: manager})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
call := func(method, path, role, session, origin, intent string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(method, path, nil)
|
||||
if role != "" {
|
||||
r = r.WithContext(context.WithValue(r.Context(), sessionKey{}, sessionIdentity{UserID: "admin-user", Username: "tester", Role: role, SessionID: session}))
|
||||
}
|
||||
r.Header.Set("Origin", origin)
|
||||
r.Header.Set("X-Governance-Action", intent)
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
for _, role := range []string{"", roleViewer, roleEngineer} {
|
||||
w := call("POST", "/api/v1/dws-login", role, "s1", "", "dws-login")
|
||||
if w.Code != 401 && w.Code != 403 {
|
||||
t.Fatalf("role=%q status=%d", role, w.Code)
|
||||
}
|
||||
}
|
||||
for _, input := range [][2]string{{"", ""}, {"https://foreign.invalid", "dws-login"}} {
|
||||
w := call("POST", "/api/v1/dws-login", roleAdmin, "s1", input[0], input[1])
|
||||
if w.Code != 403 {
|
||||
t.Fatalf("origin/intent not enforced: %d", w.Code)
|
||||
}
|
||||
}
|
||||
w := call("POST", "/api/v1/dws-login", roleAdmin, "s1", "", "dws-login")
|
||||
if w.Code != 202 || w.Header().Get("Cache-Control") != "no-store" {
|
||||
t.Fatalf("start=%d body=%s", w.Code, w.Body)
|
||||
}
|
||||
var body struct {
|
||||
Data dingtalkdiscovery.DeviceLoginView `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil || body.Data.ID == "" {
|
||||
t.Fatal("missing session", err)
|
||||
}
|
||||
path := "/api/v1/dws-login/" + body.Data.ID
|
||||
for _, method := range []string{"GET", "DELETE"} {
|
||||
w = call(method, path, roleAdmin, "other-session", "", "dws-login")
|
||||
if w.Code != 404 {
|
||||
t.Fatalf("foreign session %s=%d", method, w.Code)
|
||||
}
|
||||
w = call(method, path, roleEngineer, "s1", "", "dws-login")
|
||||
if w.Code != 403 {
|
||||
t.Fatalf("engineer %s=%d", method, w.Code)
|
||||
}
|
||||
}
|
||||
w = call("GET", path, roleAdmin, "s1", "", "")
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("get=%d", w.Code)
|
||||
}
|
||||
w = call("DELETE", path, roleAdmin, "s1", "", "dws-login")
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("cancel=%d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDWSLoginRoutesAreAdminOnly(t *testing.T) {
|
||||
for _, method := range []string{http.MethodGet, http.MethodPost, http.MethodDelete} {
|
||||
for _, path := range []string{"/api/v1/dws-login", "/api/v1/dws-login/session-id"} {
|
||||
if got := classifyRole(method, path); got != roleAdmin {
|
||||
t.Errorf("%s %s role=%s; authorization artifacts must be admin-only", method, path, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/backuphost"
|
||||
"knowledge-governance/internal/dingtalkdiscovery"
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
"knowledge-governance/internal/monitoring"
|
||||
@@ -222,6 +223,7 @@ type Config struct {
|
||||
DingtalkSources dingtalkSourcesLister
|
||||
// DingtalkDocuments 列在线文档/表格并校验可读(只读,dws)。
|
||||
DingtalkDocuments dingtalkDocLister
|
||||
DWSLogin *dingtalkdiscovery.DeviceLoginManager
|
||||
NewID func() (string, error)
|
||||
Auth *AuthConfig
|
||||
}
|
||||
@@ -256,6 +258,7 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
config.NewID = newID
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
registerDWSLoginRoutes(mux, config)
|
||||
registerQAAIRoutes(mux, config)
|
||||
mux.HandleFunc("POST /api/v1/qa-ops/departments/statistics", func(w http.ResponseWriter, r *http.Request) { qaDepartmentStatistics(w, r, config) })
|
||||
mux.HandleFunc("POST /api/v1/qa-ops/dashboard", func(w http.ResponseWriter, r *http.Request) { qaOperationsDashboard(w, r, config) })
|
||||
|
||||
Reference in New Issue
Block a user