Skip to content

Commit 5dbd706

Browse files
authored
Merge pull request #84 from blinklabs-io/feat-basic-auth
feat: add basic auth support
2 parents 4bcf242 + 1b91f17 commit 5dbd706

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

output/webhook/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ func WithUrl(url string) WebhookOptionFunc {
2424
o.url = url
2525
}
2626
}
27+
28+
// WithBasicAuth specifies the username and password
29+
func WithBasicAuth(username, password string) WebhookOptionFunc {
30+
return func(o *WebhookOutput) {
31+
o.username = username
32+
o.password = password
33+
}
34+
}

output/webhook/plugin.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import (
1919
)
2020

2121
var cmdlineOptions struct {
22-
url string
22+
url string
23+
username string
24+
password string
2325
}
2426

2527
func init() {
@@ -37,6 +39,20 @@ func init() {
3739
DefaultValue: "http://localhost:3000",
3840
Dest: &(cmdlineOptions.url),
3941
},
42+
{
43+
Name: "username",
44+
Type: plugin.PluginOptionTypeString,
45+
Description: "specifies the username for basic auth",
46+
DefaultValue: "",
47+
Dest: &(cmdlineOptions.username),
48+
},
49+
{
50+
Name: "password",
51+
Type: plugin.PluginOptionTypeString,
52+
Description: "specifies the password for basic auth",
53+
DefaultValue: "",
54+
Dest: &(cmdlineOptions.password),
55+
},
4056
},
4157
},
4258
)
@@ -45,6 +61,7 @@ func init() {
4561
func NewFromCmdlineOptions() plugin.Plugin {
4662
p := New(
4763
WithUrl(cmdlineOptions.url),
64+
WithBasicAuth(cmdlineOptions.username, cmdlineOptions.password),
4865
)
4966
return p
5067
}

output/webhook/webhook.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"context"
2020
"crypto/tls"
21+
"encoding/base64"
2122
"encoding/json"
2223
"fmt"
2324
"io"
@@ -34,6 +35,8 @@ type WebhookOutput struct {
3435
errorChan chan error
3536
eventChan chan event.Event
3637
url string
38+
username string
39+
password string
3740
}
3841

3942
func New(options ...WebhookOptionFunc) *WebhookOutput {
@@ -78,7 +81,7 @@ func (w *WebhookOutput) Start() error {
7881
return
7982
}
8083
// TODO: error handle
81-
err := SendWebhook(&evt, w.url)
84+
err := w.SendWebhook(&evt)
8285
if err != nil {
8386
logger.Errorf("ERROR: %s", err)
8487
}
@@ -87,32 +90,42 @@ func (w *WebhookOutput) Start() error {
8790
return nil
8891
}
8992

90-
func SendWebhook(e *event.Event, url string) error {
93+
func basicAuth(username, password string) string {
94+
auth := username + ":" + password
95+
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
96+
}
97+
98+
func (w *WebhookOutput) SendWebhook(e *event.Event) error {
9199
logger := logging.GetLogger()
92-
logger.Infof("sending event %s to %s", e.Type, url)
100+
logger.Infof("sending event %s to %s", e.Type, w.url)
93101
data, err := json.Marshal(e)
94102
if err != nil {
95103
return fmt.Errorf("%s", err)
96104
}
97105
// Setup request
98106
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
99107
defer cancel()
100-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(data))
108+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, w.url, bytes.NewReader(data))
101109
if err != nil {
102110
return fmt.Errorf("%s", err)
103111
}
104112
req.Header.Add("Content-Type", "application/json")
105113
req.Header.Add("User-Agent", fmt.Sprintf("Snek/%s", version.GetVersionString()))
114+
115+
// Setup authorization
116+
if w.username != "" && w.password != "" {
117+
req.Header.Add("Authorization", basicAuth(w.username, w.password))
118+
}
106119
// Setup custom transport to ignore self-signed SSL
107120
defaultTransport := http.DefaultTransport.(*http.Transport)
108121
customTransport := &http.Transport{
109-
Proxy: defaultTransport.Proxy,
110-
DialContext: defaultTransport.DialContext,
111-
MaxIdleConns: defaultTransport.MaxIdleConns,
112-
IdleConnTimeout: defaultTransport.IdleConnTimeout,
122+
Proxy: defaultTransport.Proxy,
123+
DialContext: defaultTransport.DialContext,
124+
MaxIdleConns: defaultTransport.MaxIdleConns,
125+
IdleConnTimeout: defaultTransport.IdleConnTimeout,
113126
ExpectContinueTimeout: defaultTransport.ExpectContinueTimeout,
114-
TLSHandshakeTimeout: defaultTransport.TLSHandshakeTimeout,
115-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
127+
TLSHandshakeTimeout: defaultTransport.TLSHandshakeTimeout,
128+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
116129
}
117130
client := &http.Client{Transport: customTransport}
118131
// Send payload
@@ -127,7 +140,7 @@ func SendWebhook(e *event.Event, url string) error {
127140
defer resp.Body.Close()
128141

129142
logger.Infof("sent: %s, payload: %s, body: %s, response: %s, status: %d",
130-
url,
143+
w.url,
131144
string(data),
132145
string(respBody),
133146
resp.Status,

0 commit comments

Comments
 (0)