go-jwks icon indicating copy to clipboard operation
go-jwks copied to clipboard

Use new context for refreshing the key in background

Open ejdem86grx opened this issue 3 years ago • 1 comments
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
}

ejdem86grx avatar Sep 12 '22 15:09 ejdem86grx

@andresvia yep, it turned out to be a problem for us and we opened this PR

bigkraig avatar Nov 30 '23 22:11 bigkraig