Skip to content

Commit

Permalink
Merge pull request #19 from kallisti5/master
Browse files Browse the repository at this point in the history
http/generic: Implement basic http auth via token
  • Loading branch information
russss authored Dec 12, 2019
2 parents 2c9ce41 + 491b6f7 commit 0ed531f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,18 @@ of netcat, with `-d @-` to read POST data from stdin, like so:

echo "Hello world" | curl -d @- http://irccat-host/send

Everything that works via netcat also works by POST to `/send`. Note that this endpoint
is unauthenticated.
### Generic HTTP Endpoint with authentication

```json
"generic": {
"secret": "my_secret"
}
```

Adding an optional secret allows you to require a single secret token before sending
messages to the specified channels. (Using HTTPS is recommended to ensure key security)

echo "Hello world" | curl -H "Authorization: Bearer my_secret" -d @- http://irccat-host/send

### Grafana Webhook
```json
Expand Down
14 changes: 14 additions & 0 deletions httplistener/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package httplistener

import (
"bytes"
"fmt"
"github.com/irccloud/irccat/dispatcher"
"github.com/spf13/viper"
"net/http"
)

Expand All @@ -17,6 +19,18 @@ func (hl *HTTPListener) genericHandler(w http.ResponseWriter, request *http.Requ
return
}

// Optional simple auth via token
secret := viper.GetString("http.listeners.generic.secret")
if secret != "" {
auth := request.Header.Get("Authorization")
expecting := fmt.Sprintf("Bearer %s", secret)
if auth != expecting {
http.Error(w, "Invalid Authorization", http.StatusUnauthorized)
log.Warningf("%s - Invalid Authorization!", request.RemoteAddr)
return
}
}

body := new(bytes.Buffer)
body.ReadFrom(request.Body)
message := body.String()
Expand Down
2 changes: 1 addition & 1 deletion httplistener/httplistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func New(irc *irc.Connection) (*HTTPListener, error) {

mux := http.NewServeMux()

if viper.GetBool("http.listeners.generic") {
if viper.IsSet("http.listeners.generic") {
log.Infof("Listening for HTTP POST requests at /send")
mux.HandleFunc("/send", hl.genericHandler)
}
Expand Down

0 comments on commit 0ed531f

Please sign in to comment.