forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_auth_key.go
115 lines (90 loc) · 2.96 KB
/
middleware_auth_key.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import "net/http"
import (
"bytes"
"errors"
"github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"io"
"io/ioutil"
)
// KeyExists will check if the key being used to access the API is in the request data,
// and then if the key is in the storage engine
type AuthKey struct {
*TykMiddleware
}
func (k AuthKey) New() {}
// GetConfig retrieves the configuration from the API config
func (k *AuthKey) GetConfig() (interface{}, error) {
return k.TykMiddleware.Spec.APIDefinition.Auth, nil
}
func (k *AuthKey) copyResponse(dst io.Writer, src io.Reader) {
io.Copy(dst, src)
}
func CopyRequest(r *http.Request) *http.Request {
tempRes := new(http.Request)
*tempRes = *r
defer r.Body.Close()
// Buffer body data - don't like thi but we would otherwise drain the request body
var bodyBuffer bytes.Buffer
bodyBuffer2 := new(bytes.Buffer)
io.Copy(&bodyBuffer, r.Body)
*bodyBuffer2 = bodyBuffer
// Create new ReadClosers so we can split output
r.Body = ioutil.NopCloser(&bodyBuffer)
tempRes.Body = ioutil.NopCloser(bodyBuffer2)
return tempRes
}
func (k *AuthKey) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
thisConfig := k.TykMiddleware.Spec.APIDefinition.Auth
authHeaderValue := r.Header.Get(thisConfig.AuthHeaderName)
if thisConfig.UseParam {
tempRes := CopyRequest(r)
// Set hte header name
authHeaderValue = tempRes.FormValue(thisConfig.AuthHeaderName)
}
if thisConfig.UseCookie {
tempRes := CopyRequest(r)
authCookie, notFoundErr := tempRes.Cookie(thisConfig.AuthHeaderName)
if notFoundErr != nil {
authHeaderValue = ""
} else {
authHeaderValue = authCookie.Value
}
}
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.")
return errors.New("Authorization field missing"), 400
}
// Check if API key valid
thisSessionState, keyExists := k.TykMiddleware.CheckSessionAndIdentityForValidKey(authHeaderValue)
if !keyExists {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": r.RemoteAddr,
"key": authHeaderValue,
}).Info("Attempted access with non-existent key.")
// Fire Authfailed Event
AuthFailed(k.TykMiddleware, r, authHeaderValue)
// Report in health check
ReportHealthCheckValue(k.Spec.Health, KeyFailure, "1")
return errors.New("Key not authorised"), 403
}
// Set session state on context, we will need it later
context.Set(r, SessionData, thisSessionState)
context.Set(r, AuthHeaderValue, authHeaderValue)
return nil, 200
}
func AuthFailed(m *TykMiddleware, r *http.Request, authHeaderValue string) {
go m.FireEvent(EVENT_AuthFailure,
EVENT_AuthFailureMeta{
EventMetaDefault: EventMetaDefault{Message: "Auth Failure", OriginatingRequest: EncodeRequestToEvent(r)},
Path: r.URL.Path,
Origin: r.RemoteAddr,
Key: authHeaderValue,
})
}