-
-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathidentity_test.go
More file actions
67 lines (55 loc) 路 2.36 KB
/
Copy pathidentity_test.go
File metadata and controls
67 lines (55 loc) 路 2.36 KB
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
package mercure
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// With no static resource identifier, the expected audience is derived from the
// public URL the client contacted, so a token minted for one host authorizes
// there and is rejected on another.
func TestAuthorizeDerivesAudienceFromRequestHost(t *testing.T) {
t.Parallel()
tms, err := NewTopicMatcherStore(0)
require.NoError(t, err)
hub, err := NewHub(t.Context(), testIssuerOption(), WithTopicMatcherStore(tms))
require.NoError(t, err)
details := []authorizationDetail{{
Type: authorizationDetailTypeMercure,
Actions: []mercureAction{actionSubscribe},
Topics: stringsToDetailTopics([]string{"https://example.com/books/1"}),
}}
token := mintAccessToken([]byte("subscriber"), "https://a.example.com/.well-known/mercure", details)
// Accepted on the host the token's aud names.
r := httptest.NewRequest(http.MethodGet, "https://a.example.com"+defaultHubURL, nil)
r.Header.Set("Authorization", bearerPrefix+token)
c, err := hub.authorize(r, false)
require.NoError(t, err)
assert.NotNil(t, c)
// Rejected on a different host: the derived audience no longer matches.
r = httptest.NewRequest(http.MethodGet, "https://b.example.com"+defaultHubURL, nil)
r.Header.Set("Authorization", bearerPrefix+token)
_, err = hub.authorize(r, false)
require.ErrorIs(t, err, ErrInvalidJWT)
}
// A request whose origin is not in the public-URL allowlist is rejected with
// 421 before any identity is derived from it; an allowed origin passes the
// guard. The scheme is pinned: an https allowlist rejects an http request.
func TestServeHTTPRejectsOriginNotInAllowlist(t *testing.T) {
t.Parallel()
hub := createDummy(t, WithPublicURLs([]string{"https://allowed.example.com"}))
for _, target := range []string{
"https://denied.example.com" + defaultHubURL,
"http://allowed.example.com" + defaultHubURL, // right host, wrong scheme
} {
r := httptest.NewRequest(http.MethodGet, target, nil)
w := httptest.NewRecorder()
hub.ServeHTTP(w, r)
assert.Equal(t, http.StatusMisdirectedRequest, w.Result().StatusCode, target)
}
r := httptest.NewRequest(http.MethodGet, "https://allowed.example.com"+defaultHubURL, nil)
w := httptest.NewRecorder()
hub.ServeHTTP(w, r)
assert.NotEqual(t, http.StatusMisdirectedRequest, w.Result().StatusCode)
}