From c7993f23b8b01bad55089448a40c513d5eee99e1 Mon Sep 17 00:00:00 2001
From: CrazyMax <crazy-max@users.noreply.github.com>
Date: Sun, 11 Jun 2023 22:26:27 +0200
Subject: [PATCH] Authentication support for ntfy

---
 docs/notif/ntfy.md            | 8 ++++++--
 internal/model/notif_ntfy.go  | 2 ++
 internal/notif/ntfy/client.go | 4 ++++
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/docs/notif/ntfy.md b/docs/notif/ntfy.md
index 55a61e2d..6c9832cf 100644
--- a/docs/notif/ntfy.md
+++ b/docs/notif/ntfy.md
@@ -20,10 +20,12 @@ Notifications can be sent using a [ntfy](https://ntfy.sh/) instance.
     ```
 
 | Name                | Default                             | Description                                                                |
-| ------------------- | ----------------------------------- | -------------------------------------------------------------------------- |
+|---------------------|-------------------------------------|----------------------------------------------------------------------------|
 | `endpoint`[^1]      | `https://ntfy.sh`                   | Ntfy base URL                                                              |
+| `token`             |                                     | [Access token](https://docs.ntfy.sh/publish/#access-tokens)                |
+| `tokenFile`         |                                     | Use content of secret file as acess token if `token` not defined           |
 | `topic`             |                                     | Ntfy topic                                                                 |
-| `priority`          | 3                          | The priority of the message                                                |
+| `priority`          | 3                                   | The priority of the message                                                |
 | `tags`              | `["package"]`                       | Emoji to go in your notiication                                            |
 | `timeout`           | `10s`                               | Timeout specifies a time limit for the request to be made                  |
 | `templateTitle`[^1] | See [below](#default-templatetitle) | [Notification template](../faq.md#notification-template) for message title |
@@ -31,6 +33,8 @@ Notifications can be sent using a [ntfy](https://ntfy.sh/) instance.
 
 !!! abstract "Environment variables"
     * `DIUN_NOTIF_NTFY_ENDPOINT`
+    * `DIUN_NOTIF_NTFY_TOKEN`
+    * `DIUN_NOTIF_NTFY_TOKENFILE`
     * `DIUN_NOTIF_NTFY_TOPIC`
     * `DIUN_NOTIF_NTFY_PRIORITY`
     * `DIUN_NOTIF_NTFY_TAGS`
diff --git a/internal/model/notif_ntfy.go b/internal/model/notif_ntfy.go
index f77f11ca..f2092703 100644
--- a/internal/model/notif_ntfy.go
+++ b/internal/model/notif_ntfy.go
@@ -9,6 +9,8 @@ import (
 // NotifNtfy holds ntfy notification configuration details
 type NotifNtfy struct {
 	Endpoint      string         `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"`
+	Token         string         `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
+	TokenFile     string         `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
 	Topic         string         `yaml:"topic,omitempty" json:"topic,omitempty" validate:"required"`
 	Priority      int            `yaml:"priority,omitempty" json:"priority,omitempty" validate:"omitempty,min=0"`
 	Tags          []string       `yaml:"tags,omitempty" json:"tags,omitempty" validate:"required"`
diff --git a/internal/notif/ntfy/client.go b/internal/notif/ntfy/client.go
index d23e30aa..c444bc9c 100644
--- a/internal/notif/ntfy/client.go
+++ b/internal/notif/ntfy/client.go
@@ -10,6 +10,7 @@ import (
 	"github.com/crazy-max/diun/v4/internal/model"
 	"github.com/crazy-max/diun/v4/internal/msg"
 	"github.com/crazy-max/diun/v4/internal/notif/notifier"
+	"github.com/crazy-max/diun/v4/pkg/utl"
 )
 
 // Client represents an active ntfy notification object
@@ -85,6 +86,9 @@ func (c *Client) Send(entry model.NotifEntry) error {
 		return err
 	}
 
+	if token, err := utl.GetSecret(c.cfg.Token, c.cfg.TokenFile); err == nil && token != "" {
+		req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
+	}
 	req.Header.Set("Content-Type", "application/json")
 	req.Header.Set("User-Agent", c.meta.UserAgent)