Files
knowledge-governance/internal/dingtalkdiscovery/credentials.go
T
姚章浩 3825859da6
build / build (push) Successful in 2m20s
feat(settings): 打通 PP 认证配置并统一 DWS 来源授权
PP 增加受控凭据更新、真实登录与目录检查,待验证修改不会替换生效配置;扫描与取件共享生效修订。
钉钉来源向导、校验与登记共用 DWS 授权,支持中途恢复与换账号保护,取消旧扫码自动回落。
认证审计复用已有来源审计页,配置响应不回传密码;补齐运行镜像脚本与权限。

验证:make verify 退出 0;PP/DWS/httpapi 目标 race 回归通过;隔离 HTTP 服务验证 PP 协议及扫描/取件消费者。
尚未部署;真实 PP 凭据与目录可用性在测试站部署后验证。
2026-11-17 19:08:46 +08:00

49 lines
2.1 KiB
Go

package dingtalkdiscovery
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
)
// CredentialStatus contains metadata only, never credential values.
type CredentialStatus struct {
Identity string `json:"identity,omitempty"`
Authenticated bool `json:"authenticated"`
RefreshTokenValid bool `json:"refresh_token_valid"`
ExpiresAt string `json:"expires_at,omitempty"`
RefreshExpiresAt string `json:"refresh_expires_at,omitempty"`
CorpName string `json:"corp_name,omitempty"`
UserName string `json:"user_name,omitempty"`
StatusKnown bool `json:"status_known"`
}
func ReadCredentialStatus(ctx context.Context, runner Runner) (CredentialStatus, error) {
raw, err := runner.RunDWS(ctx, "auth", "status", "--readonly", "--format", "json")
if err != nil {
return CredentialStatus{}, errors.New("could not read DWS credential status")
}
var decoded struct {
CorpID json.RawMessage `json:"corp_id"`
UserID json.RawMessage `json:"user_id"`
Authenticated *bool `json:"authenticated"`
RefreshTokenValid bool `json:"refresh_token_valid"`
ExpiresAt string `json:"expires_at"`
RefreshExpiresAt string `json:"refresh_expires_at"`
CorpName string `json:"corp_name"`
UserName string `json:"user_name"`
Reason string `json:"reason"`
}
if json.Unmarshal(raw, &decoded) != nil || decoded.Authenticated == nil {
return CredentialStatus{}, errors.New("invalid DWS credential status")
}
identity := ""
if len(decoded.CorpID) > 0 && len(decoded.UserID) > 0 && string(decoded.CorpID) != "null" && string(decoded.UserID) != "null" {
sum := sha256.Sum256(append(append(decoded.CorpID, 0), decoded.UserID...))
identity = hex.EncodeToString(sum[:])
}
return CredentialStatus{Identity: identity, Authenticated: *decoded.Authenticated, RefreshTokenValid: decoded.RefreshTokenValid, ExpiresAt: decoded.ExpiresAt, RefreshExpiresAt: decoded.RefreshExpiresAt, CorpName: decoded.CorpName, UserName: decoded.UserName, StatusKnown: decoded.Reason == ""}, nil
}