-
Notifications
You must be signed in to change notification settings - Fork 49
/
jwk_client_test.go
144 lines (129 loc) · 3.36 KB
/
jwk_client_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
package jose
import (
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/krakend/go-auth0/v2"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
)
func TestJWKClient_globalCache(t *testing.T) {
jwk := []byte(`{ "keys": [{
"kty": "RSA",
"e": "AQAB",
"use": "sig",
"kid": "8-2-2PBmlHKMo5tizxp-uw9pFrQQamfa1M1ZYMrAFZI",
"alg": "RS256",
"n": "n6p2fLU7PLwMvJ-xeukn-f5wrAdyZ0ZaFa6kanQzVBofacLs2l4FVe6_bcjw4VGWM2Ct3WgelZQUYVkFbqePODpMnV0lV8U4hxbIpMEJOJqY3tK48_PBIdEkl02DN8LaucK1Y7GpOlUZFrWAOM68TyWJTjkyc-yx0ibu2MFaGQoXacV7239Yei_x68iGBpQa2f9SYv8U5nJINdI1CuyccQp991qeskJATgn-UVqQfOfHDsUA2qud2yNOf5QKkvqqPEH_IXuTtPcf_yzVuco9rhhUW8q5bC4R0BxjCv9w4b-Q_UKjKEXQK5UlAuiWqWgmQbQO9Ne94EDFpjlkCtil2Q"
}]}`)
var count uint64
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
// Content-Type defined in https://datatracker.ietf.org/doc/html/rfc7517#section-8.5.1
w.Header().Add("Content-Type", "application/jwk-set+json")
atomic.AddUint64(&count, 1)
w.Write(jwk)
}))
defer backend.Close()
opts := JWKClientOptions{
JWKClientOptions: auth0.JWKClientOptions{
URI: backend.URL,
},
}
te := auth0.FromMultiple(
auth0.RequestTokenExtractorFunc(auth0.FromHeader),
)
cfg := config.ExtraConfig{
ValidatorNamespace: map[string]interface{}{
"shared_cache_duration": 3,
},
}
if err := SetGlobalCacher(logging.NoOp, cfg); err != nil {
t.Error(err)
return
}
for i := 0; i < 10; i++ {
client := NewJWKClientWithCache(
opts,
te,
NewGlobalMemoryKeyCacher(1*time.Second, auth0.MaxCacheSizeNoCheck, opts.KeyIdentifyStrategy),
)
if _, err := client.GetKey("8-2-2PBmlHKMo5tizxp-uw9pFrQQamfa1M1ZYMrAFZI"); err != nil {
t.Error(err)
return
}
}
if count != 1 {
t.Errorf("invalid count %d", count)
return
}
<-time.After(4 * time.Second)
for i := 0; i < 10; i++ {
client := NewJWKClientWithCache(
opts,
te,
NewGlobalMemoryKeyCacher(1*time.Second, auth0.MaxCacheSizeNoCheck, opts.KeyIdentifyStrategy),
)
if _, err := client.GetKey("8-2-2PBmlHKMo5tizxp-uw9pFrQQamfa1M1ZYMrAFZI"); err != nil {
t.Error(err)
return
}
}
if count != 2 {
t.Errorf("invalid count %d", count)
}
}
func Test_memoryMissTracker(t *testing.T) {
now := time.Now()
uks := &memoryMissTracker{
mu: new(sync.Mutex),
keys: []unknownKey{
{
name: "key1",
time: now.Add(-time.Hour),
},
{
name: "key2",
time: now.Add(-2 * time.Minute),
},
{
name: "key3",
time: now.Add(-time.Second),
},
{
name: "key4",
time: now.Add(-time.Millisecond),
},
},
ttl: time.Minute,
}
if uks.Exists("key1") {
t.Errorf("key1 should not be present in list of misses %+v", uks)
}
if len(uks.keys) != 3 {
t.Errorf("wrong size %+v", uks)
}
if !uks.Exists("key3") {
t.Errorf("key3 should be present in list of misses %+v", uks)
}
if uks.Exists("key2") {
t.Errorf("key2 should not be present in list of misses %+v", uks)
}
if len(uks.keys) != 2 {
t.Errorf("wrong size %+v", uks)
}
if uks.Exists("key1") {
t.Errorf("key1 should not be present in list of misses %+v", uks)
}
if !uks.Exists("key4") {
t.Errorf("key4 should be present in list of misses %+v", uks)
}
if !uks.Exists("key3") {
t.Errorf("key3 should be present in list of misses %+v", uks)
}
if len(uks.keys) != 2 {
t.Errorf("wrong size %+v", uks)
}
}