Struggle to understand JWKS Cache #957
-
Hi ! I struggle to understand this code: jwk_cache_example_test func ExampleJWK_CachedSet() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
const googleCerts = `https://www.googleapis.com/oauth2/v3/certs`
// The first steps are the same as examples/jwk_cache_example_test.go
c := jwk.NewCache(ctx)
c.Register(googleCerts, jwk.WithMinRefreshInterval(15*time.Minute))
_, err := c.Refresh(ctx, googleCerts)
if err != nil {
fmt.Printf("failed to refresh google JWKS: %s\n", err)
return
}
cached := jwk.NewCachedSet(c, googleCerts)
// cached fulfills the jwk.Set interface.
var _ jwk.Set = cached
// That means you can pass it to things like jws.WithKeySet,
// allowing you to pretend as if you are using the result of
//
// jwk.Fetch(ctx, googleCerts)
//
// But you are instead using a cached (and periodically refreshed)
// for each operation.
_ = jws.WithKeySet(cached)
} GoalI need to make 2 separate things in my code:
Side Questions
Thank you so much :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I don't really see a "main" question in your post. I can answer the "side" questions:
You could, but the first return value is a snapshot of the Also, we're only calling it for the side effect of loading the remote resource here; that's why the value is thrown away.
What do you mean by "also"? Do you mean to say that "c.Refresh() returns a jwk.Set as well, why create a cached set?" I kind of hinted in the previous answer, but |
Beta Was this translation helpful? Give feedback.
-
Reopen, because otherwise it will not be visible. |
Beta Was this translation helpful? Give feedback.
Thank you for your prompt and detailed answer.
I think I got it, let's see...
Here the intent of the call to
Refresh
is just to verify that the cache system is working, but we don't need to use thejwk.Set
yet_, err := c.Refresh(ctx, googleCerts)
On the other hand, the following line returns the
jwk.Set
I can use directly in my program without having to call any kind of function. The value ofcached
is automatically updated under the hood by your program.cached := jwk.NewCachedSet(c, googleCerts)
... from there, any time I need to parse and verify a token, I can use
cached
like below: