-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
36 lines (31 loc) · 917 Bytes
/
handler.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
// handler.go
package http_signature_auth
import (
"net/http"
"net/http/httputil"
"github.com/rs/zerolog/log"
)
func NewSignatureAuthHandler(keysDB *Keys, handlerFunc http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Debug().Msgf("Received request from %s", r.RemoteAddr)
dump, err := httputil.DumpRequest(r, true)
if err != nil {
log.Error().Msgf("cannot dump request: %s", err)
} else {
log.Debug().Msgf("%q", dump)
}
ok, err := VerifySignature(keysDB, r)
if err != nil {
log.Debug().Msgf("error when verifying signature: %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if !ok {
log.Debug().Msgf("Unauthorized request from %s", r.RemoteAddr)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
log.Debug().Msgf("Authorized request from %s", r.RemoteAddr)
handlerFunc(w, r)
}
}