forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmw_strip_auth.go
93 lines (66 loc) · 1.82 KB
/
mw_strip_auth.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
package gateway
import (
"net/http"
"net/url"
"strings"
"github.com/sirupsen/logrus"
)
type StripAuth struct {
BaseMiddleware
}
func (sa *StripAuth) Name() string {
return "StripAuth"
}
func (sa *StripAuth) EnabledForSpec() bool {
return sa.Spec.StripAuthData
}
func (sa *StripAuth) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
config := sa.Spec.Auth
log.WithFields(logrus.Fields{
"prefix": sa.Name(),
}).Debugf("sa.Spec.Auth: %+v\n", config)
if sa.Spec.Auth.UseParam {
sa.stripFromParams(r)
}
sa.stripFromHeaders(r)
return nil, http.StatusOK
}
// strips auth from query string params
func (sa *StripAuth) stripFromParams(r *http.Request) {
config := sa.Spec.Auth
reqUrlPtr, _ := url.Parse(r.URL.String())
authParamName := "Authorization"
if config.ParamName != "" {
authParamName = config.ParamName
} else if config.AuthHeaderName != "" {
authParamName = config.AuthHeaderName
}
queryStringValues := reqUrlPtr.Query()
queryStringValues.Del(authParamName)
reqUrlPtr.RawQuery = queryStringValues.Encode()
r.URL, _ = r.URL.Parse(reqUrlPtr.String())
}
// strips auth key from headers
func (sa *StripAuth) stripFromHeaders(r *http.Request) {
config := sa.Spec.Auth
authHeaderName := "Authorization"
if config.AuthHeaderName != "" {
authHeaderName = config.AuthHeaderName
}
r.Header.Del(authHeaderName)
// Strip Authorization from Cookie Header
cookieName := "Cookie"
if config.CookieName != "" {
cookieName = config.CookieName
}
cookieValue := r.Header.Get(cookieName)
cookies := strings.Split(r.Header.Get(cookieName), ";")
for i, c := range cookies {
if strings.HasPrefix(c, authHeaderName) {
cookies = append(cookies[:i], cookies[i+1:]...)
cookieValue = strings.Join(cookies, ";")
r.Header.Set(cookieName, cookieValue)
break
}
}
}