Add optional API user support for token authentication
- Add user field to apiTokenAuth struct - Add WithAPITokenUser(token, user) method for custom username - Default to "jsonrpc" when no user specified (backward compatible) - Add tests for custom user and empty user scenarios Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
ef7bd74e4a
commit
10e2ecf47e
4 changed files with 93 additions and 2 deletions
9
auth.go
9
auth.go
|
|
@ -9,12 +9,17 @@ type Authenticator interface {
|
|||
|
||||
// apiTokenAuth implements API token authentication.
|
||||
type apiTokenAuth struct {
|
||||
user string
|
||||
token string
|
||||
}
|
||||
|
||||
// Apply adds HTTP Basic Auth with username "jsonrpc" and the API token.
|
||||
// Apply adds HTTP Basic Auth with the configured user (or "jsonrpc" if empty) and the API token.
|
||||
func (a *apiTokenAuth) Apply(req *http.Request) {
|
||||
req.SetBasicAuth("jsonrpc", a.token)
|
||||
user := a.user
|
||||
if user == "" {
|
||||
user = "jsonrpc"
|
||||
}
|
||||
req.SetBasicAuth(user, a.token)
|
||||
}
|
||||
|
||||
// basicAuth implements username/password authentication.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue