Browse Source

http/generic: Implement basic http auth via token

* Solves #18
tags/v0.4.5^2
Alexander von Gluck IV 4 years ago
parent
commit
491b6f7cc4
3 changed files with 27 additions and 3 deletions
  1. 12
    2
      README.md
  2. 14
    0
      httplistener/generic.go
  3. 1
    1
      httplistener/httplistener.go

+ 12
- 2
README.md View File

@@ -54,8 +54,18 @@ of netcat, with `-d @-` to read POST data from stdin, like so:
54 54
 
55 55
     echo "Hello world" | curl -d @- http://irccat-host/send
56 56
 
57
-Everything that works via netcat also works by POST to `/send`. Note that this endpoint
58
-is unauthenticated.
57
+### Generic HTTP Endpoint with authentication
58
+
59
+```json
60
+"generic": {
61
+    "secret": "my_secret"
62
+}
63
+```
64
+
65
+Adding an optional secret allows you to require a single secret token before sending
66
+messages to the specified channels. (Using HTTPS is recommended to ensure key security)
67
+
68
+    echo "Hello world" | curl -H "Authorization: Bearer my_secret" -d @- http://irccat-host/send
59 69
 
60 70
 ### Grafana Webhook
61 71
 ```json

+ 14
- 0
httplistener/generic.go View File

@@ -2,7 +2,9 @@ package httplistener
2 2
 
3 3
 import (
4 4
 	"bytes"
5
+	"fmt"
5 6
 	"github.com/irccloud/irccat/dispatcher"
7
+	"github.com/spf13/viper"
6 8
 	"net/http"
7 9
 )
8 10
 
@@ -17,6 +19,18 @@ func (hl *HTTPListener) genericHandler(w http.ResponseWriter, request *http.Requ
17 19
 		return
18 20
 	}
19 21
 
22
+	// Optional simple auth via token
23
+	secret := viper.GetString("http.listeners.generic.secret")
24
+	if secret != "" {
25
+		auth := request.Header.Get("Authorization")
26
+		expecting := fmt.Sprintf("Bearer %s", secret)
27
+		if auth != expecting {
28
+			http.Error(w, "Invalid Authorization", http.StatusUnauthorized)
29
+			log.Warningf("%s - Invalid Authorization!", request.RemoteAddr)
30
+			return
31
+		}
32
+	}
33
+
20 34
 	body := new(bytes.Buffer)
21 35
 	body.ReadFrom(request.Body)
22 36
 	message := body.String()

+ 1
- 1
httplistener/httplistener.go View File

@@ -25,7 +25,7 @@ func New(irc *irc.Connection) (*HTTPListener, error) {
25 25
 
26 26
 	mux := http.NewServeMux()
27 27
 
28
-	if viper.GetBool("http.listeners.generic") {
28
+	if viper.IsSet("http.listeners.generic") {
29 29
 		log.Infof("Listening for HTTP POST requests at /send")
30 30
 		mux.HandleFunc("/send", hl.genericHandler)
31 31
 	}

Loading…
Cancel
Save