3825859da6
build / build (push) Successful in 2m20s
PP 增加受控凭据更新、真实登录与目录检查,待验证修改不会替换生效配置;扫描与取件共享生效修订。 钉钉来源向导、校验与登记共用 DWS 授权,支持中途恢复与换账号保护,取消旧扫码自动回落。 认证审计复用已有来源审计页,配置响应不回传密码;补齐运行镜像脚本与权限。 验证:make verify 退出 0;PP/DWS/httpapi 目标 race 回归通过;隔离 HTTP 服务验证 PP 协议及扫描/取件消费者。 尚未部署;真实 PP 凭据与目录可用性在测试站部署后验证。
170 lines
4.8 KiB
Go
170 lines
4.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// DingTalk login proxy. The scan-code login itself lives in the
|
|
// dingtalk-login sidecar (headless Chromium); the API is the authenticated
|
|
// gate and browser relay, so no QR or token crosses the web origin without a
|
|
// session. classifyRole keeps every non-GET at engineer+; capture replacement
|
|
// remains a deliberate human action.
|
|
//
|
|
// POST /api/v1/dingtalk-session -> start a login session {space?}
|
|
// GET /api/v1/dingtalk-session/{id} -> poll state (qr_png while waiting)
|
|
// DELETE /api/v1/dingtalk-session/{id} -> abort a session
|
|
|
|
func dingtalkSidecarURL() string {
|
|
return strings.TrimRight(strings.TrimSpace(os.Getenv("DINGTALK_LOGIN_SIDECAR_URL")), "/")
|
|
}
|
|
|
|
func requireDingtalkIdentity(w http.ResponseWriter, r *http.Request) bool {
|
|
_, ok := requireSessionIdentity(w, r)
|
|
return ok
|
|
}
|
|
|
|
func startDingtalkSession(w http.ResponseWriter, r *http.Request) {
|
|
if !requireDingtalkIdentity(w, r) {
|
|
return
|
|
}
|
|
var body struct {
|
|
Space string `json:"space"`
|
|
}
|
|
if r.Body != nil && r.ContentLength != 0 {
|
|
if err := decodeJSONStrict(r, &body); err != nil {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid_request_body"})
|
|
return
|
|
}
|
|
}
|
|
payload, _ := json.Marshal(map[string]string{"space": strings.TrimSpace(body.Space)})
|
|
status, data := proxyDingtalk(http.MethodPost, "/sessions", payload)
|
|
log.Printf("dingtalk-login session started by %s (status %d)", actorFor(r), status)
|
|
writeProxyResult(w, status, data)
|
|
}
|
|
|
|
func getDingtalkSession(w http.ResponseWriter, r *http.Request) {
|
|
if !requireDingtalkIdentity(w, r) {
|
|
return
|
|
}
|
|
id := safeSessionID(r.PathValue("id"))
|
|
if id == "" {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid_session_id"})
|
|
return
|
|
}
|
|
status, data := proxyDingtalk(http.MethodGet, "/sessions/"+id, nil)
|
|
writeProxyResult(w, status, data)
|
|
}
|
|
|
|
func deleteDingtalkSession(w http.ResponseWriter, r *http.Request) {
|
|
if !requireDingtalkIdentity(w, r) {
|
|
return
|
|
}
|
|
id := safeSessionID(r.PathValue("id"))
|
|
if id == "" {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid_session_id"})
|
|
return
|
|
}
|
|
status, data := proxyDingtalk(http.MethodDelete, "/sessions/"+id, nil)
|
|
log.Printf("dingtalk-login session %s closed by %s", id, actorFor(r))
|
|
writeProxyResult(w, status, data)
|
|
}
|
|
|
|
// registerDingtalkSpace resolves a DingTalk knowledge-base space URL through
|
|
// the login sidecar and, on success, registers the returned space as a
|
|
// source connection (deterministic id, enabled, manual-only schedule until an
|
|
// admin sets an interval). Re-registering the same space re-syncs name/root.
|
|
func registerDingtalkSpace(w http.ResponseWriter, r *http.Request, config Config) {
|
|
if !requireDingtalkIdentity(w, r) {
|
|
return
|
|
}
|
|
operationID, ok := requireOperationID(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
var input struct {
|
|
URL string `json:"url"`
|
|
}
|
|
if err := decodeJSONStrict(r, &input); err != nil || strings.TrimSpace(input.URL) == "" {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "url is required"})
|
|
return
|
|
}
|
|
registerDWSOnlySpace(w, r, config, strings.TrimSpace(input.URL), operationID)
|
|
}
|
|
|
|
var dingtalkSpacePattern = regexp.MustCompile(`/spaces/([A-Za-z0-9_-]+)`)
|
|
|
|
func extractDingtalkSpaceID(raw string) string {
|
|
if !validDingtalkURL(raw) {
|
|
return ""
|
|
}
|
|
match := dingtalkSpacePattern.FindStringSubmatch(raw)
|
|
if len(match) == 2 {
|
|
return match[1]
|
|
}
|
|
trimmed := strings.TrimSpace(raw)
|
|
if regexp.MustCompile(`^[A-Za-z0-9_-]{8,64}$`).MatchString(trimmed) {
|
|
return trimmed
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func safeSessionID(id string) string {
|
|
id = strings.TrimSpace(id)
|
|
if id == "" || len(id) > 64 {
|
|
return ""
|
|
}
|
|
for _, c := range id {
|
|
if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-') {
|
|
return ""
|
|
}
|
|
}
|
|
return id
|
|
}
|
|
|
|
func writeProxyResult(w http.ResponseWriter, status int, data []byte) {
|
|
if status == 0 || data == nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "dingtalk_login_unavailable"})
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_, _ = w.Write(data)
|
|
}
|
|
|
|
func proxyDingtalk(method, path string, body []byte) (int, []byte) {
|
|
base := dingtalkSidecarURL()
|
|
if base == "" {
|
|
return 0, nil
|
|
}
|
|
var reader io.Reader
|
|
if body != nil {
|
|
reader = bytes.NewReader(body)
|
|
}
|
|
req, err := http.NewRequest(method, base+path, reader)
|
|
if err != nil {
|
|
return 0, nil
|
|
}
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
client := &http.Client{Timeout: 15 * time.Second}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return 0, nil
|
|
}
|
|
defer resp.Body.Close()
|
|
data, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
|
if err != nil {
|
|
return resp.StatusCode, nil
|
|
}
|
|
return resp.StatusCode, data
|
|
}
|