-
Notifications
You must be signed in to change notification settings - Fork 10
/
auth_test.go
209 lines (187 loc) · 7.08 KB
/
auth_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
package iprepd
import (
"bytes"
"crypto/sha256"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"go.mozilla.org/hawk"
)
func TestAuth(t *testing.T) {
assert.Nil(t, baseTest())
h := mwHandler(newRouter())
// auth'd endpoint, should fail
recorder := httptest.NewRecorder()
h.ServeHTTP(recorder, httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil))
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// valid api key
recorder = httptest.NewRecorder()
req := httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
req.Header.Set("Authorization", "APIKey key1")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusOK, recorder.Code)
// invalid api key
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
req.Header.Set("Authorization", "APIKey key1invalid")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// zero length api key
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
req.Header.Set("Authorization", "APIKey ")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// valid Read-Only API key for write-not-required endpoint
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
req.Header.Set("Authorization", "APIKey rokey1")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusOK, recorder.Code)
// valid Read-Only API key for write-required endpoint
recorder = httptest.NewRecorder()
buf := "{\"object\": \"192.168.0.1\", \"type\": \"ip\", \"reputation\": 50}"
req = httptest.NewRequest("PUT", "/type/ip/192.168.0.1", bytes.NewReader([]byte(buf)))
req.Header.Set("Authorization", "APIKey rokey1")
req.Header.Set("Content-Type", "application/json")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// valid hawk header
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
auth := hawk.NewRequestAuth(req, &hawk.Credentials{
ID: "root",
Key: "toor",
Hash: sha256.New,
}, 0)
req.Header.Set("Authorization", auth.RequestHeader())
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusOK, recorder.Code)
// valid Read-Only hawk for a write-not-required endpoint
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
auth = hawk.NewRequestAuth(req, &hawk.Credentials{
ID: "roroot",
Key: "rotoor",
Hash: sha256.New,
}, 0)
req.Header.Set("Authorization", auth.RequestHeader())
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusOK, recorder.Code)
// valid Read-Only hawk for a write-required endpoint
recorder = httptest.NewRecorder()
buf = "{\"object\": \"192.168.0.1\", \"type\": \"ip\", \"reputation\": 50}"
req = httptest.NewRequest("PUT", "/type/ip/192.168.0.1", bytes.NewReader([]byte(buf)))
auth = hawk.NewRequestAuth(req, &hawk.Credentials{
ID: "roroot",
Key: "rotoor",
Hash: sha256.New,
}, 0)
hash := auth.PayloadHash("application/json")
hash.Write([]byte(buf))
auth.SetHash(hash)
req.Header.Set("Authorization", auth.RequestHeader())
req.Header.Set("Content-Type", "application/json")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// invalid hawk id
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
auth = hawk.NewRequestAuth(req, &hawk.Credentials{
ID: "invalid",
Key: "toor",
Hash: sha256.New,
}, 0)
req.Header.Set("Authorization", auth.RequestHeader())
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// invalid hawk secret
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
auth = hawk.NewRequestAuth(req, &hawk.Credentials{
ID: "root",
Key: "invalid",
Hash: sha256.New,
}, 0)
req.Header.Set("Authorization", auth.RequestHeader())
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// valid hawk credentials with a content-type set but no request body on GET,
// verify the request is rejected
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
auth = hawk.NewRequestAuth(req, &hawk.Credentials{
ID: "root",
Key: "toor",
Hash: sha256.New,
}, 0)
req.Header.Set("Authorization", auth.RequestHeader())
req.Header.Set("Content-Type", "application/json")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// valid hawk put with a request body
recorder = httptest.NewRecorder()
buf = "{\"object\": \"192.168.0.1\", \"type\": \"ip\", \"reputation\": 50}"
req = httptest.NewRequest("PUT", "/type/ip/192.168.0.1", bytes.NewReader([]byte(buf)))
auth = hawk.NewRequestAuth(req, &hawk.Credentials{
ID: "root",
Key: "toor",
Hash: sha256.New,
}, 0)
hash = auth.PayloadHash("application/json")
hash.Write([]byte(buf))
auth.SetHash(hash)
req.Header.Set("Authorization", auth.RequestHeader())
req.Header.Set("Content-Type", "application/json")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusOK, recorder.Code)
// valid hawk creds in a put with a request body, but missing a content-type
recorder = httptest.NewRecorder()
req = httptest.NewRequest("PUT", "/type/ip/192.168.0.1", bytes.NewReader([]byte(buf)))
auth = hawk.NewRequestAuth(req, &hawk.Credentials{
ID: "root",
Key: "toor",
Hash: sha256.New,
}, 0)
hash = auth.PayloadHash("application/json")
hash.Write([]byte(buf))
auth.SetHash(hash)
req.Header.Set("Authorization", auth.RequestHeader())
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// valid hawk creds in a put with a request body, missing a payload hash
recorder = httptest.NewRecorder()
req = httptest.NewRequest("PUT", "/type/ip/192.168.0.1", bytes.NewReader([]byte(buf)))
auth = hawk.NewRequestAuth(req, &hawk.Credentials{
ID: "root",
Key: "toor",
Hash: sha256.New,
}, 0)
req.Header.Set("Authorization", auth.RequestHeader())
req.Header.Set("Content-Type", "application/json")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// invalid hawk header
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
req.Header.Set("Authorization", "Hawk invalid")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// zero length hawk header
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
req.Header.Set("Authorization", "Hawk ")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// empty authorization header
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/type/ip/192.168.0.1", nil)
req.Header.Set("Authorization", " ")
h.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
// unauth'd endpoint, should succeed
recorder = httptest.NewRecorder()
h.ServeHTTP(recorder, httptest.NewRequest("GET", "/__heartbeat__", nil))
assert.Equal(t, http.StatusOK, recorder.Code)
}