fix: normalize API token format in client

Accept tokens both with and without PVEAPIToken= prefix,
since token files may contain the full Authorization header value.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Davíð Steinn Geirsson 2026-03-20 11:40:07 +00:00
parent 56fe551700
commit 2bdb508672

View file

@ -6,6 +6,7 @@ import (
"io" "io"
"net" "net"
"net/http" "net/http"
"strings"
"sync" "sync"
"time" "time"
) )
@ -40,6 +41,11 @@ func NewClient(hosts []string, token string, tlsInsecure bool, maxConcurrent int
TLSHandshakeTimeout: 5 * time.Second, TLSHandshakeTimeout: 5 * time.Second,
} }
// Normalize token: ensure it has the PVEAPIToken= prefix
if !strings.HasPrefix(token, "PVEAPIToken=") {
token = "PVEAPIToken=" + token
}
return &Client{ return &Client{
hosts: hosts, hosts: hosts,
token: token, token: token,
@ -78,7 +84,7 @@ func (c *Client) Get(path string) ([]byte, error) {
lastErr = fmt.Errorf("creating request for %s: %w", url, err) lastErr = fmt.Errorf("creating request for %s: %w", url, err)
continue continue
} }
req.Header.Set("Authorization", "PVEAPIToken="+c.token) req.Header.Set("Authorization", c.token)
resp, err := c.httpClient.Do(req) resp, err := c.httpClient.Do(req)
if err != nil { if err != nil {