Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】合并 auth 中重复的 RIO 签名 GET 请求构造 (#253)

fx-kurumi812ace2b9191
diff --git a/internal/auth/client.go b/internal/auth/client.go
index 105157d8..2ee79cfd 100644
--- a/internal/auth/client.go
+++ b/internal/auth/client.go
@@ -204,6 +204,19 @@ func (c *Client) GenerateRIOHeaders() map[string]string {
return GenerateRIOHeaders(c.cfg.PaaSID, c.cfg.AppToken, timestamp, nonce)
}
+// newSignedGet 构造带 RIO 网关签名头与指定 Accept 头的 GET 请求。
+func (c *Client) newSignedGet(ctx context.Context, reqURL, accept string) (*http.Request, error) {
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
+ if err != nil {
+ return nil, err
+ }
+ for k, v := range c.GenerateRIOHeaders() {
+ req.Header.Set(k, v)
+ }
+ req.Header.Set("Accept", accept)
+ return req, nil
+}
+
// ExchangeCode 通过企业 SSO 认证网关将 code 换取员工信息。
// 遵循降级设计:当网关不可用、未配置凭证或鉴权出错时,记录日志并优雅降级为访客,不抛出阻断异常。
func (c *Client) ExchangeCode(ctx context.Context, code string) (*User, error) {
@@ -218,17 +231,12 @@ func (c *Client) ExchangeCode(ctx context.Context, code string) (*User, error) {
}
reqURL := fmt.Sprintf("%s%s?code=%s", c.cfg.AuthHost, c.cfg.AuthURI, url.QueryEscape(code))
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
+ req, err := c.newSignedGet(ctx, reqURL, "application/json")
if err != nil {
log.Printf("[auth] create request failed: %v, fallback to guest", err)
return FallbackGuestUser(), nil
}
- for k, v := range c.GenerateRIOHeaders() {
- req.Header.Set(k, v)
- }
- req.Header.Set("Accept", "application/json")
-
resp, err := c.httpClient.Do(req)
if err != nil {
log.Printf("[auth] request SSO endpoint failed: %v, fallback to guest", err)
diff --git a/internal/auth/photo.go b/internal/auth/photo.go
index 85305760..582e9e18 100644
--- a/internal/auth/photo.go
+++ b/internal/auth/photo.go
@@ -202,16 +202,11 @@ func (c *Client) FetchPhoto(ctx context.Context, rawUsername string, rawSpec int
}
reqURL := fmt.Sprintf("%s%s/%d/%s.png", c.cfg.AuthHost, photoPath, spec, url.PathEscape(username))
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
+ req, err := c.newSignedGet(ctx, reqURL, "image/png,image/*;q=0.8,*/*;q=0.5")
if err != nil {
return nil, "", fmt.Errorf("create photo request failed: %w", err)
}
- for k, v := range c.GenerateRIOHeaders() {
- req.Header.Set(k, v)
- }
- req.Header.Set("Accept", "image/png,image/*;q=0.8,*/*;q=0.5")
-
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, "", fmt.Errorf("request SSO photo failed: %w", err)
diff --git a/internal/auth/signed_get_test.go b/internal/auth/signed_get_test.go
new file mode 100644
index 00000000..43977850
--- /dev/null
+++ b/internal/auth/signed_get_test.go
@@ -0,0 +1,58 @@
+package auth
+
+import (
+ "context"
+ "net/http"
+ "testing"
+)
+
+func TestNewSignedGetSetsRIOAndAcceptHeaders(t *testing.T) {
+ c := NewClient(Config{PaaSID: "paas-1", AppToken: "token-1", AuthHost: "https://sso.example.com"})
+ req, err := c.newSignedGet(context.Background(), "https://sso.example.com/auth?code=x", "application/json")
+ if err != nil {
+ t.Fatalf("newSignedGet: %v", err)
+ }
+ if req.Method != http.MethodGet {
+ t.Fatalf("method = %q, want GET", req.Method)
+ }
+ if got := req.URL.String(); got != "https://sso.example.com/auth?code=x" {
+ t.Fatalf("url = %q", got)
+ }
+ if got := req.Header.Get("Accept"); got != "application/json" {
+ t.Fatalf("Accept = %q", got)
+ }
+ if got := req.Header.Get("x-rio-paasid"); got != "paas-1" {
+ t.Fatalf("x-rio-paasid = %q", got)
+ }
+ ts, nonce := req.Header.Get("x-rio-timestamp"), req.Header.Get("x-rio-nonce")
+ if ts == "" || len(nonce) != 4 {
+ t.Fatalf("timestamp=%q nonce=%q", ts, nonce)
+ }
+ want := GenerateRIOHeaders("paas-1", "token-1", ts, nonce)["x-rio-signature"]
+ if got := req.Header.Get("x-rio-signature"); got != want {
+ t.Fatalf("x-rio-signature = %q, want %q", got, want)
+ }
+}
+
+func TestNewSignedGetPropagatesContext(t *testing.T) {
+ type ctxKey struct{}
+ ctx := context.WithValue(context.Background(), ctxKey{}, "v")
+ c := NewClient(Config{PaaSID: "p", AppToken: "t"})
+ req, err := c.newSignedGet(ctx, "https://sso.example.com/photo/1/a.png", "image/png")
+ if err != nil {
+ t.Fatalf("newSignedGet: %v", err)
+ }
+ if req.Context().Value(ctxKey{}) != "v" {
+ t.Fatal("request context not propagated")
+ }
+ if got := req.Header.Get("Accept"); got != "image/png" {
+ t.Fatalf("Accept = %q", got)
+ }
+}
+
+func TestNewSignedGetInvalidURL(t *testing.T) {
+ c := NewClient(Config{PaaSID: "p", AppToken: "t"})
+ if _, err := c.newSignedGet(context.Background(), "://bad url", "application/json"); err == nil {
+ t.Fatal("expected error for invalid URL")
+ }
+}
diff --git a/internal/auth/signed_request_callers_test.go b/internal/auth/signed_request_callers_test.go
new file mode 100644
index 00000000..a61941a9
--- /dev/null
+++ b/internal/auth/signed_request_callers_test.go
@@ -0,0 +1,89 @@
+package auth
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+)
+
+// assertSignedRIORequest 校验请求带有正确的 RIO 网关签名头和期望的 Accept 头。
+func assertSignedRIORequest(t *testing.T, r *http.Request, paasID, token, wantAccept string) {
+ t.Helper()
+ if r.Method != http.MethodGet {
+ t.Errorf("method = %s, want GET", r.Method)
+ }
+ if got := r.Header.Get("Accept"); got != wantAccept {
+ t.Errorf("Accept = %q, want %q", got, wantAccept)
+ }
+ if got := r.Header.Get("x-rio-paasid"); got != paasID {
+ t.Errorf("x-rio-paasid = %q, want %q", got, paasID)
+ }
+ ts, nonce := r.Header.Get("x-rio-timestamp"), r.Header.Get("x-rio-nonce")
+ if ts == "" || nonce == "" {
+ t.Fatalf("missing timestamp/nonce: %q/%q", ts, nonce)
+ }
+ want := GenerateRIOHeaders(paasID, token, ts, nonce)["x-rio-signature"]
+ if got := r.Header.Get("x-rio-signature"); got != want {
+ t.Errorf("x-rio-signature = %q, want %q", got, want)
+ }
+}
+
+func TestExchangeCodeSendsSignedJSONRequest(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ assertSignedRIORequest(t, r, "paas_ex", "token_ex", "application/json")
+ _ = json.NewEncoder(w).Encode(IOAAuthResponse{Data: &IOAAuthData{StaffId: 7, LoginName: "alice"}})
+ }))
+ defer server.Close()
+
+ c := NewClient(Config{PaaSID: "paas_ex", AppToken: "token_ex", AuthHost: server.URL, AuthURI: "/auth"})
+ user, err := c.ExchangeCode(context.Background(), "code-1")
+ if err != nil {
+ t.Fatalf("ExchangeCode: %v", err)
+ }
+ if user.IsGuest || user.LoginName != "alice" {
+ t.Fatalf("user = %+v, want non-guest alice", user)
+ }
+}
+
+func TestFetchPhotoSendsSignedImageRequest(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ assertSignedRIORequest(t, r, "paas_ph", "token_ph", "image/png,image/*;q=0.8,*/*;q=0.5")
+ w.Header().Set("Content-Type", "image/png")
+ _, _ = w.Write([]byte("\x89PNG\r\n\x1a\n"))
+ }))
+ defer server.Close()
+
+ c := NewClient(Config{PaaSID: "paas_ph", AppToken: "token_ph", AuthHost: server.URL, PhotoURI: "/photo"})
+ if _, ct, err := c.FetchPhoto(context.Background(), "alice", 100); err != nil || ct != "image/png" {
+ t.Fatalf("FetchPhoto = ct %q err %v, want image/png nil", ct, err)
+ }
+}
+
+// 构造请求失败(非法 AuthHost)时:ExchangeCode 降级为访客,FetchPhoto 返回包装后的错误且不缓存。
+func TestSignedRequestBuildFailureAtCallers(t *testing.T) {
+ cfg := Config{PaaSID: "p", AppToken: "t", AuthHost: "http://bad host\x7f", AuthURI: "/auth", PhotoURI: "/photo"}
+ c := NewClient(cfg)
+
+ user, err := c.ExchangeCode(context.Background(), "code-1")
+ if err != nil {
+ t.Fatalf("ExchangeCode err = %v, want nil (guest fallback)", err)
+ }
+ if !user.IsGuest {
+ t.Fatalf("user = %+v, want guest fallback", user)
+ }
+
+ _, _, err = c.FetchPhoto(context.Background(), "alice", 100)
+ if err == nil || !strings.Contains(err.Error(), "create photo request failed") {
+ t.Fatalf("FetchPhoto err = %v, want create photo request failed", err)
+ }
+ if errors.Is(err, ErrPhotoNotFound) || errors.Is(err, ErrCredentialsNotConfigured) {
+ t.Fatalf("FetchPhoto err = %v, should not be a sentinel error", err)
+ }
+ if _, _, ok := c.photoCache.get("100:alice"); ok {
+ t.Fatal("failed request must not populate photo cache")
+ }
+}