Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
node: fix device flow polling against dex's non-RFC 401 responses
Antonio Ojea994d082a7d24
diff --git a/internal/node/oidc.go b/internal/node/oidc.goindex b06deab6..511e967a 100644--- a/internal/node/oidc.go+++ b/internal/node/oidc.go@@ -383,6 +383,19 @@ func stdinIsInteractive() bool {return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)}+// bodySnippet renders a response body for error messages, bounded so a+// misbehaving server can't flood logs.+func bodySnippet(body []byte) string {+ s := strings.TrimSpace(string(body))+ if s == "" {+ return "(empty response body)"+ }+ if len(s) > 256 {+ s = s[:256] + "..."+ }+ return s+}+// DeviceLogin performs OAuth 2.0 Device Authorization Grant (RFC 8628).func (n *SamNode) DeviceLogin(ctx context.Context, deviceAuthURL, tokenURL, clientID, audience string, requestRefresh bool) (string, error) {if deviceAuthURL == "" {@@ -546,16 +559,23 @@ func (n *SamNode) DeviceLogin(ctx context.Context, deviceAuthURL, tokenURL, cliereturn jwt, nil}- if tokenResp.StatusCode != http.StatusBadRequest {- return "", fmt.Errorf("token request failed with status: %s", tokenResp.Status)- }-var errResp struct {Error string `json:"error"`ErrorDescription string `json:"error_description"`}- if err := json.Unmarshal(body, &errResp); err != nil {- return "", fmt.Errorf("failed to decode token polling error response: %w", err)+ _ = json.Unmarshal(body, &errResp)+ pending := errResp.Error == "authorization_pending" || errResp.Error == "slow_down"++ // RFC 8628 §3.5 delivers polling errors as HTTP 400 with an OAuth error+ // body. Known exception: dex returns the pending/slow_down signals with+ // HTTP 401. Anything else is a real failure and is surfaced verbatim.+ rfcError := tokenResp.StatusCode == http.StatusBadRequest && errResp.Error != ""+ dexPending := tokenResp.StatusCode == http.StatusUnauthorized && pending+ if !rfcError && !dexPending {+ if errResp.Error != "" {+ return "", fmt.Errorf("token request failed with status %s: %s - %s", tokenResp.Status, errResp.Error, errResp.ErrorDescription)+ }+ return "", fmt.Errorf("token request failed with status %s: %s", tokenResp.Status, bodySnippet(body))}switch errResp.Error {diff --git a/internal/node/oidc_test.go b/internal/node/oidc_test.goindex 14a07650..23fa2598 100644--- a/internal/node/oidc_test.go+++ b/internal/node/oidc_test.go@@ -510,6 +510,106 @@ func TestDeviceLoginRetriesOnTransientTokenError(t *testing.T) {}}+// TestDeviceLoginPending401 verifies polling survives providers (e.g. dex)+// that return authorization_pending with HTTP 401 instead of the+// RFC 8628-mandated 400: the OAuth error code in the body wins over the+// HTTP status code.+func TestDeviceLoginPending401(t *testing.T) {+ mux := http.NewServeMux()+ mux.HandleFunc("/device", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "device_code": "dev_code_1",+ "user_code": "CCCC-DDDD",+ "verification_uri": "https://example.com/device",+ "expires_in": 60,+ "interval": 1,+ })+ })++ var pollCount int32+ mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ if atomic.AddInt32(&pollCount, 1) == 1 {+ w.WriteHeader(http.StatusUnauthorized)+ _ = json.NewEncoder(w).Encode(map[string]string{"error": "authorization_pending"})+ return+ }+ _ = json.NewEncoder(w).Encode(map[string]string{"id_token": "token_after_pending_401"})+ })++ server := httptest.NewServer(mux)+ defer server.Close()++ node := &SamNode{}+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)+ defer cancel()++ token, err := node.DeviceLogin(ctx, server.URL+"/device", server.URL+"/token", "client_id_test", "sam-e2e", false)+ if err != nil {+ t.Fatalf("DeviceLogin failed: %v", err)+ }+ if token != "token_after_pending_401" {+ t.Fatalf("Expected token_after_pending_401, got %q", token)+ }+ if got := atomic.LoadInt32(&pollCount); got < 2 {+ t.Fatalf("expected at least 2 poll attempts, got %d", got)+ }+}++// TestDeviceLoginFatalErrors verifies polling aborts on terminal responses:+// a non-OAuth error body (regardless of status) and an OAuth error code+// that is not a pending/slow_down signal.+func TestDeviceLoginFatalErrors(t *testing.T) {+ cases := []struct {+ name string+ status int+ body string+ wantErr string+ }{+ {"non-oauth 401", http.StatusUnauthorized, `{"message":"nope"}`, "token request failed with status"},+ {"invalid_client 401", http.StatusUnauthorized, `{"error":"invalid_client","error_description":"unknown client"}`, "invalid_client"},+ {"access_denied 400", http.StatusBadRequest, `{"error":"access_denied"}`, "denied"},+ {"oauth-shaped 500 is not a protocol error", http.StatusInternalServerError, `{"error":"server_error"}`, "server_error"},+ {"html 502 surfaces the body", http.StatusBadGateway, `<html>bad gateway</html>`, "bad gateway"},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ mux := http.NewServeMux()+ mux.HandleFunc("/device", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "device_code": "dev_code_1",+ "user_code": "EEEE-FFFF",+ "verification_uri": "https://example.com/device",+ "expires_in": 60,+ "interval": 1,+ })+ })+ mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ w.WriteHeader(tc.status)+ _, _ = w.Write([]byte(tc.body))+ })++ server := httptest.NewServer(mux)+ defer server.Close()++ node := &SamNode{}+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)+ defer cancel()++ _, err := node.DeviceLogin(ctx, server.URL+"/device", server.URL+"/token", "client_id_test", "sam-e2e", false)+ if err == nil {+ t.Fatal("expected DeviceLogin to fail")+ }+ if !strings.Contains(err.Error(), tc.wantErr) {+ t.Fatalf("expected error containing %q, got %v", tc.wantErr, err)+ }+ })+ }+}+func TestParseAuthMode(t *testing.T) {cases := []struct {in string