From c6e0230e5d7bcb9d9cef538d99e4430e6f44aa41 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Wed, 26 Mar 2025 17:59:41 +0000 Subject: [PATCH] Allow sending headers in extension widget --- docs/configuration.md | 9 +++++++++ internal/glance/widget-extension.go | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 4d42c08..15ee95f 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1413,6 +1413,7 @@ Display a widget provided by an external source (3rd party). If you want to lear | url | string | yes | | | fallback-content-type | string | no | | | allow-potentially-dangerous-html | boolean | no | false | +| headers | key & value | no | | | parameters | key & value | no | | ##### `url` @@ -1421,6 +1422,14 @@ The URL of the extension. **Note that the query gets stripped from this URL and ##### `fallback-content-type` Optionally specify the fallback content type of the extension if the URL does not return a valid `Widget-Content-Type` header. Currently the only supported value for this property is `html`. +##### `headers` +Optionally specify the headers that will be sent with the request. Example: + +```yaml +headers: + x-api-key: ${SECRET_KEY} +``` + ##### `allow-potentially-dangerous-html` Whether to allow the extension to display HTML. diff --git a/internal/glance/widget-extension.go b/internal/glance/widget-extension.go index c044a8a..3732eb8 100644 --- a/internal/glance/widget-extension.go +++ b/internal/glance/widget-extension.go @@ -22,6 +22,7 @@ type extensionWidget struct { URL string `yaml:"url"` FallbackContentType string `yaml:"fallback-content-type"` Parameters queryParametersField `yaml:"parameters"` + Headers map[string]string `yaml:"headers"` AllowHtml bool `yaml:"allow-potentially-dangerous-html"` Extension extension `yaml:"-"` cachedHTML template.HTML `yaml:"-"` @@ -46,6 +47,7 @@ func (widget *extensionWidget) update(ctx context.Context) { URL: widget.URL, FallbackContentType: widget.FallbackContentType, Parameters: widget.Parameters, + Headers: widget.Headers, AllowHtml: widget.AllowHtml, }) @@ -85,6 +87,7 @@ type extensionRequestOptions struct { URL string `yaml:"url"` FallbackContentType string `yaml:"fallback-content-type"` Parameters queryParametersField `yaml:"parameters"` + Headers map[string]string `yaml:"headers"` AllowHtml bool `yaml:"allow-potentially-dangerous-html"` } @@ -113,6 +116,10 @@ func fetchExtension(options extensionRequestOptions) (extension, error) { request.URL.RawQuery = options.Parameters.toQueryString() } + for key, value := range options.Headers { + request.Header.Add(key, value) + } + response, err := http.DefaultClient.Do(request) if err != nil { slog.Error("Failed fetching extension", "url", options.URL, "error", err)