forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiauth_test.go
216 lines (180 loc) · 6.89 KB
/
multiauth_test.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"encoding/base64"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/justinas/alice"
)
const multiAuthDev = `{
"api_id": "55",
"org_id": "default",
"use_basic_auth": true,
"use_standard_auth": true,
"base_identity_provided_by": "auth_token",
"auth": {
"auth_header_name": "x-standard-auth"
},
"version_data": {
"not_versioned": true,
"versions": {
"Default": {
"name": "Default"
}
}
},
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `"
}
}`
func createMultiAuthKeyAuthSession() *SessionState {
session := new(SessionState)
session.Rate = 100.0
session.Allowance = session.Rate
session.LastCheck = time.Now().Unix()
session.Per = 1.0
session.QuotaRenewalRate = 300 // 5 minutes
session.QuotaRenews = time.Now().Unix()
session.QuotaRemaining = 900
session.QuotaMax = 10
session.AccessRights = map[string]AccessDefinition{"55": {APIName: "Tyk Multi Key Test", APIID: "55", Versions: []string{"default"}}}
return session
}
func createMultiBasicAuthSession() *SessionState {
session := new(SessionState)
session.Rate = 8.0
session.Allowance = session.Rate
session.LastCheck = time.Now().Unix()
session.Per = 1.0
session.QuotaRenewalRate = 300 // 5 minutes
session.QuotaRenews = time.Now().Unix() + 20
session.QuotaRemaining = 1
session.QuotaMax = -1
session.BasicAuthData.Password = "TEST"
session.AccessRights = map[string]AccessDefinition{"55": {APIName: "Tyk Multi Key Test", APIID: "55", Versions: []string{"default"}}}
return session
}
func getMultiAuthStandardAndBasicAuthChain(spec *APISpec) http.Handler {
remote, _ := url.Parse(testHttpAny)
proxy := TykNewSingleHostReverseProxy(remote, spec)
proxyHandler := ProxyHandler(proxy, spec)
baseMid := &BaseMiddleware{spec, proxy}
chain := alice.New(
CreateMiddleware(&IPWhiteListMiddleware{baseMid}),
CreateMiddleware(&BasicAuthKeyIsValid{baseMid}),
CreateMiddleware(&AuthKey{baseMid}),
CreateMiddleware(&VersionCheck{BaseMiddleware: baseMid}),
CreateMiddleware(&KeyExpired{baseMid}),
CreateMiddleware(&AccessRightsCheck{baseMid}),
CreateMiddleware(&RateLimitAndQuotaCheck{baseMid})).Then(proxyHandler)
return chain
}
func TestMultiSession_BA_Standard_OK(t *testing.T) {
spec := createSpecTest(t, multiAuthDev)
// Create BA
baSession := createMultiBasicAuthSession()
username := "0987876"
password := "TEST"
// Basic auth sessions are stored as {org-id}{username}, so we need to append it here when we create the session.
spec.SessionManager.UpdateSession("default0987876", baSession, 60)
// Create key
session := createMultiAuthKeyAuthSession()
customToken := "84573485734587384888723487243"
// AuthKey sessions are stored by {token}
spec.SessionManager.UpdateSession(customToken, session, 60)
to_encode := strings.Join([]string{username, password}, ":")
encodedPass := base64.StdEncoding.EncodeToString([]byte(to_encode))
recorder := httptest.NewRecorder()
req := testReq(t, "GET", "/", nil)
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", encodedPass))
req.Header.Set("x-standard-auth", fmt.Sprintf("Bearer %s", customToken))
chain := getMultiAuthStandardAndBasicAuthChain(spec)
chain.ServeHTTP(recorder, req)
if recorder.Code != 200 {
t.Error("Initial request failed with non-200 code, should have gone through!: \n", recorder.Code)
}
}
func TestMultiSession_BA_Standard_Identity(t *testing.T) {
spec := createSpecTest(t, multiAuthDev)
// Create BA
baSession := createMultiBasicAuthSession()
username := "0987876"
password := "TEST"
// Basic auth sessions are stored as {org-id}{username}, so we need to append it here when we create the session.
spec.SessionManager.UpdateSession("default0987876", baSession, 60)
// Create key
session := createMultiAuthKeyAuthSession()
customToken := "84573485734587384888723487243"
// AuthKey sessions are stored by {token}
spec.SessionManager.UpdateSession(customToken, session, 60)
to_encode := strings.Join([]string{username, password}, ":")
encodedPass := base64.StdEncoding.EncodeToString([]byte(to_encode))
recorder := httptest.NewRecorder()
req := testReq(t, "GET", "/", nil)
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", encodedPass))
req.Header.Set("x-standard-auth", fmt.Sprintf("Bearer %s", customToken))
chain := getMultiAuthStandardAndBasicAuthChain(spec)
chain.ServeHTTP(recorder, req)
if recorder.Code != 200 {
t.Error("Initial request failed with non-200 code, should have gone through!: \n", recorder.Code)
}
if recorder.Header().Get("X-Ratelimit-Remaining") == "-1" {
t.Error("Expected quota limit but found -1, wrong base identity became context")
t.Error(recorder.Header().Get("X-Ratelimit-Remaining"))
}
}
func TestMultiSession_BA_Standard_FAILBA(t *testing.T) {
spec := createSpecTest(t, multiAuthDev)
// Create BA
baSession := createMultiBasicAuthSession()
username := "0987876"
password := "WRONG"
// Basic auth sessions are stored as {org-id}{username}, so we need to append it here when we create the session.
spec.SessionManager.UpdateSession("default0987876", baSession, 60)
// Create key
session := createMultiAuthKeyAuthSession()
customToken := "84573485734587384888723487243"
// AuthKey sessions are stored by {token}
spec.SessionManager.UpdateSession(customToken, session, 60)
to_encode := strings.Join([]string{username, password}, ":")
encodedPass := base64.StdEncoding.EncodeToString([]byte(to_encode))
recorder := httptest.NewRecorder()
req := testReq(t, "GET", "/", nil)
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", encodedPass))
req.Header.Set("x-standard-auth", fmt.Sprintf("Bearer %s", customToken))
chain := getMultiAuthStandardAndBasicAuthChain(spec)
chain.ServeHTTP(recorder, req)
if recorder.Code != 401 {
t.Error("Wrong response code received, expected 401: \n", recorder.Code)
}
}
func TestMultiSession_BA_Standard_FAILAuth(t *testing.T) {
spec := createSpecTest(t, multiAuthDev)
// Create BA
baSession := createMultiBasicAuthSession()
username := "0987876"
password := "TEST"
// Basic auth sessions are stored as {org-id}{username}, so we need to append it here when we create the session.
spec.SessionManager.UpdateSession("default0987876", baSession, 60)
// Create key
session := createMultiAuthKeyAuthSession()
customToken := "84573485734587384888723487243"
// AuthKey sessions are stored by {token}
spec.SessionManager.UpdateSession(customToken, session, 60)
to_encode := strings.Join([]string{username, password}, ":")
encodedPass := base64.StdEncoding.EncodeToString([]byte(to_encode))
recorder := httptest.NewRecorder()
req := testReq(t, "GET", "/", nil)
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", encodedPass))
req.Header.Set("x-standard-auth", fmt.Sprintf("Bearer %s", "WRONGTOKEN"))
chain := getMultiAuthStandardAndBasicAuthChain(spec)
chain.ServeHTTP(recorder, req)
if recorder.Code != 403 {
t.Error("Wrong response code received, expected 403: \n", recorder.Code)
}
}