/
teams_config.go
160 lines (135 loc) · 4.23 KB
/
teams_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package teams
import (
"fmt"
"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/types"
"net/url"
"regexp"
"strings"
"github.com/containrrr/shoutrrr/pkg/services/standard"
)
// Config for use within the teams plugin
type Config struct {
standard.EnumlessConfig
Group string `url:"user" optional:""`
Tenant string `url:"host" optional:""`
AltID string `url:"path1" optional:""`
GroupOwner string `url:"path2" optional:""`
Title string `key:"title" optional:""`
Color string `key:"color" optional:""`
Host string `key:"host" optional:"" default:"outlook.office.com"`
}
func (config *Config) webhookParts() [4]string {
return [4]string{config.Group, config.Tenant, config.AltID, config.GroupOwner}
}
// SetFromWebhookURL updates the config WebhookParts from a teams webhook URL
func (config *Config) SetFromWebhookURL(webhookURL string) error {
parts, err := parseAndVerifyWebhookURL(webhookURL)
if err != nil {
return err
}
config.setFromWebhookParts(parts)
return nil
}
// ConfigFromWebhookURL creates a new Config from a parsed Teams Webhook URL
func ConfigFromWebhookURL(webhookURL url.URL) (*Config, error) {
config := &Config{
Host: webhookURL.Host,
}
if err := config.SetFromWebhookURL(webhookURL.String()); err != nil {
return nil, err
}
return config, nil
}
// GetURL returns a URL representation of it's current field values
func (config *Config) GetURL() *url.URL {
resolver := format.NewPropKeyResolver(config)
return config.getURL(&resolver)
}
// SetURL updates a ServiceConfig from a URL representation of it's field values
func (config *Config) SetURL(url *url.URL) error {
resolver := format.NewPropKeyResolver(config)
return config.setURL(&resolver, url)
}
func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
return &url.URL{
User: url.User(config.Group),
Host: config.Tenant,
Path: "/" + config.AltID + "/" + config.GroupOwner,
Scheme: Scheme,
ForceQuery: false,
RawQuery: format.BuildQuery(resolver),
}
}
func (config *Config) setURL(resolver types.ConfigQueryResolver, url *url.URL) error {
var webhookParts [4]string
if pass, legacyFormat := url.User.Password(); legacyFormat {
parts := strings.Split(url.User.Username(), "@")
if len(parts) != 2 {
return fmt.Errorf("invalid URL format")
}
webhookParts = [4]string{parts[0], parts[1], pass, url.Hostname()}
} else {
parts := strings.Split(url.Path, "/")
if parts[0] == "" {
parts = parts[1:]
}
webhookParts = [4]string{url.User.Username(), url.Hostname(), parts[0], parts[1]}
}
if err := verifyWebhookParts(webhookParts); err != nil {
return fmt.Errorf("invalid URL format: %w", err)
}
config.setFromWebhookParts(webhookParts)
for key, vals := range url.Query() {
if err := resolver.Set(key, vals[0]); err != nil {
return err
}
}
return nil
}
func (config *Config) setFromWebhookParts(parts [4]string) {
config.Group = parts[0]
config.Tenant = parts[1]
config.AltID = parts[2]
config.GroupOwner = parts[3]
}
func buildWebhookURL(host, group, tenant, altID, groupOwner string) string {
// config.Group, config.Tenant, config.AltID, config.GroupOwner
path := Path
if host == LegacyHost {
path = LegacyPath
}
return fmt.Sprintf(
"https://%s/%s/%s@%s/%s/%s/%s",
host,
path,
group,
tenant,
ProviderName,
altID,
groupOwner)
}
func parseAndVerifyWebhookURL(webhookURL string) (parts [4]string, err error) {
pattern, err := regexp.Compile(`([0-9a-f-]{36})@([0-9a-f-]{36})/[^/]+/([0-9a-f]{32})/([0-9a-f-]{36})`)
if err != nil {
return parts, err
}
groups := pattern.FindStringSubmatch(webhookURL)
if len(groups) != 5 {
return parts, fmt.Errorf("invalid webhook URL format")
}
copy(parts[:], groups[1:])
return parts, nil
}
const (
// Scheme is the identifying part of this service's configuration URL
Scheme = "teams"
// LegacyHost is the default host for legacy webhook requests
LegacyHost = "outlook.office.com"
// LegacyPath is the initial path of the webhook URL for legacy webhook requests
LegacyPath = "webhook"
// Path is the initial path of the webhook URL for domain-scoped webhook requests
Path = "webhookb2"
// ProviderName is the name of the Teams integration provider
ProviderName = "IncomingWebhook"
)