Skip to content

Commit

Permalink
Merge pull request #890 from crazy-max/ntfy-auth
Browse files Browse the repository at this point in the history
Authentication support for ntfy
  • Loading branch information
crazy-max authored Jun 11, 2023
2 parents ef09003 + c7993f2 commit fa7f290
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions docs/notif/ntfy.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ 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 |
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |

!!! 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`
Expand Down
2 changes: 2 additions & 0 deletions internal/model/notif_ntfy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
4 changes: 4 additions & 0 deletions internal/notif/ntfy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit fa7f290

Please sign in to comment.