-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode.go
31 lines (27 loc) · 817 Bytes
/
decode.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
package middleware
import (
"net/http"
"github.com/AndreasAbdi/alexa-local-server/internal/encoding"
)
//GetVerifyJSON Decode the JSON request and verify it.
func GetVerifyJSON(appID string) func(http.ResponseWriter, *http.Request, http.HandlerFunc) {
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
ctx := r.Context()
echoReq, err := encoding.GetRequest(ctx, r)
if err != nil {
httpError(w, err.Error(), "Bad Request", 400)
return
}
// Check the timestamp
if !echoReq.VerifyTimestamp() && r.URL.Query().Get("_dev") == "" {
httpError(w, "Request too old to continue (>150s).", "Bad Request", 400)
return
}
// Check the app id
if !echoReq.VerifyAppID(appID) {
httpError(w, "Echo AppID mismatch!", "Bad Request", 400)
return
}
next(w, r)
}
}