Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication support for ntfy #890

Merged
merged 1 commit into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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