forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_key_exists.go
57 lines (45 loc) · 1.43 KB
/
middleware_key_exists.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
package main
import "net/http"
import (
"github.com/gorilla/context"
"github.com/Sirupsen/logrus"
)
type KeyExists struct{
TykMiddleware
}
func (k KeyExists) New() func(http.Handler) http.Handler {
aliceHandler := func(h http.Handler) http.Handler {
thisHandler := func(w http.ResponseWriter, r *http.Request) {
authHeaderValue := r.Header.Get(k.Spec.ApiDefinition.Auth.AuthHeaderName)
if authHeaderValue == "" {
// No header value, fail
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": r.RemoteAddr,
}).Info("Attempted access with malformed header, no auth header found.")
handler := ErrorHandler{k.TykMiddleware}
handler.HandleError(w, r, "Authorisation field missing", 400)
return
}
// Check if API key valid
key_exists, thisSessionState := authManager.IsKeyAuthorised(authHeaderValue)
if !key_exists {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": r.RemoteAddr,
"key": authHeaderValue,
}).Info("Attempted access with non-existent key.")
handler := ErrorHandler{k.TykMiddleware}
handler.HandleError(w, r, "Key not authorised", 403)
return
}
// Set session state on context, we will need it later
context.Set(r, SessionData, thisSessionState)
context.Set(r, AuthHeaderValue, authHeaderValue)
// Request is valid, carry on
h.ServeHTTP(w, r)
}
return http.HandlerFunc(thisHandler)
}
return aliceHandler
}