-
Notifications
You must be signed in to change notification settings - Fork 33
/
authorize_test.go
324 lines (293 loc) · 9.98 KB
/
authorize_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Julien Vehent jvehent@mozilla.com [:ulfr]
package main
import (
"bytes"
"crypto/sha256"
"net/http"
"testing"
"time"
"go.mozilla.org/hawk"
)
func TestMissingAuthorization(t *testing.T) {
ag, _ := newTestAutographer(t)
body := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaa")
bodyrdr := bytes.NewReader(body)
req, err := http.NewRequest("POST", "http://foo.bar/sign/data", bodyrdr)
if err != nil {
t.Fatal(err)
}
_, _, err = ag.authorizeHeader(req)
if err == nil {
t.Errorf("expected auth to fail with missing authorization but succeeded")
}
if err.Error() != "missing Authorization header" {
t.Errorf("expected auth to fail with missing authorization but got error: %v", err)
}
}
func TestBogusAuthorization(t *testing.T) {
ag, _ := newTestAutographer(t)
body := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaa")
bodyrdr := bytes.NewReader(body)
req, err := http.NewRequest("POST", "http://foo.bar/sign/data", bodyrdr)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Authorization", `Hawk thisisbob="bob", andhereisamac="nVg5STp2fD+P7G3ELmUztb3hP/LQajwD+FDQM7rZvhw=", ts="1453681057"`)
_, _, err = ag.authorizeHeader(req)
if err == nil {
t.Errorf("expected auth to fail with invalid authorization but succeeded")
}
if err.Error() != "hawk: invalid mac, missing or empty" {
t.Errorf("expected auth to fail with no authorization but got error: %v", err)
}
}
func TestBadPayload(t *testing.T) {
ag, conf := newTestAutographer(t)
body := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaa")
bodyrdr := bytes.NewReader(body)
req, err := http.NewRequest("POST", "http://foo.bar/sign/data", bodyrdr)
if err != nil {
t.Fatal(err)
}
auth, err := ag.getAuthByID(conf.Authorizations[0].ID)
if err != nil {
t.Fatal(err)
}
authheader := getAuthHeader(req, auth.ID, auth.Key, sha256.New, id(), "application/json", []byte(`9247oldfjd18weohfa`))
req.Header.Set("Authorization", authheader)
_, err = ag.authorize(req, body)
if err == nil {
t.Errorf("expected auth to fail with payload validation failed but succeeded")
}
if err.Error() != "payload validation failed" {
t.Errorf("expected auth to fail with payload validation failed but got error: %v", err)
}
}
func TestExpiredAuth(t *testing.T) {
ag, _ := newTestAutographer(t)
body := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaa")
bodyrdr := bytes.NewReader(body)
req, err := http.NewRequest("POST", "http://foo.bar/sign/data", bodyrdr)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", `Hawk id="bob", mac="nVg5STp2fD+P7G3ELmUztb3hP/LQajwD+FDQM7rZvhw=", ts="1453681057", nonce="TKLzwtGS", hash="sL12YYG2CnALd5o5dqHRKjNO0AvgmPPeIqlfZQfszfo=", ext="59d2rtbmji6617pthvwa1h370"`)
_, _, err = ag.authorizeHeader(req)
if err == nil {
t.Errorf("expected auth to fail with expired timestamp but succeeded")
}
if err.Error() != hawk.ErrTimestampSkew.Error() {
t.Errorf("expected auth to fail with expired timestamp but got error: %v", err)
}
}
func TestDuplicateNonce(t *testing.T) {
ag, conf := newTestAutographer(t)
body := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaa")
bodyrdr := bytes.NewReader(body)
req, err := http.NewRequest("POST", "http://foo.bar/sign/data", bodyrdr)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
auth, err := ag.getAuthByID(conf.Authorizations[0].ID)
if err != nil {
t.Fatal(err)
}
authheader := getAuthHeader(req, auth.ID, auth.Key, sha256.New, id(), "application/json", body)
req.Header.Set("Authorization", authheader)
// run it once
_, _, _ = ag.authorizeHeader(req)
// and run it twice
_, _, err = ag.authorizeHeader(req)
if err == nil {
t.Errorf("expected auth to fail with duplicate nonce, but succeeded")
}
if err.Error() != hawk.ErrReplay.Error() {
t.Errorf("expected auth to fail with duplicate nonces but got error: %v", err)
}
}
func TestNonceFromLRU(t *testing.T) {
ag, conf := newTestAutographer(t)
req, err := http.NewRequest("POST", "http://foo.bar/sign/data", nil)
if err != nil {
t.Fatal(err)
}
authCreds, err := ag.getAuthByID(conf.Authorizations[0].ID)
if err != nil {
t.Fatal(err)
}
auth1 := hawk.NewRequestAuth(req,
&hawk.Credentials{
ID: authCreds.ID,
Key: authCreds.Key,
Hash: sha256.New},
0)
req.Header.Set("Authorization", auth1.RequestHeader())
_, _, err = ag.authorizeHeader(req)
if err != nil {
t.Fatalf("error authorizing header for first request: %s", err)
}
auth2 := hawk.NewRequestAuth(req,
&hawk.Credentials{
ID: authCreds.ID,
Key: authCreds.Key,
Hash: sha256.New},
0)
req.Header.Set("Authorization", auth2.RequestHeader())
_, _, err = ag.authorizeHeader(req)
if err != nil {
t.Fatalf("error authorizing header for second request: %s", err)
}
if ag.nonces.Contains(auth1.Nonce) {
t.Errorf("First nonce %q found in cache, should have been removed", auth1.Nonce)
t.Logf("nonces: %+v", ag.nonces.Keys())
}
if !ag.nonces.Contains(auth2.Nonce) {
t.Errorf("Second nonce %q not found in cache, should have been present", auth2.Nonce)
t.Logf("nonces: %+v", ag.nonces.Keys())
}
}
func TestSignerNotFound(t *testing.T) {
ag, _ := newTestAutographer(t)
_, err := ag.authBackend.getSignerForUser(`unknown018qoegdxc`, `unkown093ytid`)
if err == nil {
t.Errorf("expected to fail lookup up a signer but succeeded")
}
}
func TestDefaultSignerNotFound(t *testing.T) {
ag, _ := newTestAutographer(t)
_, err := ag.authBackend.getSignerForUser(`unknown018qoegdxc`, ``)
if err == nil {
t.Errorf("expected to fail lookup up a signer but succeeded")
}
}
func TestAutographerAddAuthorizationsFails(t *testing.T) {
ag, _ := newTestAutographer(t)
testcases := []struct {
name string
auths []authorization
errStr string
}{
{
name: "two authorizations with same ID fails",
auths: []authorization{
{
ID: "alice",
Signers: []string{"appkey1"},
},
{
ID: "alice",
Signers: []string{"appkey2"},
},
},
errStr: `authorization id 'alice' already defined, duplicates are not permitted`,
},
{
name: "authorization without a signer ID fails",
auths: []authorization{
{
ID: "bernie",
Signers: []string{},
},
},
errStr: `auth id "bernie" must have at least one signer configured`,
},
{
name: "invalid empty string auth ID fails",
auths: []authorization{
{
ID: "",
Signers: []string{"appkey1"},
},
},
errStr: `authorization id '' is invalid, it must match ^[a-zA-Z0-9-_]{1,255}$`,
},
{
name: "invalid long auth ID fails",
auths: []authorization{
{
ID: "fe3b321f83bf7a09c9199d118915f74ffef8de9b7abcc2dae93ea83cf0541a0c127bc91d1a0ba028af781553abad2bb4101ea1f84559e395d6f301308b4ead9956ef4ccd1ea7c8ce50a422cc78e7ddc1518ef8e54a08141e277808638b4104acf3e6211189222feea199c7da25d9aff5b55c02f6f686f2f1e91ea97dda6b33135593c5c4f80106d5836646557e2b001b3c531d10a4e9f6b7a6b4bd99303bce40592e13d1a8daad93f5cfd2fa78f423ae3dcad40303a0d9d85a166142e09f507904fee326470d1d50af28e2f4348f307d2f76b9c5dd3f9f9b3537c7ef86e63b606b1c57b408c8ae687e7d5c969002203777240029d4998644dbc347fc8f666c5b",
Signers: []string{"appkey1"},
},
},
errStr: `authorization id 'fe3b321f83bf7a09c9199d118915f74ffef8de9b7abcc2dae93ea83cf0541a0c127bc91d1a0ba028af781553abad2bb4101ea1f84559e395d6f301308b4ead9956ef4ccd1ea7c8ce50a422cc78e7ddc1518ef8e54a08141e277808638b4104acf3e6211189222feea199c7da25d9aff5b55c02f6f686f2f1e91ea97dda6b33135593c5c4f80106d5836646557e2b001b3c531d10a4e9f6b7a6b4bd99303bce40592e13d1a8daad93f5cfd2fa78f423ae3dcad40303a0d9d85a166142e09f507904fee326470d1d50af28e2f4348f307d2f76b9c5dd3f9f9b3537c7ef86e63b606b1c57b408c8ae687e7d5c969002203777240029d4998644dbc347fc8f666c5b' is invalid, it must match ^[a-zA-Z0-9-_]{1,255}$`,
},
{
name: "invalid auth ID with symbols fails",
auths: []authorization{
{
ID: "%!@",
Signers: []string{"appkey1"},
},
},
errStr: `authorization id '%!@' is invalid, it must match ^[a-zA-Z0-9-_]{1,255}$`,
},
{
name: "invalid auth ID with newline fails",
auths: []authorization{
{
ID: "\n",
Signers: []string{"appkey1"},
},
},
errStr: `authorization id '
' is invalid, it must match ^[a-zA-Z0-9-_]{1,255}$`,
},
{
name: "invalid auth ID with period fails",
auths: []authorization{
{
ID: ".",
Signers: []string{"appkey1"},
},
},
errStr: `authorization id '.' is invalid, it must match ^[a-zA-Z0-9-_]{1,255}$`,
},
}
for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
err := ag.addAuthorizations(testcase.auths)
if err == nil {
t.Fatalf("%s: addAuthorizations did not fail as expected", testcase.name)
}
if err.Error() != testcase.errStr {
t.Fatalf("%s: addAuthorizations failed with %q instead of expected error %q", testcase.name, err.Error(), testcase.errStr)
}
})
}
}
// set an authorization with a ts validity of 2 seconds, then sleep 5 seconds
// to trigger the hawk skew error
func TestHawkTimestampSkewFail(t *testing.T) {
ag, _ := newTestAutographer(t)
var err error
ag.hawkMaxTimestampSkew, err = time.ParseDuration("2s")
if err != nil {
t.Fatal(err)
}
ag.addAuthorizations([]authorization{
{
ID: "alice",
Key: "1862300e9bd18eafab2eb8d6",
Signers: []string{"appkey1"},
}})
body := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaa")
bodyrdr := bytes.NewReader(body)
req, err := http.NewRequest("POST", "http://foo.bar/sign/data", bodyrdr)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
authheader := getAuthHeader(req, "alice", "1862300e9bd18eafab2eb8d6", sha256.New, id(), "application/json", body)
req.Header.Set("Authorization", authheader)
time.Sleep(5 * time.Second)
_, _, err = ag.authorizeHeader(req)
if err.Error() != hawk.ErrTimestampSkew.Error() {
t.Errorf("expected auth to fail with skewed timestamp but got error: %v", err)
}
}