forked from dunglas/mercure
-
Notifications
You must be signed in to change notification settings - Fork 3
/
authorization_test.go
552 lines (442 loc) · 23.7 KB
/
authorization_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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package mercure
import (
"net/http"
"testing"
"github.com/golang-jwt/jwt/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
validEmptyHeader = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30._esyynAyo2Z6PyGe0mM_SuQ3c-C7sMQJ1YxVLvlj80A"
validFullHeader = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiZm9vIiwiYmFyIl0sInN1YnNjcmliZSI6WyJmb28iLCJiYXoiXX19.e7USPnr2YHHqLYSu9-jEVsynuTXGtAQUDAZuzoR8lxQ"
)
const (
validEmptyHeaderRsa = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.e30.YbkSeO9GIBYedph1uSQz0Y6zp1NwDEB8O7ek3cc3Vw4Fjh6DwrJAwmXoNSqT6FhHDv14QG70qPIuyzsR0Q9nHFo7hGEqE8E85F8z3Pj5eBjHKBMJFno7jww514Vyp35c490ZHD6_d3F9PmxWrPkKezc1mcwlCegwiMJIS2CeR7k"
validFullHeaderRsa = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiZm9vIiwiYmFyIl0sInN1YnNjcmliZSI6WyJmb28iLCJiYXoiXX19.B-ZBdmCbtDaP27wB_DvF9xIetQm88M2Q1d-LP2DZoEHrz6lYDuHkgXzSDnFdbLCZ653e0r_VOaKxe2Pc6R4F0ok2vksC6P5gHhqIUcQuTSlzNFyTrg4tyy4mMkcm1h85te9gkV4LR6TABfZpFPqqIS4t7fpCMxvtAkyf_RR5F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Y67pKPwcnnlR
Ki0jUpg2xzzwyA+nEI6Bf6CDaHKnCqxL7x0yk2XqeA==
-----END RSA PRIVATE KEY-----
`
const certificateRsa = `-----BEGIN CERTIFICATE-----
MIIChDCCAe0CCQDlJhrdK2G+pDANBgkqhkiG9w0BAQsFADCBhTELMAkGA1UEBhMC
VVMxEzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDVNhbiBGcmFuY2lzY28x
EjAQBgNVBAoMCUFjbWUsIEluYzEUMBIGA1UEAwwLZXhhbXBsZS5jb20xHzAdBgkq
hkiG9w0BCQEWEGFjbWVAZXhhbXBsZS5jb20wIBcNMjAxMjIxMTYxNjM2WhgPMzAy
MDA0MjMxNjE2MzZaMIGFMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5p
YTEWMBQGA1UEBwwNU2FuIEZyYW5jaXNjbzESMBAGA1UECgwJQWNtZSwgSW5jMRQw
EgYDVQQDDAtleGFtcGxlLmNvbTEfMB0GCSqGSIb3DQEJARYQYWNtZUBleGFtcGxl
LmNvbTCBnjANBgkqhkiG9w0BAQEFAAOBjAAwgYgCgYB1cLibBZs7BZzpBo/joAKe
JUzakQhdEWUpo2PhAljid7EhM0PKrZCXutbko6/zypqrwccXsdGLVC+8GkkuI76t
8LPAZliMSthqqAFYy4yvB804Uj0KYL9gI4hFw+ogBER3skixo2AFIHzK1SdNSeXz
Ui/jvmwne5z7jQMmBwjjhwIDAQABMA0GCSqGSIb3DQEBCwUAA4GBABosw/cIJkKr
KKBRFBiYuZEeilRHVP2UiUzC8dAASLyw7r63Fg8J7NEN5bYFNdNw1uvvteMryjYu
t+4Iti/mSObpG8FbNb/pOkSJjuJvAxnAIL8iM/DbF28a0SfWiluu5Nk/PciJXLU4
Utb8p35tfj97usdiEB0AN8ray4wZbVWj
-----END CERTIFICATE-----
`
var keyfuncHS256 = func() jwt.Keyfunc {
keyfunc, _ := createJWTKeyfunc([]byte("!ChangeMe!"), "HS256")
return keyfunc
}()
var keyfuncRS256publicKey = func() jwt.Keyfunc {
keyfunc, _ := createJWTKeyfunc([]byte(publicKeyRsa), "RS256")
return keyfunc
}()
func TestAuthorizeMultipleAuthorizationHeader(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", validEmptyHeader)
r.Header.Add("Authorization", validEmptyHeader)
keyfunc, _ := createJWTKeyfunc([]byte{}, "HS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.Error(t, err, `invalid "Authorization" HTTP header`)
require.Nil(t, claims)
}
func TestAuthorizeMultipleAuthorizationHeaderRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", validEmptyHeaderRsa)
r.Header.Add("Authorization", validEmptyHeaderRsa)
keyfunc, _ := createJWTKeyfunc([]byte{}, "RS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.Error(t, err, `invalid "Authorization" HTTP header`)
require.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderTooShort(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer x")
keyfunc, _ := createJWTKeyfunc([]byte{}, "HS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.Error(t, err, `invalid "Authorization" HTTP header`)
require.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderNoBearer(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", "Greater "+validEmptyHeader)
keyfunc, _ := createJWTKeyfunc([]byte{}, "HS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.EqualError(t, err, `invalid "Authorization" HTTP header`)
require.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderNoBearerRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", "Greater "+validEmptyHeaderRsa)
keyfunc, _ := createJWTKeyfunc([]byte{}, "RS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.EqualError(t, err, `invalid "Authorization" HTTP header`)
require.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderInvalidAlg(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", bearerPrefix+createDummyNoneSignedJWT())
keyfunc, _ := createJWTKeyfunc([]byte{}, "HS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.EqualError(t, err, "unable to parse JWT: token is unverifiable: error while executing keyfunc: *jwt.signingMethodNone: unexpected signing method")
require.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderInvalidKey(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", bearerPrefix+validEmptyHeader)
keyfunc, _ := createJWTKeyfunc([]byte{}, "HS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.EqualError(t, err, "unable to parse JWT: token signature is invalid: signature is invalid")
require.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderNoContent(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", bearerPrefix+validEmptyHeader)
claims, err := authorize(r, keyfuncHS256, []string{}, defaultCookieName)
require.NoError(t, err)
require.Nil(t, claims.Mercure.Publish)
require.Nil(t, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationHeaderNoContentRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", bearerPrefix+validEmptyHeaderRsa)
claims, err := authorize(r, keyfuncRS256publicKey, []string{}, defaultCookieName)
require.NoError(t, err)
require.Nil(t, claims.Mercure.Publish)
require.Nil(t, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationHeader(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", bearerPrefix+validFullHeader)
claims, err := authorize(r, keyfuncHS256, []string{}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationHeaderRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", bearerPrefix+validFullHeaderRsa)
claims, err := authorize(r, keyfuncRS256publicKey, []string{}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationHeaderNamespacedRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", bearerPrefix+validFullHeaderNamespacedRsa)
claims, err := authorize(r, keyfuncRS256publicKey, []string{}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationHeaderRsaWithCert(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", bearerPrefix+validFullHeaderRsaForCert)
keyfunc, _ := createJWTKeyfunc([]byte(certificateRsa), "RS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationHeaderWrongAlgorithm(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.Header.Add("Authorization", bearerPrefix+validFullHeaderRsa)
claims, err := authorize(r, keyfuncHS256, []string{}, defaultCookieName)
require.EqualError(t, err, "unable to parse JWT: token is unverifiable: error while executing keyfunc: *jwt.SigningMethodRSA: unexpected signing method")
assert.Nil(t, claims)
}
func TestAuthorizeAuthorizationQueryTooShort(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
query := r.URL.Query()
query.Set("authorization", "x")
r.URL.RawQuery = query.Encode()
keyfunc, _ := createJWTKeyfunc([]byte{}, "HS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.EqualError(t, err, `invalid "authorization" Query parameter`)
require.Nil(t, claims)
}
func TestAuthorizeAuthorizationQueryInvalidAlg(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
query := r.URL.Query()
query.Set("authorization", createDummyNoneSignedJWT())
r.URL.RawQuery = query.Encode()
keyfunc, _ := createJWTKeyfunc([]byte{}, "HS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.EqualError(t, err, "unable to parse JWT: token is unverifiable: error while executing keyfunc: *jwt.signingMethodNone: unexpected signing method")
require.Nil(t, claims)
}
func TestAuthorizeAuthorizationQueryInvalidKey(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
query := r.URL.Query()
query.Set("authorization", validEmptyHeader)
r.URL.RawQuery = query.Encode()
keyfunc, _ := createJWTKeyfunc([]byte{}, "HS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.EqualError(t, err, "unable to parse JWT: token signature is invalid: signature is invalid")
require.Nil(t, claims)
}
func TestAuthorizeAuthorizationQueryNoContent(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
query := r.URL.Query()
query.Set("authorization", validEmptyHeader)
r.URL.RawQuery = query.Encode()
claims, err := authorize(r, keyfuncHS256, []string{}, defaultCookieName)
require.NoError(t, err)
require.Nil(t, claims.Mercure.Publish)
require.Nil(t, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationQueryNoContentRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
query := r.URL.Query()
query.Set("authorization", validEmptyHeaderRsa)
r.URL.RawQuery = query.Encode()
claims, err := authorize(r, keyfuncRS256publicKey, []string{}, defaultCookieName)
require.NoError(t, err)
require.Nil(t, claims.Mercure.Publish)
require.Nil(t, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationQuery(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
query := r.URL.Query()
query.Set("authorization", validFullHeader)
r.URL.RawQuery = query.Encode()
claims, err := authorize(r, keyfuncHS256, []string{}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationQueryRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
query := r.URL.Query()
query.Set("authorization", validFullHeaderRsa)
r.URL.RawQuery = query.Encode()
claims, err := authorize(r, keyfuncRS256publicKey, []string{}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationQueryNamespacedRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
query := r.URL.Query()
query.Set("authorization", validFullHeaderNamespacedRsa)
r.URL.RawQuery = query.Encode()
claims, err := authorize(r, keyfuncRS256publicKey, []string{}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationQueryRsaWithCert(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
query := r.URL.Query()
query.Set("authorization", validFullHeaderRsaForCert)
r.URL.RawQuery = query.Encode()
keyfunc, _ := createJWTKeyfunc([]byte(certificateRsa), "RS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeAuthorizationQueryWrongAlgorithm(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
query := r.URL.Query()
query.Set("authorization", validFullHeaderRsa)
r.URL.RawQuery = query.Encode()
claims, err := authorize(r, keyfuncHS256, []string{}, defaultCookieName)
require.EqualError(t, err, "unable to parse JWT: token is unverifiable: error while executing keyfunc: *jwt.SigningMethodRSA: unexpected signing method")
require.Nil(t, claims)
}
func TestAuthorizeCookieInvalidAlg(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: createDummyNoneSignedJWT()})
claims, err := authorize(r, keyfuncHS256, []string{}, defaultCookieName)
require.EqualError(t, err, "unable to parse JWT: token is unverifiable: error while executing keyfunc: *jwt.signingMethodNone: unexpected signing method")
require.Nil(t, claims)
}
func TestAuthorizeCookieInvalidKey(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validEmptyHeader})
keyfunc, _ := createJWTKeyfunc([]byte{}, "HS256")
claims, err := authorize(r, keyfunc, []string{}, defaultCookieName)
require.EqualError(t, err, "unable to parse JWT: token signature is invalid: signature is invalid")
require.Nil(t, claims)
}
func TestAuthorizeCookieNoContent(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validEmptyHeader})
claims, err := authorize(r, keyfuncHS256, []string{}, defaultCookieName)
require.NoError(t, err)
require.Nil(t, claims.Mercure.Publish)
require.Nil(t, claims.Mercure.Subscribe)
}
func TestAuthorizeCookieNoContentRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validEmptyHeaderRsa})
claims, err := authorize(r, keyfuncRS256publicKey, []string{}, defaultCookieName)
require.NoError(t, err)
require.Nil(t, claims.Mercure.Publish)
require.Nil(t, claims.Mercure.Subscribe)
}
func TestAuthorizeCookie(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeader})
claims, err := authorize(r, keyfuncHS256, []string{}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeCookieRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeaderRsa})
claims, err := authorize(r, keyfuncRS256publicKey, []string{}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeCookieNoOriginNoReferer(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeader})
claims, err := authorize(r, keyfuncHS256, []string{}, defaultCookieName)
require.EqualError(t, err, `an "Origin" or a "Referer" HTTP header must be present to use the cookie-based authorization mechanism`)
require.Nil(t, claims)
}
func TestAuthorizeCookieNoOriginNoRefererRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeaderRsa})
claims, err := authorize(r, keyfuncRS256publicKey, []string{}, defaultCookieName)
require.EqualError(t, err, `an "Origin" or a "Referer" HTTP header must be present to use the cookie-based authorization mechanism`)
require.Nil(t, claims)
}
func TestAuthorizeCookieOriginNotAllowed(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.Header.Add("Origin", "http://example.com")
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeader})
claims, err := authorize(r, keyfuncHS256, []string{"http://example.net"}, defaultCookieName)
require.EqualError(t, err, `"http://example.com": origin not allowed to post updates`)
require.Nil(t, claims)
}
func TestAuthorizeCookieOriginNotAllowedRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.Header.Add("Origin", "http://example.com")
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeaderRsa})
claims, err := authorize(r, keyfuncRS256publicKey, []string{"http://example.net"}, defaultCookieName)
require.EqualError(t, err, `"http://example.com": origin not allowed to post updates`)
require.Nil(t, claims)
}
func TestAuthorizeCookieRefererNotAllowed(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.Header.Add("Referer", "http://example.com/foo/bar")
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeader})
claims, err := authorize(r, keyfuncHS256, []string{"http://example.net"}, defaultCookieName)
require.EqualError(t, err, `"http://example.com": origin not allowed to post updates`)
require.Nil(t, claims)
}
func TestAuthorizeCookieRefererNotAllowedRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.Header.Add("Referer", "http://example.com/foo/bar")
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeaderRsa})
claims, err := authorize(r, keyfuncRS256publicKey, []string{"http://example.net"}, defaultCookieName)
require.EqualError(t, err, `"http://example.com": origin not allowed to post updates`)
require.Nil(t, claims)
}
func TestAuthorizeCookieInvalidReferer(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.Header.Add("Referer", "http://192.168.0.%31/")
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeader})
claims, err := authorize(r, keyfuncHS256, []string{"http://example.net"}, defaultCookieName)
require.EqualError(t, err, `unable to parse referer: parse "http://192.168.0.%31/": invalid URL escape "%31"`)
require.Nil(t, claims)
}
func TestAuthorizeCookieInvalidRefererRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.Header.Add("Referer", "http://192.168.0.%31/")
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeaderRsa})
claims, err := authorize(r, keyfuncRS256publicKey, []string{"http://example.net"}, defaultCookieName)
require.EqualError(t, err, `unable to parse referer: parse "http://192.168.0.%31/": invalid URL escape "%31"`)
require.Nil(t, claims)
}
func TestAuthorizeCookieOriginHasPriority(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.Header.Add("Origin", "http://example.net")
r.Header.Add("Referer", "http://example.com")
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeader})
claims, err := authorize(r, keyfuncHS256, []string{"http://example.net"}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeCookieOriginHasPriorityRsa(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.Header.Add("Origin", "http://example.net")
r.Header.Add("Referer", "http://example.com")
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeaderRsa})
claims, err := authorize(r, keyfuncRS256publicKey, []string{"http://example.net"}, defaultCookieName)
require.NoError(t, err)
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
}
func TestAuthorizeAllOriginsAllowed(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.Header.Add("Origin", "http://example.com")
r.AddCookie(&http.Cookie{Name: defaultCookieName, Value: validFullHeader})
_, err := authorize(r, keyfuncHS256, []string{"*"}, defaultCookieName)
require.NoError(t, err)
}
func TestAuthorizeCustomCookieName(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
r.Header.Add("Origin", "http://example.com")
r.AddCookie(&http.Cookie{Name: "foo", Value: validFullHeader})
_, err := authorize(r, keyfuncHS256, []string{"*"}, "foo")
require.NoError(t, err)
}
func TestCanReceive(t *testing.T) {
tss := &TopicSelectorStore{}
assert.True(t, canReceive(tss, []string{"foo", "bar"}, []string{"foo", "bar"}))
assert.True(t, canReceive(tss, []string{"foo", "bar"}, []string{"bar"}))
assert.True(t, canReceive(tss, []string{"foo", "bar"}, []string{"*"}))
assert.False(t, canReceive(tss, []string{"foo", "bar"}, []string{}))
assert.False(t, canReceive(tss, []string{"foo", "bar"}, []string{"baz"}))
assert.False(t, canReceive(tss, []string{"foo", "bar"}, []string{"baz", "bat"}))
}
func TestCanDispatch(t *testing.T) {
tss := &TopicSelectorStore{}
assert.True(t, canDispatch(tss, []string{"foo", "bar"}, []string{"foo", "bar"}))
assert.True(t, canDispatch(tss, []string{"foo", "bar"}, []string{"*"}))
assert.False(t, canDispatch(tss, []string{"foo", "bar"}, []string{}))
assert.False(t, canDispatch(tss, []string{"foo", "bar"}, []string{"foo"}))
assert.False(t, canDispatch(tss, []string{"foo", "bar"}, []string{"baz"}))
assert.False(t, canDispatch(tss, []string{"foo", "bar"}, []string{"baz", "bat"}))
}