From ed795a0b5a163db401c36d554c307207f13b20fa Mon Sep 17 00:00:00 2001 From: Micah Parks <66095735+MicahParks@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:34:40 -0500 Subject: [PATCH] Add context to default functions (#108) --- README.md | 3 ++- go.mod | 2 +- go.sum | 4 ++-- keyfunc.go | 12 ++++++++++-- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a7fac1a..a71c9e8 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,8 @@ if err != nil { ``` When using the `keyfunc.NewDefault` function, the JWK Set will be automatically refreshed using -[`jwkset.NewDefaultHTTPClient`](https://pkg.go.dev/github.com/MicahParks/jwkset#NewHTTPClient). +[`jwkset.NewDefaultHTTPClient`](https://pkg.go.dev/github.com/MicahParks/jwkset#NewHTTPClient). This does launch a " +refresh goroutine". If you want the ability to end this goroutine, use the `keyfunc.NewDefaultHTTPClientCtx` function. It is also possible to create a `keyfunc.Keyfunc` from given keys like HMAC shared secrets. See `examples/hmac/main.go`. diff --git a/go.mod b/go.mod index 046bb12..3eb7b53 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/MicahParks/keyfunc/v3 go 1.21 require ( - github.com/MicahParks/jwkset v0.5.7 + github.com/MicahParks/jwkset v0.5.12 github.com/golang-jwt/jwt/v5 v5.2.0 ) diff --git a/go.sum b/go.sum index f7bd1ac..9cf3a89 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/MicahParks/jwkset v0.5.7 h1:eHUGJrSO8EetbNnSb7xhlYQ9mX0vQ7Ga9wN1HhGL3i4= -github.com/MicahParks/jwkset v0.5.7/go.mod h1:q8ptTGn/Z9c4MwbcfeCDssADeVQb3Pk7PnVxrvi+2QY= +github.com/MicahParks/jwkset v0.5.12 h1:wEwKZXB77yHFIHBtYoawNKIUwqC1X24S8tIhWutJHMA= +github.com/MicahParks/jwkset v0.5.12/go.mod h1:q8ptTGn/Z9c4MwbcfeCDssADeVQb3Pk7PnVxrvi+2QY= github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw= github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= diff --git a/keyfunc.go b/keyfunc.go index 5d02dc7..a7b06e0 100644 --- a/keyfunc.go +++ b/keyfunc.go @@ -55,9 +55,17 @@ func New(options Options) (Keyfunc, error) { // NewDefault creates a new Keyfunc with a default JWK Set storage and options. // -// This will launch "refresh goroutines" to automatically refresh the remote HTTP resources. +// This will launch "refresh goroutine" to automatically refresh the remote HTTP resources. func NewDefault(urls []string) (Keyfunc, error) { - client, err := jwkset.NewDefaultHTTPClient(urls) + return NewDefaultCtx(context.Background(), urls) +} + +// NewDefaultCtx creates a new Keyfunc with a default JWK Set storage and options. The context is used to end the +// "refresh goroutine". +// +// This will launch "refresh goroutine" to automatically refresh the remote HTTP resources. +func NewDefaultCtx(ctx context.Context, urls []string) (Keyfunc, error) { + client, err := jwkset.NewDefaultHTTPClientCtx(ctx, urls) if err != nil { return nil, err }