From 3331670823dc1e7fd2aa3f1c8e43bc386c227397 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Wed, 6 Aug 2025 04:43:35 +0100 Subject: [PATCH] Add basic auth support to custom API --- internal/glance/widget-custom-api.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/glance/widget-custom-api.go b/internal/glance/widget-custom-api.go index 1cbbfc6..28a20e0 100644 --- a/internal/glance/widget-custom-api.go +++ b/internal/glance/widget-custom-api.go @@ -36,8 +36,12 @@ type CustomAPIRequest struct { Body any `yaml:"body"` MockResponse string `yaml:"mock-response"` SkipJSONValidation bool `yaml:"skip-json-validation"` - bodyReader io.ReadSeeker `yaml:"-"` - httpRequest *http.Request `yaml:"-"` + BasicAuth struct { + Username string `yaml:"username"` + Password string `yaml:"password"` + } `yaml:"basic-auth"` + bodyReader io.ReadSeeker `yaml:"-"` + httpRequest *http.Request `yaml:"-"` } type customAPIWidget struct { @@ -190,6 +194,10 @@ func (req *CustomAPIRequest) initialize() error { httpReq.Header.Add(key, value) } + if req.BasicAuth.Username != "" || req.BasicAuth.Password != "" { + httpReq.SetBasicAuth(req.BasicAuth.Username, req.BasicAuth.Password) + } + req.httpRequest = httpReq return nil @@ -708,6 +716,11 @@ var customAPITemplateFuncs = func() template.FuncMap { return req }, + "withBasicAuth": func(username, password string, req *CustomAPIRequest) *CustomAPIRequest { + req.BasicAuth.Username = username + req.BasicAuth.Password = password + return req + }, "getResponse": func(req *CustomAPIRequest) *customAPIResponseData { err := req.initialize() if err != nil {