| 1 | package pushover |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | |
| 7 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 8 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 9 | ) |
| 10 | |
| 11 | // Config for the Pushover notification service. |
| 12 | type Config struct { |
| 13 | Token string `desc:"API Token/Key" url:"pass"` |
| 14 | User string `desc:"User Key" url:"host"` |
| 15 | Devices []string ` key:"devices" optional:""` |
| 16 | Priority int8 ` key:"priority" default:"0"` |
| 17 | Title string ` key:"title" optional:""` |
| 18 | EncryptionKey string `desc:"256-bit AES key as 64 hex characters for end-to-end encryption" key:"encryptionkey,key" optional:""` |
| 19 | } |
| 20 | |
| 21 | const ( |
| 22 | // Scheme is the identifying part of this service's configuration URL. |
| 23 | Scheme = "pushover" |
| 24 | |
| 25 | // redactedPlaceholder replaces secrets when a Pushover URL is masked. |
| 26 | redactedPlaceholder = "REDACTED" |
| 27 | ) |
| 28 | |
| 29 | // Enums returns the fields that should use a corresponding EnumFormatter to Print/Parse their values. |
| 30 | func (c *Config) Enums() map[string]types.EnumFormatter { |
| 31 | return map[string]types.EnumFormatter{} |
| 32 | } |
| 33 | |
| 34 | // GetURL returns a URL representation of its current field values. |
| 35 | func (c *Config) GetURL() *url.URL { |
| 36 | resolver := format.NewPropKeyResolver(c) |
| 37 | |
| 38 | return c.getURL(&resolver) |
| 39 | } |
| 40 | |
| 41 | // SetURL updates the Config from a URL representation of its field values. |
| 42 | func (c *Config) SetURL(serviceURL *url.URL) error { |
| 43 | resolver := format.NewPropKeyResolver(c) |
| 44 | |
| 45 | return c.setURL(&resolver, serviceURL) |
| 46 | } |
| 47 | |
| 48 | // getURL constructs a URL from the Config's fields using the provided resolver. |
| 49 | func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 50 | return &url.URL{ |
| 51 | User: url.UserPassword("Token", c.Token), |
| 52 | Host: c.User, |
| 53 | Scheme: Scheme, |
| 54 | ForceQuery: true, |
| 55 | RawQuery: format.BuildQuery(resolver), |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // setURL updates the Config from a URL using the provided resolver. |
| 60 | func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 61 | password, _ := serviceURL.User.Password() |
| 62 | c.User = serviceURL.Host |
| 63 | c.Token = password |
| 64 | |
| 65 | for key, vals := range serviceURL.Query() { |
| 66 | if err := resolver.Set(key, vals[0]); err != nil { |
| 67 | return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if _, err := parseEncryptionKey(c.EncryptionKey); err != nil { |
| 72 | return err |
| 73 | } |
| 74 | |
| 75 | if serviceURL.String() != "pushover://[email protected]" { |
| 76 | if len(c.User) < 1 { |
| 77 | return ErrUserMissing |
| 78 | } |
| 79 | |
| 80 | if len(c.Token) < 1 { |
| 81 | return ErrTokenMissing |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | // MaskURL redacts the API token in URL userinfo and sensitive query parameters. |
| 89 | // |
| 90 | // Parameters: |
| 91 | // - parsedURL: The Pushover URL to modify. |
| 92 | func MaskURL(parsedURL *url.URL) { |
| 93 | if parsedURL == nil { |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | if parsedURL.User != nil { |
| 98 | parsedURL.User = url.UserPassword(parsedURL.User.Username(), redactedPlaceholder) |
| 99 | } |
| 100 | |
| 101 | queryParams := parsedURL.Query() |
| 102 | if queryParams.Get("token") != "" { |
| 103 | queryParams.Set("token", redactedPlaceholder) |
| 104 | } |
| 105 | |
| 106 | if queryParams.Get("user") != "" { |
| 107 | queryParams.Set("user", redactedPlaceholder) |
| 108 | } |
| 109 | |
| 110 | if queryParams.Get("encryptionkey") != "" { |
| 111 | queryParams.Set("encryptionkey", redactedPlaceholder) |
| 112 | } |
| 113 | |
| 114 | if queryParams.Get("key") != "" { |
| 115 | queryParams.Set("key", redactedPlaceholder) |
| 116 | } |
| 117 | |
| 118 | parsedURL.RawQuery = queryParams.Encode() |
| 119 | } |