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 }