go-jwks
go-jwks copied to clipboard
Use new context for refreshing the key in background
trafficstars
In its current shape the background refresh will never run, if the ctx passed to the go-jwks client will be canceled or expires before the library gets the new key.
Example code:
func External() {
// 1. create a context with cancel
ctx, cancel := context.WithCancel(context.Background())
// 3. cancel is called before the method has finished, and this is causing the refreshKey method to fail because the context is cancelled at this point
defer cancel()
key, err := jwks.GetKey(ctx)
// 2. the key is in the cache, but it should be refreshed, so GetKey starts a goroutine with the ctx passed to the refreshKey method
return
}
@andresvia yep, it turned out to be a problem for us and we opened this PR