-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
64 lines (53 loc) · 1.33 KB
/
main.go
File metadata and controls
64 lines (53 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type Payload struct {
Action string `json:"action"`
}
func main() {
http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
secret := os.Getenv("GITHUB_WEBHOOK_SECRET")
if secret == "" {
http.Error(w, "Secret not set", http.StatusInternalServerError)
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
signature := r.Header.Get("X-Hub-Signature")
if signature == "" {
http.Error(w, "Signature not set", http.StatusBadRequest)
return
}
mac := hmac.New(sha1.New, []byte(secret))
mac.Write(body)
expectedMAC := hex.EncodeToString(mac.Sum(nil))
if !hmac.Equal([]byte(signature[5:]), []byte(expectedMAC)) {
http.Error(w, "Invalid signature", http.StatusUnauthorized)
return
}
var payload Payload
if err := json.Unmarshal(body, &payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Printf("Received event: %s", payload.Action)
})
log.Println("Listening on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}