-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
51 lines (42 loc) · 1.29 KB
/
middleware.go
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
package echo
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/pratikmallya/clubhouse-webhook/pkg/signature"
)
// Config specifies configuration options for the middleware.
type Config struct {
// Key is the secret used for generating HMAC digest. This is the secret provided to Cluhouse when configuring
// webhooks.
Key []byte
}
// NewConfig returns a new Config with the provided configuration options.
func NewConfig(key string) Config {
return Config{
Key: []byte(key),
}
}
// HeaderVerification is a verification middleware that only allows requests that are verified to originate
// from Clubhouse.
func HeaderVerification(config Config, skipper middleware.Skipper) echo.MiddlewareFunc {
if skipper == nil {
skipper = middleware.DefaultSkipper
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if skipper(c) {
return next(c)
}
verified, err := signature.Verify(c.Request(), config.Key)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("webhook verification failed. Error: %w", err))
}
if !verified {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Errorf("webhook signature did not match"))
}
return next(c)
}
}
}