Skip to content

Commit

Permalink
Merge pull request #84 from blinklabs-io/feat-basic-auth
Browse files Browse the repository at this point in the history
feat: add basic auth support
  • Loading branch information
verbotenj authored Sep 22, 2023
2 parents 4bcf242 + 1b91f17 commit 5dbd706
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
8 changes: 8 additions & 0 deletions output/webhook/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ func WithUrl(url string) WebhookOptionFunc {
o.url = url
}
}

// WithBasicAuth specifies the username and password
func WithBasicAuth(username, password string) WebhookOptionFunc {
return func(o *WebhookOutput) {
o.username = username
o.password = password
}
}
19 changes: 18 additions & 1 deletion output/webhook/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
)

var cmdlineOptions struct {
url string
url string
username string
password string
}

func init() {
Expand All @@ -37,6 +39,20 @@ func init() {
DefaultValue: "http://localhost:3000",
Dest: &(cmdlineOptions.url),
},
{
Name: "username",
Type: plugin.PluginOptionTypeString,
Description: "specifies the username for basic auth",
DefaultValue: "",
Dest: &(cmdlineOptions.username),
},
{
Name: "password",
Type: plugin.PluginOptionTypeString,
Description: "specifies the password for basic auth",
DefaultValue: "",
Dest: &(cmdlineOptions.password),
},
},
},
)
Expand All @@ -45,6 +61,7 @@ func init() {
func NewFromCmdlineOptions() plugin.Plugin {
p := New(
WithUrl(cmdlineOptions.url),
WithBasicAuth(cmdlineOptions.username, cmdlineOptions.password),
)
return p
}
35 changes: 24 additions & 11 deletions output/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io"
Expand All @@ -34,6 +35,8 @@ type WebhookOutput struct {
errorChan chan error
eventChan chan event.Event
url string
username string
password string
}

func New(options ...WebhookOptionFunc) *WebhookOutput {
Expand Down Expand Up @@ -78,7 +81,7 @@ func (w *WebhookOutput) Start() error {
return
}
// TODO: error handle
err := SendWebhook(&evt, w.url)
err := w.SendWebhook(&evt)
if err != nil {
logger.Errorf("ERROR: %s", err)
}
Expand All @@ -87,32 +90,42 @@ func (w *WebhookOutput) Start() error {
return nil
}

func SendWebhook(e *event.Event, url string) error {
func basicAuth(username, password string) string {
auth := username + ":" + password
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}

func (w *WebhookOutput) SendWebhook(e *event.Event) error {
logger := logging.GetLogger()
logger.Infof("sending event %s to %s", e.Type, url)
logger.Infof("sending event %s to %s", e.Type, w.url)
data, err := json.Marshal(e)
if err != nil {
return fmt.Errorf("%s", err)
}
// Setup request
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(data))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, w.url, bytes.NewReader(data))
if err != nil {
return fmt.Errorf("%s", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", fmt.Sprintf("Snek/%s", version.GetVersionString()))

// Setup authorization
if w.username != "" && w.password != "" {
req.Header.Add("Authorization", basicAuth(w.username, w.password))
}
// Setup custom transport to ignore self-signed SSL
defaultTransport := http.DefaultTransport.(*http.Transport)
customTransport := &http.Transport{
Proxy: defaultTransport.Proxy,
DialContext: defaultTransport.DialContext,
MaxIdleConns: defaultTransport.MaxIdleConns,
IdleConnTimeout: defaultTransport.IdleConnTimeout,
Proxy: defaultTransport.Proxy,
DialContext: defaultTransport.DialContext,
MaxIdleConns: defaultTransport.MaxIdleConns,
IdleConnTimeout: defaultTransport.IdleConnTimeout,
ExpectContinueTimeout: defaultTransport.ExpectContinueTimeout,
TLSHandshakeTimeout: defaultTransport.TLSHandshakeTimeout,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSHandshakeTimeout: defaultTransport.TLSHandshakeTimeout,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: customTransport}
// Send payload
Expand All @@ -127,7 +140,7 @@ func SendWebhook(e *event.Event, url string) error {
defer resp.Body.Close()

logger.Infof("sent: %s, payload: %s, body: %s, response: %s, status: %d",
url,
w.url,
string(data),
string(respBody),
resp.Status,
Expand Down

0 comments on commit 5dbd706

Please sign in to comment.