forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_version_check.go
68 lines (58 loc) · 2 KB
/
middleware_version_check.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"errors"
"fmt"
"github.com/lonelycode/tykcommon"
"net/http"
)
// VersionCheck will check whether the version of the requested API the request is accessing has any restrictions on URL endpoints
type VersionCheck struct {
*TykMiddleware
sh SuccessHandler
}
// New lets you do any initialisations for the object can be done here
func (v *VersionCheck) New() {
v.sh = SuccessHandler{v.TykMiddleware}
}
// GetConfig retrieves the configuration from the API config
func (v *VersionCheck) GetConfig() (interface{}, error) {
return nil, nil
}
func (v *VersionCheck) DoMockReply(w http.ResponseWriter, meta interface{}) {
// Reply with some alternate data
thisMeta := meta.(*tykcommon.EndpointMethodMeta)
responseMessage := []byte(thisMeta.Data)
for header, value := range thisMeta.Headers {
w.Header().Add(header, value)
}
w.WriteHeader(thisMeta.Code)
fmt.Fprintf(w, string(responseMessage))
return
}
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (v *VersionCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
// Check versioning, blacklist, whitelist and ignored status
requestValid, stat, meta := v.TykMiddleware.Spec.IsRequestValid(r)
if requestValid == false {
// Fire a versioning failure event
go v.TykMiddleware.FireEvent(EVENT_VersionFailure,
EVENT_VersionFailureMeta{
EventMetaDefault: EventMetaDefault{Message: "Attempted access to disallowed version / path.", OriginatingRequest: EncodeRequestToEvent(r)},
Path: r.URL.Path,
Origin: r.RemoteAddr,
Key: "",
Reason: string(stat),
})
return errors.New(string(stat)), 403
}
// We handle redirects before ignores in case we aren't using a whitelist
if stat == StatusRedirectFlowByReply {
v.DoMockReply(w, meta)
return nil, 666
}
if stat == StatusOkAndIgnore {
v.sh.ServeHTTP(w, r)
return nil, 666
}
return nil, 200
}