Add WithAuthHeader for custom authentication header name

Support configuring a custom HTTP header name for authentication
(e.g., X-API-Auth instead of Authorization) for Kanboard servers
with specific proxy configurations that use alternative auth headers.

- Add headerName field to apiTokenAuth and basicAuth structs
- Add WithAuthHeader() fluent method to Client
- Auth methods pass custom header name when creating auth structs
- Add tests for custom header with API token, basic auth, and custom user

Closes kanboard-9wa

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Oliver Jakoubek 2026-01-23 18:26:55 +01:00
commit 192053952f
6 changed files with 691 additions and 15 deletions

33
auth.go
View file

@ -1,6 +1,9 @@
package kanboard
import "net/http"
import (
"encoding/base64"
"net/http"
)
// Authenticator applies authentication to HTTP requests.
type Authenticator interface {
@ -9,8 +12,9 @@ type Authenticator interface {
// apiTokenAuth implements API token authentication.
type apiTokenAuth struct {
user string
token string
user string
token string
headerName string
}
// Apply adds HTTP Basic Auth with the configured user (or "jsonrpc" if empty) and the API token.
@ -19,16 +23,31 @@ func (a *apiTokenAuth) Apply(req *http.Request) {
if user == "" {
user = "jsonrpc"
}
req.SetBasicAuth(user, a.token)
if a.headerName != "" {
req.Header.Set(a.headerName, "Basic "+basicAuthValue(user, a.token))
} else {
req.SetBasicAuth(user, a.token)
}
}
// basicAuth implements username/password authentication.
type basicAuth struct {
username string
password string
username string
password string
headerName string
}
// Apply adds HTTP Basic Auth with username and password.
func (a *basicAuth) Apply(req *http.Request) {
req.SetBasicAuth(a.username, a.password)
if a.headerName != "" {
req.Header.Set(a.headerName, "Basic "+basicAuthValue(a.username, a.password))
} else {
req.SetBasicAuth(a.username, a.password)
}
}
// basicAuthValue returns the base64-encoded value for HTTP Basic Auth.
func basicAuthValue(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}