2026-01-15 18:10:35 +01:00
|
|
|
package kanboard
|
|
|
|
|
|
2026-01-23 18:26:55 +01:00
|
|
|
import (
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
2026-01-15 18:10:35 +01:00
|
|
|
|
|
|
|
|
// Authenticator applies authentication to HTTP requests.
|
|
|
|
|
type Authenticator interface {
|
|
|
|
|
Apply(req *http.Request)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// apiTokenAuth implements API token authentication.
|
|
|
|
|
type apiTokenAuth struct {
|
2026-01-23 18:26:55 +01:00
|
|
|
user string
|
|
|
|
|
token string
|
|
|
|
|
headerName string
|
2026-01-15 18:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-23 17:55:31 +01:00
|
|
|
// Apply adds HTTP Basic Auth with the configured user (or "jsonrpc" if empty) and the API token.
|
2026-01-15 18:10:35 +01:00
|
|
|
func (a *apiTokenAuth) Apply(req *http.Request) {
|
2026-01-23 17:55:31 +01:00
|
|
|
user := a.user
|
|
|
|
|
if user == "" {
|
|
|
|
|
user = "jsonrpc"
|
|
|
|
|
}
|
2026-01-23 18:26:55 +01:00
|
|
|
if a.headerName != "" {
|
2026-01-27 11:01:28 +01:00
|
|
|
req.Header.Set(a.headerName, basicAuthValue(user, a.token))
|
2026-01-23 18:26:55 +01:00
|
|
|
} else {
|
|
|
|
|
req.SetBasicAuth(user, a.token)
|
|
|
|
|
}
|
2026-01-15 18:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// basicAuth implements username/password authentication.
|
|
|
|
|
type basicAuth struct {
|
2026-01-23 18:26:55 +01:00
|
|
|
username string
|
|
|
|
|
password string
|
|
|
|
|
headerName string
|
2026-01-15 18:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply adds HTTP Basic Auth with username and password.
|
|
|
|
|
func (a *basicAuth) Apply(req *http.Request) {
|
2026-01-23 18:26:55 +01:00
|
|
|
if a.headerName != "" {
|
2026-01-27 11:01:28 +01:00
|
|
|
req.Header.Set(a.headerName, basicAuthValue(a.username, a.password))
|
2026-01-23 18:26:55 +01:00
|
|
|
} 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))
|
2026-01-15 18:10:35 +01:00
|
|
|
}
|