forked from fabpot/mercure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization_test.go
376 lines (304 loc) · 17.5 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
package mercure
import (
"net/http"
"testing"
"github.com/dgrijalva/jwt-go"
"github.com/stretchr/testify/assert"
)
const (
validEmptyHeader = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30._esyynAyo2Z6PyGe0mM_SuQ3c-C7sMQJ1YxVLvlj80A"
validFullHeader = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ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yk2XqeA==
-----END RSA PRIVATE KEY-----
`
func TestAuthorizeMultipleAuthorizationHeader(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", validEmptyHeader)
r.Header.Add("Authorization", validEmptyHeader)
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
assert.EqualError(t, err, "invalid \"Authorization\" HTTP header")
assert.Nil(t, claims)
}
func TestAuthorizeMultipleAuthorizationHeaderRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", validEmptyHeaderRsa)
r.Header.Add("Authorization", validEmptyHeaderRsa)
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodRS256}, []string{})
assert.EqualError(t, err, "invalid \"Authorization\" HTTP header")
assert.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderTooShort(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Bearer x")
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
assert.EqualError(t, err, "invalid \"Authorization\" HTTP header")
assert.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderNoBearer(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Greater "+validEmptyHeader)
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
assert.EqualError(t, err, "invalid \"Authorization\" HTTP header")
assert.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderNoBearerRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Greater "+validEmptyHeaderRsa)
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodRS256}, []string{})
assert.EqualError(t, err, "invalid \"Authorization\" HTTP header")
assert.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderInvalidAlg(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Bearer "+createDummyNoneSignedJWT())
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
assert.EqualError(t, err, "unable to parse JWT: 'none' signature type is not allowed")
assert.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderInvalidKey(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Bearer "+validEmptyHeader)
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
assert.EqualError(t, err, "unable to parse JWT: signature is invalid")
assert.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderInvalidKeyRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Bearer "+validEmptyHeaderRsa)
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodRS256}, []string{})
assert.EqualError(t, err, "unable to parse JWT: public key error")
assert.Nil(t, claims)
}
func TestAuthorizeAuthorizationHeaderNoContent(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Bearer "+validEmptyHeader)
claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{})
assert.Nil(t, claims.Mercure.Publish)
assert.Nil(t, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestAuthorizeAuthorizationHeaderNoContentRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Bearer "+validEmptyHeaderRsa)
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
assert.Nil(t, claims.Mercure.Publish)
assert.Nil(t, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestAuthorizeAuthorizationHeader(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Bearer "+validFullHeader)
claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{})
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestAuthorizeAuthorizationHeaderRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Bearer "+validFullHeaderRsa)
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestAuthorizeAuthorizationHeaderNamespacedRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Bearer "+validFullHeaderNamespacedRsa)
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestAuthorizeAuthorizationHeaderWrongAlgorithm(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Authorization", "Bearer "+validFullHeaderRsa)
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), nil}, []string{})
assert.EqualError(t, err, "unable to parse JWT: <nil>: unexpected signing method")
assert.Nil(t, claims)
}
func TestAuthorizeCookieInvalidAlg(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: createDummyNoneSignedJWT()})
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
assert.EqualError(t, err, "unable to parse JWT: 'none' signature type is not allowed")
assert.Nil(t, claims)
}
func TestAuthorizeCookieInvalidKey(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validEmptyHeader})
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
assert.EqualError(t, err, "unable to parse JWT: signature is invalid")
assert.Nil(t, claims)
}
func TestAuthorizeCookieEmptyKeyRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validEmptyHeaderRsa})
claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodRS256}, []string{})
assert.EqualError(t, err, "unable to parse JWT: public key error")
assert.Nil(t, claims)
}
func TestAuthorizeCookieInvalidKeyRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validEmptyHeaderRsa})
claims, err := authorize(r, &jwtConfig{[]byte(privateKeyRsa), jwt.SigningMethodRS256}, []string{})
assert.EqualError(t, err, "unable to parse JWT: unable to parse PKIX public key: asn1: structure error: tags don't match (16 vs {class:0 tag:2 length:1 isCompound:false}) {optional:false explicit:false application:false private:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} AlgorithmIdentifier @2")
assert.Nil(t, claims)
}
func TestAuthorizeCookieNoContent(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validEmptyHeader})
claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{})
assert.Nil(t, claims.Mercure.Publish)
assert.Nil(t, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestAuthorizeCookieNoContentRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validEmptyHeaderRsa})
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
assert.Nil(t, claims.Mercure.Publish)
assert.Nil(t, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestAuthorizeCookie(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})
claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{})
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestAuthorizeCookieRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestAuthorizeCookieNoOriginNoReferer(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})
claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{})
assert.EqualError(t, err, "an \"Origin\" or a \"Referer\" HTTP header must be present to use the cookie-based authorization mechanism")
assert.Nil(t, claims)
}
func TestAuthorizeCookieNoOriginNoRefererRsa(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
assert.EqualError(t, err, "an \"Origin\" or a \"Referer\" HTTP header must be present to use the cookie-based authorization mechanism")
assert.Nil(t, claims)
}
func TestAuthorizeCookieOriginNotAllowed(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Origin", "http://example.com")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})
claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{"http://example.net"})
assert.EqualError(t, err, `"http://example.com": origin not allowed to post updates`)
assert.Nil(t, claims)
}
func TestAuthorizeCookieOriginNotAllowedRsa(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Origin", "http://example.com")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{"http://example.net"})
assert.EqualError(t, err, `"http://example.com": origin not allowed to post updates`)
assert.Nil(t, claims)
}
func TestAuthorizeCookieRefererNotAllowed(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Referer", "http://example.com/foo/bar")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})
claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{"http://example.net"})
assert.EqualError(t, err, `"http://example.com": origin not allowed to post updates`)
assert.Nil(t, claims)
}
func TestAuthorizeCookieRefererNotAllowedRsa(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Referer", "http://example.com/foo/bar")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{"http://example.net"})
assert.EqualError(t, err, `"http://example.com": origin not allowed to post updates`)
assert.Nil(t, claims)
}
func TestAuthorizeCookieInvalidReferer(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Referer", "http://192.168.0.%31/")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})
claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{"http://example.net"})
assert.EqualError(t, err, `unable to parse referer: parse "http://192.168.0.%31/": invalid URL escape "%31"`)
assert.Nil(t, claims)
}
func TestAuthorizeCookieInvalidRefererRsa(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Referer", "http://192.168.0.%31/")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{"http://example.net"})
assert.EqualError(t, err, `unable to parse referer: parse "http://192.168.0.%31/": invalid URL escape "%31"`)
assert.Nil(t, claims)
}
func TestAuthorizeCookieOriginHasPriority(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Origin", "http://example.net")
r.Header.Add("Referer", "http://example.com")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})
claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{"http://example.net"})
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestAuthorizeCookieOriginHasPriorityRsa(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r.Header.Add("Origin", "http://example.net")
r.Header.Add("Referer", "http://example.com")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})
claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{"http://example.net"})
assert.Equal(t, []string{"foo", "bar"}, claims.Mercure.Publish)
assert.Equal(t, []string{"foo", "baz"}, claims.Mercure.Subscribe)
assert.Nil(t, err)
}
func TestCanReceive(t *testing.T) {
s := NewTopicSelectorStore()
assert.True(t, canReceive(s, []string{"foo", "bar"}, []string{"foo", "bar"}, true))
assert.True(t, canReceive(s, []string{"foo", "bar"}, []string{"bar"}, true))
assert.True(t, canReceive(s, []string{"foo", "bar"}, []string{"*"}, true))
assert.False(t, canReceive(s, []string{"foo", "bar"}, []string{}, true))
assert.False(t, canReceive(s, []string{"foo", "bar"}, []string{"baz"}, true))
assert.False(t, canReceive(s, []string{"foo", "bar"}, []string{"baz", "bat"}, true))
}
func TestCanDispatch(t *testing.T) {
s := NewTopicSelectorStore()
assert.True(t, canDispatch(s, []string{"foo", "bar"}, []string{"foo", "bar"}))
assert.True(t, canDispatch(s, []string{"foo", "bar"}, []string{"*"}))
assert.False(t, canDispatch(s, []string{"foo", "bar"}, []string{}))
assert.False(t, canDispatch(s, []string{"foo", "bar"}, []string{"foo"}))
assert.False(t, canDispatch(s, []string{"foo", "bar"}, []string{"baz"}))
assert.False(t, canDispatch(s, []string{"foo", "bar"}, []string{"baz", "bat"}))
}