mirror of
https://github.com/adnanh/webhook.git
synced 2025-05-14 17:44:42 +00:00
added support for secrets
This commit is contained in:
parent
be543f4005
commit
179367a28d
1 changed files with 27 additions and 2 deletions
29
webhook.go
29
webhook.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
@ -12,6 +13,9 @@ import (
|
||||||
|
|
||||||
"github.com/go-martini/martini"
|
"github.com/go-martini/martini"
|
||||||
|
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha256"
|
||||||
|
|
||||||
l4g "code.google.com/p/log4go"
|
l4g "code.google.com/p/log4go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -78,13 +82,34 @@ func hookHandler(req *http.Request, params martini.Params) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
go func(id string, params interface{}) {
|
body, err := ioutil.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
l4g.Warn("Error occurred while trying to read the request body: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go func(id string, body []byte, signature string, params interface{}) {
|
||||||
if hook := webhooks.Match(id, params); hook != nil {
|
if hook := webhooks.Match(id, params); hook != nil {
|
||||||
|
if hook.Secret != "" {
|
||||||
|
if signature == "" {
|
||||||
|
l4g.Error("Hook %s got matched, but the request contained invalid signature.", hook.ID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mac := hmac.New(sha256.New, []byte(hook.Secret))
|
||||||
|
mac.Write(body)
|
||||||
|
expectedMAC := mac.Sum(nil)
|
||||||
|
|
||||||
|
if !hmac.Equal([]byte(signature), expectedMAC) {
|
||||||
|
l4g.Error("Hook %s got matched, but the request contained invalid signature. Expected %s, got %s.", hook.ID, signature, expectedMAC)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cmd := exec.Command(hook.Command, "", "", hook.Cwd)
|
cmd := exec.Command(hook.Command, "", "", hook.Cwd)
|
||||||
out, _ := cmd.Output()
|
out, _ := cmd.Output()
|
||||||
l4g.Info("Hook %s triggered successfully! Command output:\n%s", hook.ID, out)
|
l4g.Info("Hook %s triggered successfully! Command output:\n%s", hook.ID, out)
|
||||||
}
|
}
|
||||||
}(params["id"], p)
|
}(params["id"], body, req.Header.Get("X-Hub-Signature"), p)
|
||||||
|
|
||||||
return "Got it, thanks. :-)"
|
return "Got it, thanks. :-)"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue