forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_authentication_jwks_strategy_test.go
178 lines (147 loc) · 4.61 KB
/
client_authentication_jwks_strategy_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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package fosite
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/dgraph-io/ristretto"
"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
"github.com/ory/fosite/internal/gen"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/square/go-jose.v2"
)
func initServerWithKey(t *testing.T) *httptest.Server {
var set *jose.JSONWebKeySet
var h http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
require.NoError(t, json.NewEncoder(w).Encode(set))
}
ts := httptest.NewServer(h)
set = &jose.JSONWebKeySet{
Keys: []jose.JSONWebKey{
{
KeyID: "bar",
Use: "sig",
Key: &gen.MustRSAKey().PublicKey,
},
},
}
t.Cleanup(ts.Close)
return ts
}
var errRoundTrip = errors.New("roundtrip error")
type failingTripper struct{}
func (r *failingTripper) RoundTrip(*http.Request) (*http.Response, error) {
return nil, errRoundTrip
}
func TestDefaultJWKSFetcherStrategy(t *testing.T) {
ctx := context.Background()
var h http.HandlerFunc
s := NewDefaultJWKSFetcherStrategy()
t.Run("case=fetching", func(t *testing.T) {
var set *jose.JSONWebKeySet
h = func(w http.ResponseWriter, r *http.Request) {
require.NoError(t, json.NewEncoder(w).Encode(set))
}
ts := httptest.NewServer(h)
defer ts.Close()
set = &jose.JSONWebKeySet{
Keys: []jose.JSONWebKey{
{
KeyID: "foo",
Use: "sig",
Key: &gen.MustRSAKey().PublicKey,
},
},
}
keys, err := s.Resolve(ctx, ts.URL, false)
require.NoError(t, err)
assert.True(t, len(keys.Key("foo")) == 1)
set = &jose.JSONWebKeySet{
Keys: []jose.JSONWebKey{
{
KeyID: "bar",
Use: "sig",
Key: &gen.MustRSAKey().PublicKey,
},
},
}
keys, err = s.Resolve(ctx, ts.URL, false)
require.NoError(t, err)
assert.Len(t, keys.Keys, 1, "%+v", keys)
assert.True(t, len(keys.Key("foo")) == 1)
assert.True(t, len(keys.Key("bar")) == 0)
keys, err = s.Resolve(ctx, ts.URL, true)
require.NoError(t, err)
assert.True(t, len(keys.Key("foo")) == 0)
assert.True(t, len(keys.Key("bar")) == 1)
})
t.Run("JWKSFetcherWithCache", func(t *testing.T) {
ts := initServerWithKey(t)
cache, _ := ristretto.NewCache(&ristretto.Config{NumCounters: 10 * 1000, MaxCost: 1000, BufferItems: 64})
location := ts.URL
expected := &jose.JSONWebKeySet{}
require.True(t, cache.Set(defaultJWKSFetcherStrategyCachePrefix+location, expected, 1))
cache.Wait()
s := NewDefaultJWKSFetcherStrategy(JWKSFetcherWithCache(cache))
actual, err := s.Resolve(ctx, location, false)
require.NoError(t, err)
assert.Equal(t, expected, actual)
})
t.Run("JWKSFetcherWithTTL", func(t *testing.T) {
ts := initServerWithKey(t)
s := NewDefaultJWKSFetcherStrategy(JKWKSFetcherWithDefaultTTL(time.Nanosecond))
_, err := s.Resolve(ctx, ts.URL, false)
require.NoError(t, err)
s.(*DefaultJWKSFetcherStrategy).cache.Wait()
_, ok := s.(*DefaultJWKSFetcherStrategy).cache.Get(defaultJWKSFetcherStrategyCachePrefix + ts.URL)
assert.Falsef(t, ok, "expected cache to be empty")
})
t.Run("JWKSFetcherWithHTTPClient", func(t *testing.T) {
rt := retryablehttp.NewClient()
rt.RetryMax = 0
rt.HTTPClient = &http.Client{Transport: new(failingTripper)}
s := NewDefaultJWKSFetcherStrategy(JWKSFetcherWithHTTPClient(rt))
_, err := s.Resolve(ctx, "https://google.com", false)
require.ErrorIs(t, err, errRoundTrip)
})
t.Run("JWKSFetcherWithHTTPClientSource", func(t *testing.T) {
rt := retryablehttp.NewClient()
rt.RetryMax = 0
rt.HTTPClient = &http.Client{Transport: new(failingTripper)}
s := NewDefaultJWKSFetcherStrategy(
JWKSFetcherWithHTTPClient(retryablehttp.NewClient()),
JWKSFetcherWithHTTPClientSource(func(ctx context.Context) *retryablehttp.Client {
return rt
}))
_, err := s.Resolve(ctx, "https://www.google.com", false)
require.ErrorIs(t, err, errRoundTrip)
})
t.Run("case=error_network", func(t *testing.T) {
s := NewDefaultJWKSFetcherStrategy()
h = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
}
ts := httptest.NewServer(h)
defer ts.Close()
_, err := s.Resolve(context.Background(), ts.URL, true)
require.Error(t, err)
_, err = s.Resolve(context.Background(), "$%/19", true)
require.Error(t, err)
})
t.Run("case=error_encoding", func(t *testing.T) {
s := NewDefaultJWKSFetcherStrategy()
h = func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("[]"))
}
ts := httptest.NewServer(h)
defer ts.Close()
_, err := s.Resolve(context.Background(), ts.URL, true)
require.Error(t, err)
})
}