From 138b9b79dbb679204f1c4d7b9f74cfdc3d29dd96 Mon Sep 17 00:00:00 2001 From: griefie <36406769+griefie@users.noreply.github.com> Date: Thu, 18 Dec 2025 09:41:44 +0100 Subject: [PATCH] Ntfy: support auth tokens (#25856) --- .../Config/defaultYaml/messaging.yaml | 1 + evcc.dist.yaml | 1 + push/ntfy.go | 35 ++++++++++++++++--- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/assets/js/components/Config/defaultYaml/messaging.yaml b/assets/js/components/Config/defaultYaml/messaging.yaml index cfc4da90b..43cd92e45 100644 --- a/assets/js/components/Config/defaultYaml/messaging.yaml +++ b/assets/js/components/Config/defaultYaml/messaging.yaml @@ -34,5 +34,6 @@ # uri: smtp://:@:/?fromAddress=&toAddresses= #- type: ntfy # uri: https:/// +# authtoken: # priority: # tags: diff --git a/evcc.dist.yaml b/evcc.dist.yaml index 0d6f2efc3..3f8764609 100644 --- a/evcc.dist.yaml +++ b/evcc.dist.yaml @@ -228,5 +228,6 @@ messaging: # uri: smtp://:@:/?fromAddress=&toAddresses= # - type: ntfy # uri: https:/// + # authtoken: # priority: # tags: diff --git a/push/ntfy.go b/push/ntfy.go index d2a1331cf..d45b149c4 100644 --- a/push/ntfy.go +++ b/push/ntfy.go @@ -1,8 +1,11 @@ package push import ( + "encoding/base64" "errors" + "fmt" "net/http" + "net/url" "strings" "github.com/evcc-io/evcc/util" @@ -24,9 +27,10 @@ type Ntfy struct { // NewNtfyFromConfig creates new Ntfy messenger func NewNtfyFromConfig(other map[string]any) (Messenger, error) { var cc struct { - URI string - Priority string - Tags string + URI string + Priority string + Tags string + AuthToken string } if err := util.DecodeOther(other, &cc); err != nil { @@ -37,8 +41,31 @@ func NewNtfyFromConfig(other map[string]any) (Messenger, error) { return nil, errors.New("missing uri") } + u, err := url.Parse(cc.URI) + if err != nil { + return nil, err + } + log := util.NewLogger("ntfy") - if token, ok := strings.CutPrefix(cc.URI, "https://ntfy.sh/"); ok { + + if cc.AuthToken != "" { + bearer := "Bearer " + cc.AuthToken + encoded := base64.RawStdEncoding.EncodeToString([]byte(bearer)) + + q := u.Query() + if q.Has("auth") { + return nil, fmt.Errorf("uri already contains auth parameter") + } + + q.Set("auth", encoded) + u.RawQuery = q.Encode() + + cc.URI = u.String() + + log = log.Redact(cc.AuthToken, bearer, encoded) + } + + if token, ok := strings.CutPrefix(u.String(), "https://ntfy.sh/"); ok { log = log.Redact(token) }