Skip to content

Commit 2466969

Browse files
authored
Fix build after jwx v2 to v3 upgrade (#1367)
Signed-off-by: Juan Antonio Osorio <[email protected]>
1 parent 6478a71 commit 2466969

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/gofrs/flock v0.12.1
1616
github.com/google/go-containerregistry v0.20.6
1717
github.com/google/uuid v1.6.0
18-
github.com/lestrrat-go/jwx/v2 v2.1.6
18+
github.com/lestrrat-go/httprc/v3 v3.0.0
1919
github.com/lestrrat-go/jwx/v3 v3.0.10
2020
github.com/lmittmann/tint v1.1.2
2121
github.com/mark3labs/mcp-go v0.37.0
@@ -153,6 +153,7 @@ require (
153153
github.com/jpillora/backoff v1.0.0 // indirect
154154
github.com/json-iterator/go v1.1.12 // indirect
155155
github.com/klauspost/compress v1.18.0 // indirect
156+
github.com/lestrrat-go/option/v2 v2.0.0 // indirect
156157
github.com/letsencrypt/boulder v0.0.0-20240620165639-de9c06129bec // indirect
157158
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
158159
github.com/mailru/easyjson v0.9.0 // indirect
@@ -282,8 +283,6 @@ require (
282283
github.com/inconshreveable/mousetrap v1.1.0 // indirect
283284
github.com/lestrrat-go/blackmagic v1.0.4 // indirect
284285
github.com/lestrrat-go/httpcc v1.0.1 // indirect
285-
github.com/lestrrat-go/httprc v1.0.6 // indirect
286-
github.com/lestrrat-go/iter v1.0.2 // indirect
287286
github.com/lestrrat-go/option v1.0.1 // indirect
288287
github.com/moby/docker-image-spec v1.3.1 // indirect
289288
github.com/opencontainers/go-digest v1.0.0

go.sum

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,15 +1271,14 @@ github.com/lestrrat-go/blackmagic v1.0.4 h1:IwQibdnf8l2KoO+qC3uT4OaTWsW7tuRQXy9T
12711271
github.com/lestrrat-go/blackmagic v1.0.4/go.mod h1:6AWFyKNNj0zEXQYfTMPfZrAXUWUfTIZ5ECEUEJaijtw=
12721272
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
12731273
github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E=
1274-
github.com/lestrrat-go/httprc v1.0.6 h1:qgmgIRhpvBqexMJjA/PmwSvhNk679oqD1RbovdCGW8k=
1275-
github.com/lestrrat-go/httprc v1.0.6/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo=
1276-
github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
1277-
github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
1278-
github.com/lestrrat-go/jwx/v2 v2.1.6 h1:hxM1gfDILk/l5ylers6BX/Eq1m/pnxe9NBwW6lVfecA=
1279-
github.com/lestrrat-go/jwx/v2 v2.1.6/go.mod h1:Y722kU5r/8mV7fYDifjug0r8FK8mZdw0K0GpJw/l8pU=
1274+
github.com/lestrrat-go/httprc/v3 v3.0.0 h1:nZUx/zFg5uc2rhlu1L1DidGr5Sj02JbXvGSpnY4LMrc=
1275+
github.com/lestrrat-go/httprc/v3 v3.0.0/go.mod h1:k2U1QIiyVqAKtkffbg+cUmsyiPGQsb9aAfNQiNFuQ9Q=
1276+
github.com/lestrrat-go/jwx/v3 v3.0.10 h1:XuoCBhZBncRIjMQ32HdEc76rH0xK/Qv2wq5TBouYJDw=
12801277
github.com/lestrrat-go/jwx/v3 v3.0.10/go.mod h1:kNMedLgTpHvPJkK5EMVa1JFz+UVyY2dMmZKu3qjl/Pk=
12811278
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
12821279
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
1280+
github.com/lestrrat-go/option/v2 v2.0.0 h1:XxrcaJESE1fokHy3FpaQ/cXW8ZsIdWcdFzzLOcID3Ss=
1281+
github.com/lestrrat-go/option/v2 v2.0.0/go.mod h1:oSySsmzMoR0iRzCDCaUfsCzxQHUEuhOViQObyy7S6Vg=
12831282
github.com/letsencrypt/boulder v0.0.0-20240620165639-de9c06129bec h1:2tTW6cDth2TSgRbAhD7yjZzTQmcN25sDRPEeinR51yQ=
12841283
github.com/letsencrypt/boulder v0.0.0-20240620165639-de9c06129bec/go.mod h1:TmwEoGCwIti7BCeJ9hescZgRtatxRE+A72pCoPfmcfk=
12851284
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
@@ -1591,6 +1590,8 @@ github.com/transparency-dev/tessera v0.2.1-0.20250610150926-8ee4e93b2823 h1:s3p7
15911590
github.com/transparency-dev/tessera v0.2.1-0.20250610150926-8ee4e93b2823/go.mod h1:Jv2IDwG1q8QNXZTaI1X6QX8s96WlJn73ka2hT1n4N5c=
15921591
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
15931592
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
1593+
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
1594+
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
15941595
github.com/vbatts/tar-split v0.12.1 h1:CqKoORW7BUWBe7UL/iqTVvkTBOF8UvOMKOIZykxnnbo=
15951596
github.com/vbatts/tar-split v0.12.1/go.mod h1:eF6B6i6ftWQcDqEn3/iGFRFRo8cBIMSJVOpnNdfTMFA=
15961597
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=

pkg/auth/token.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
"time"
1414

1515
"github.com/golang-jwt/jwt/v5"
16-
"github.com/lestrrat-go/jwx/v2/jwk"
16+
"github.com/lestrrat-go/httprc/v3"
17+
"github.com/lestrrat-go/jwx/v3/jwk"
1718

1819
"github.com/stacklok/toolhive/pkg/logger"
1920
"github.com/stacklok/toolhive/pkg/networking"
@@ -195,10 +196,15 @@ func NewTokenValidator(ctx context.Context, config TokenValidatorConfig) (*Token
195196
config.httpClient = httpClient
196197

197198
// Create a new JWKS client with auto-refresh
198-
cache := jwk.NewCache(ctx)
199+
// In jwx v3, NewCache requires an httprc.Client
200+
httprcClient := httprc.NewClient(httprc.WithHTTPClient(httpClient))
201+
cache, err := jwk.NewCache(ctx, httprcClient)
202+
if err != nil {
203+
return nil, fmt.Errorf("failed to create JWKS cache: %w", err)
204+
}
199205

200-
// Register the JWKS URL with the cache using custom HTTP client
201-
err = cache.Register(jwksURL, jwk.WithHTTPClient(httpClient))
206+
// Register the JWKS URL with the cache
207+
err = cache.Register(ctx, jwksURL)
202208
if err != nil {
203209
return nil, fmt.Errorf("failed to register JWKS URL: %w", err)
204210
}
@@ -229,9 +235,10 @@ func (v *TokenValidator) getKeyFromJWKS(ctx context.Context, token *jwt.Token) (
229235
}
230236

231237
// Get the key set from the JWKS
232-
keySet, err := v.jwksClient.Get(ctx, v.jwksURL)
238+
// In jwx v3, Get is replaced with Lookup
239+
keySet, err := v.jwksClient.Lookup(ctx, v.jwksURL)
233240
if err != nil {
234-
return nil, fmt.Errorf("failed to get JWKS: %w", err)
241+
return nil, fmt.Errorf("failed to lookup JWKS: %w", err)
235242
}
236243

237244
// Get the key with the matching key ID
@@ -241,9 +248,10 @@ func (v *TokenValidator) getKeyFromJWKS(ctx context.Context, token *jwt.Token) (
241248
}
242249

243250
// Get the raw key
251+
// In jwx v3, Raw method is replaced with Export function
244252
var rawKey interface{}
245-
if err := key.Raw(&rawKey); err != nil {
246-
return nil, fmt.Errorf("failed to get raw key: %w", err)
253+
if err := jwk.Export(key, &rawKey); err != nil {
254+
return nil, fmt.Errorf("failed to export raw key: %w", err)
247255
}
248256

249257
return rawKey, nil

pkg/auth/token_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"time"
1616

1717
"github.com/golang-jwt/jwt/v5"
18-
"github.com/lestrrat-go/jwx/v2/jwk"
18+
"github.com/lestrrat-go/jwx/v3/jwk"
1919
)
2020

2121
const testKeyID = "test-key-1"
@@ -31,7 +31,7 @@ func TestTokenValidator(t *testing.T) {
3131
publicKey := &privateKey.PublicKey
3232

3333
// Create a key set with the public key
34-
key, err := jwk.FromRaw(publicKey)
34+
key, err := jwk.Import(publicKey)
3535
if err != nil {
3636
t.Fatalf("Failed to create JWK from public key: %v", err)
3737
}
@@ -76,7 +76,7 @@ func TestTokenValidator(t *testing.T) {
7676
}
7777

7878
// Force a refresh of the JWKS cache
79-
_, err = validator.jwksClient.Get(ctx, jwksServer.URL)
79+
_, err = validator.jwksClient.Lookup(ctx, jwksServer.URL)
8080
if err != nil {
8181
t.Fatalf("Failed to refresh JWKS cache: %v", err)
8282
}
@@ -173,7 +173,7 @@ func TestTokenValidatorMiddleware(t *testing.T) {
173173
publicKey := &privateKey.PublicKey
174174

175175
// Create a key set with the public key
176-
key, err := jwk.FromRaw(publicKey)
176+
key, err := jwk.Import(publicKey)
177177
if err != nil {
178178
t.Fatalf("Failed to create JWK from public key: %v", err)
179179
}
@@ -218,7 +218,7 @@ func TestTokenValidatorMiddleware(t *testing.T) {
218218
}
219219

220220
// Force a refresh of the JWKS cache
221-
_, err = validator.jwksClient.Get(ctx, jwksServer.URL)
221+
_, err = validator.jwksClient.Lookup(ctx, jwksServer.URL)
222222
if err != nil {
223223
t.Fatalf("Failed to refresh JWKS cache: %v", err)
224224
}
@@ -505,7 +505,7 @@ func TestNewTokenValidatorWithOIDCDiscovery(t *testing.T) {
505505
publicKey := &privateKey.PublicKey
506506

507507
// Create a key set with the public key
508-
key, err := jwk.FromRaw(publicKey)
508+
key, err := jwk.Import(publicKey)
509509
if err != nil {
510510
t.Fatalf("Failed to create JWK from public key: %v", err)
511511
}
@@ -608,7 +608,7 @@ func TestNewTokenValidatorWithOIDCDiscovery(t *testing.T) {
608608
}
609609

610610
// Force a refresh of the JWKS cache
611-
_, err = validator.jwksClient.Get(ctx, validator.jwksURL)
611+
_, err = validator.jwksClient.Lookup(ctx, validator.jwksURL)
612612
if err != nil {
613613
t.Fatalf("Failed to refresh JWKS cache: %v", err)
614614
}

0 commit comments

Comments
 (0)