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
 
54
 
55
     echo "Hello world" | curl -d @- http://irccat-host/send
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
 ### Grafana Webhook
70
 ### Grafana Webhook
61
 ```json
71
 ```json

+ 14
- 0
httplistener/generic.go View File

2
 
2
 
3
 import (
3
 import (
4
 	"bytes"
4
 	"bytes"
5
+	"fmt"
5
 	"github.com/irccloud/irccat/dispatcher"
6
 	"github.com/irccloud/irccat/dispatcher"
7
+	"github.com/spf13/viper"
6
 	"net/http"
8
 	"net/http"
7
 )
9
 )
8
 
10
 
17
 		return
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
 	body := new(bytes.Buffer)
34
 	body := new(bytes.Buffer)
21
 	body.ReadFrom(request.Body)
35
 	body.ReadFrom(request.Body)
22
 	message := body.String()
36
 	message := body.String()

+ 1
- 1
httplistener/httplistener.go View File

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

Loading…
Cancel
Save