MicahParks / keyfunc

Create a jwt.Keyfunc for JWT parsing with a JWK Set or given cryptographic keys (like HMAC) in Golang.
Apache License 2.0
274 stars 45 forks source link

Refresh JWKS manually without background gofunc #80

Closed mariotoffia closed 1 year ago

mariotoffia commented 1 year ago

Hi and thanks for this great library! :)

We're converting to lambda only solution and I wonder if there's a good way of allowing external code to manually Refresh the JWKS due to lambda is not allowed to compute between calls.

I don't know If you think this is in scope for your library, but if it is I would be glad to supply with a PR if you could help me with some directions how you want it to be done.

My first thought was to make refresh spin up the background worker and when finished cancel it.

Cheers, Mario

MicahParks commented 1 year ago

Hello Mario, thank you for the kind words :slightly_smiling_face:

The background goroutine and its behavior are controlled by the keyfunc.Options data structure passed to keyfunc.Get. keyfunc.Options utilizes the zero values of its fields. When fields are left blank or their zero value, the default behavior will occur. By default, no background goroutine will be launched and the JWK Set will be fetched only once.

Here is an example:

jwks, err := keyfunc.Get("https://jwks-service.appspot.com/.well-known/jwks.json", keyfunc.Options{})

The above example will get the JWK Set located at https://jwks-service.appspot.com/.well-known/jwks.json. The resulting *keyfunc.JWKS can be accessed for its jwt.Keyfunc method normally.