forked from xiaofei-du/krakend-jose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwk_client_test.go
81 lines (76 loc) · 2.19 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
package jose
import (
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"github.com/krakend/go-auth0"
"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, r *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)
}
}