Skip to content

Commit ddd9e90

Browse files
authored
update jwx to v3 (oapi-codegen#2300)
1 parent 357f00c commit ddd9e90

8 files changed

Lines changed: 92 additions & 87 deletions

File tree

examples/authenticated-api/echo/server/fake_jws.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"crypto/ecdsa"
55
"fmt"
66

7-
"github.com/lestrrat-go/jwx/jwa"
8-
"github.com/lestrrat-go/jwx/jwk"
9-
"github.com/lestrrat-go/jwx/jws"
10-
"github.com/lestrrat-go/jwx/jwt"
7+
"github.com/lestrrat-go/jwx/v3/jwa"
8+
"github.com/lestrrat-go/jwx/v3/jwk"
9+
"github.com/lestrrat-go/jwx/v3/jws"
10+
"github.com/lestrrat-go/jwx/v3/jwt"
1111
"github.com/oapi-codegen/oapi-codegen/v2/pkg/ecdsafile"
1212
)
1313

@@ -46,14 +46,12 @@ func NewFakeAuthenticator() (*FakeAuthenticator, error) {
4646
}
4747

4848
set := jwk.NewSet()
49-
pubKey := jwk.NewECDSAPublicKey()
50-
51-
err = pubKey.FromRaw(&privKey.PublicKey)
49+
pubKey, err := jwk.Import(&privKey.PublicKey)
5250
if err != nil {
5351
return nil, fmt.Errorf("parsing jwk key: %w", err)
5452
}
5553

56-
err = pubKey.Set(jwk.AlgorithmKey, jwa.ES256)
54+
err = pubKey.Set(jwk.AlgorithmKey, jwa.ES256())
5755
if err != nil {
5856
return nil, fmt.Errorf("setting key algorithm: %w", err)
5957
}
@@ -63,7 +61,10 @@ func NewFakeAuthenticator() (*FakeAuthenticator, error) {
6361
return nil, fmt.Errorf("setting key ID: %w", err)
6462
}
6563

66-
set.Add(pubKey)
64+
err = set.AddKey(pubKey)
65+
if err != nil {
66+
return nil, fmt.Errorf("adding public key to key set: %w", err)
67+
}
6768

6869
return &FakeAuthenticator{PrivateKey: privKey, KeySet: set}, nil
6970
}
@@ -78,7 +79,7 @@ func (f *FakeAuthenticator) ValidateJWS(jwsString string) (jwt.Token, error) {
7879
// SignToken takes a JWT and signs it with our private key, returning a JWS.
7980
func (f *FakeAuthenticator) SignToken(t jwt.Token) ([]byte, error) {
8081
hdr := jws.NewHeaders()
81-
if err := hdr.Set(jws.AlgorithmKey, jwa.ES256); err != nil {
82+
if err := hdr.Set(jws.AlgorithmKey, jwa.ES256()); err != nil {
8283
return nil, fmt.Errorf("setting algorithm: %w", err)
8384
}
8485
if err := hdr.Set(jws.TypeKey, "JWT"); err != nil {
@@ -87,7 +88,7 @@ func (f *FakeAuthenticator) SignToken(t jwt.Token) ([]byte, error) {
8788
if err := hdr.Set(jws.KeyIDKey, KeyID); err != nil {
8889
return nil, fmt.Errorf("setting Key ID: %w", err)
8990
}
90-
return jwt.Sign(t, jwa.ES256, f.PrivateKey, jwt.WithHeaders(hdr))
91+
return jwt.Sign(t, jwt.WithKey(jwa.ES256(), f.PrivateKey, jws.WithProtectedHeaders(hdr)))
9192
}
9293

9394
// CreateJWSWithClaims is a helper function to create JWT's with the specified

examples/authenticated-api/echo/server/jwt_authenticator.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99

1010
"github.com/getkin/kin-openapi/openapi3filter"
11-
"github.com/lestrrat-go/jwx/jwt"
11+
"github.com/lestrrat-go/jwx/v3/jwt"
1212
middleware "github.com/oapi-codegen/echo-middleware"
1313
)
1414

@@ -89,30 +89,27 @@ func Authenticate(v JWSValidator, ctx context.Context, input *openapi3filter.Aut
8989
// as a list under the "perms" claim, short for permissions, to keep the token
9090
// shorter.
9191
func GetClaimsFromToken(t jwt.Token) ([]string, error) {
92-
rawPerms, found := t.Get(PermissionsClaim)
93-
if !found {
92+
if !t.Has(PermissionsClaim) {
9493
// If the perms aren't found, it means that the token has none, but it has
9594
// passed signature validation by now, so it's a valid token, so we return
9695
// the empty list.
9796
return make([]string, 0), nil
9897
}
9998

100-
// rawPerms will be an untyped JSON list, so we need to convert it to
101-
// a string list.
102-
rawList, ok := rawPerms.([]interface{})
103-
if !ok {
104-
return nil, fmt.Errorf("'%s' claim is unexpected type'", PermissionsClaim)
99+
var rawList []interface{}
100+
if err := t.Get(PermissionsClaim, &rawList); err != nil {
101+
return nil, fmt.Errorf("getting %q claim: %w", PermissionsClaim, err)
105102
}
106103

107104
claims := make([]string, len(rawList))
108-
109105
for i, rawClaim := range rawList {
110-
var ok bool
111-
claims[i], ok = rawClaim.(string)
106+
claim, ok := rawClaim.(string)
112107
if !ok {
113108
return nil, fmt.Errorf("%s[%d] is not a string", PermissionsClaim, i)
114109
}
110+
claims[i] = claim
115111
}
112+
116113
return claims, nil
117114
}
118115

examples/authenticated-api/stdhttp/go.mod

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ replace github.com/oapi-codegen/oapi-codegen/v2 => ../../../
66

77
require (
88
github.com/getkin/kin-openapi v0.134.0
9-
github.com/lestrrat-go/jwx v1.2.31
9+
github.com/lestrrat-go/jwx/v3 v3.0.13
1010
github.com/oapi-codegen/nethttp-middleware v1.1.2
1111
github.com/oapi-codegen/oapi-codegen/v2 v2.0.0-00010101000000-000000000000
1212
github.com/oapi-codegen/testutil v1.1.0
@@ -22,25 +22,28 @@ require (
2222
github.com/goccy/go-json v0.10.3 // indirect
2323
github.com/gorilla/mux v1.8.1 // indirect
2424
github.com/josharian/intern v1.0.0 // indirect
25-
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect
26-
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
25+
github.com/lestrrat-go/blackmagic v1.0.4 // indirect
26+
github.com/lestrrat-go/dsig v1.0.0 // indirect
27+
github.com/lestrrat-go/dsig-secp256k1 v1.0.0 // indirect
2728
github.com/lestrrat-go/httpcc v1.0.1 // indirect
28-
github.com/lestrrat-go/iter v1.0.2 // indirect
29-
github.com/lestrrat-go/option v1.0.1 // indirect
29+
github.com/lestrrat-go/httprc/v3 v3.0.2 // indirect
30+
github.com/lestrrat-go/option/v2 v2.0.0 // indirect
3031
github.com/mailru/easyjson v0.9.1 // indirect
3132
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
3233
github.com/oasdiff/yaml v0.0.0-20260313112342-a3ea61cb4d4c // indirect
3334
github.com/oasdiff/yaml3 v0.0.0-20260224194419-61cd415a242b // indirect
3435
github.com/perimeterx/marshmallow v1.1.5 // indirect
35-
github.com/pkg/errors v0.9.1 // indirect
3636
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
37+
github.com/segmentio/asm v1.2.1 // indirect
3738
github.com/speakeasy-api/jsonpath v0.6.0 // indirect
3839
github.com/speakeasy-api/openapi-overlay v0.10.3 // indirect
40+
github.com/valyala/fastjson v1.6.7 // indirect
3941
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
4042
github.com/woodsbury/decimal128 v1.4.0 // indirect
41-
golang.org/x/crypto v0.32.0 // indirect
43+
golang.org/x/crypto v0.46.0 // indirect
4244
golang.org/x/mod v0.33.0 // indirect
4345
golang.org/x/sync v0.19.0 // indirect
46+
golang.org/x/sys v0.41.0 // indirect
4447
golang.org/x/text v0.34.0 // indirect
4548
golang.org/x/tools v0.42.0 // indirect
4649
gopkg.in/yaml.v2 v2.4.0 // indirect

examples/authenticated-api/stdhttp/go.sum

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,20 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
5353
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
5454
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
5555
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
56-
github.com/lestrrat-go/backoff/v2 v2.0.8 h1:oNb5E5isby2kiro9AgdHLv5N5tint1AnDVVf2E2un5A=
57-
github.com/lestrrat-go/backoff/v2 v2.0.8/go.mod h1:rHP/q/r9aT27n24JQLa7JhSQZCKBBOiM/uP402WwN8Y=
58-
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
59-
github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
56+
github.com/lestrrat-go/blackmagic v1.0.4 h1:IwQibdnf8l2KoO+qC3uT4OaTWsW7tuRQXy9TRN9QanA=
57+
github.com/lestrrat-go/blackmagic v1.0.4/go.mod h1:6AWFyKNNj0zEXQYfTMPfZrAXUWUfTIZ5ECEUEJaijtw=
58+
github.com/lestrrat-go/dsig v1.0.0 h1:OE09s2r9Z81kxzJYRn07TFM9XA4akrUdoMwr0L8xj38=
59+
github.com/lestrrat-go/dsig v1.0.0/go.mod h1:dEgoOYYEJvW6XGbLasr8TFcAxoWrKlbQvmJgCR0qkDo=
60+
github.com/lestrrat-go/dsig-secp256k1 v1.0.0 h1:JpDe4Aybfl0soBvoVwjqDbp+9S1Y2OM7gcrVVMFPOzY=
61+
github.com/lestrrat-go/dsig-secp256k1 v1.0.0/go.mod h1:CxUgAhssb8FToqbL8NjSPoGQlnO4w3LG1P0qPWQm/NU=
6062
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
6163
github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E=
62-
github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
63-
github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
64-
github.com/lestrrat-go/jwx v1.2.31 h1:/OM9oNl/fzyldpv5HKZ9m7bTywa7COUfg8gujd9nJ54=
65-
github.com/lestrrat-go/jwx v1.2.31/go.mod h1:eQJKoRwWcLg4PfD5CFA5gIZGxhPgoPYq9pZISdxLf0c=
66-
github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
67-
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
68-
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
64+
github.com/lestrrat-go/httprc/v3 v3.0.2 h1:7u4HUaD0NQbf2/n5+fyp+T10hNCsAnwKfqn4A4Baif0=
65+
github.com/lestrrat-go/httprc/v3 v3.0.2/go.mod h1:mSMtkZW92Z98M5YoNNztbRGxbXHql7tSitCvaxvo9l0=
66+
github.com/lestrrat-go/jwx/v3 v3.0.13 h1:AdHKiPIYeCSnOJtvdpipPg/0SuFh9rdkN+HF3O0VdSk=
67+
github.com/lestrrat-go/jwx/v3 v3.0.13/go.mod h1:2m0PV1A9tM4b/jVLMx8rh6rBl7F6WGb3EG2hufN9OQU=
68+
github.com/lestrrat-go/option/v2 v2.0.0 h1:XxrcaJESE1fokHy3FpaQ/cXW8ZsIdWcdFzzLOcID3Ss=
69+
github.com/lestrrat-go/option/v2 v2.0.0/go.mod h1:oSySsmzMoR0iRzCDCaUfsCzxQHUEuhOViQObyy7S6Vg=
6970
github.com/mailru/easyjson v0.9.1 h1:LbtsOm5WAswyWbvTEOqhypdPeZzHavpZx96/n553mR8=
7071
github.com/mailru/easyjson v0.9.1/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
7172
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
@@ -95,11 +96,11 @@ github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
9596
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
9697
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
9798
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
98-
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
99-
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10099
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
101100
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
102101
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
102+
github.com/segmentio/asm v1.2.1 h1:DTNbBqs57ioxAD4PrArqftgypG4/qNpXoJx8TVXxPR0=
103+
github.com/segmentio/asm v1.2.1/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
103104
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
104105
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
105106
github.com/speakeasy-api/jsonpath v0.6.0 h1:IhtFOV9EbXplhyRqsVhHoBmmYjblIRh5D1/g8DHMXJ8=
@@ -109,12 +110,13 @@ github.com/speakeasy-api/openapi-overlay v0.10.3/go.mod h1:RJjV0jbUHqXLS0/Mxv5XE
109110
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
110111
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
111112
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
112-
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
113113
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
114114
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
115115
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
116116
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
117117
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
118+
github.com/valyala/fastjson v1.6.7 h1:ZE4tRy0CIkh+qDc5McjatheGX2czdn8slQjomexVpBM=
119+
github.com/valyala/fastjson v1.6.7/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
118120
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
119121
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ=
120122
github.com/woodsbury/decimal128 v1.4.0 h1:xJATj7lLu4f2oObouMt2tgGiElE5gO6mSWUjQsBgUlc=
@@ -123,8 +125,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
123125
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
124126
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
125127
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
126-
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
127-
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
128+
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
129+
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
128130
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
129131
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
130132
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=

examples/authenticated-api/stdhttp/server/fake_jws.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"crypto/ecdsa"
55
"fmt"
66

7-
"github.com/lestrrat-go/jwx/jwa"
8-
"github.com/lestrrat-go/jwx/jwk"
9-
"github.com/lestrrat-go/jwx/jws"
10-
"github.com/lestrrat-go/jwx/jwt"
7+
"github.com/lestrrat-go/jwx/v3/jwa"
8+
"github.com/lestrrat-go/jwx/v3/jwk"
9+
"github.com/lestrrat-go/jwx/v3/jws"
10+
"github.com/lestrrat-go/jwx/v3/jwt"
1111
"github.com/oapi-codegen/oapi-codegen/v2/pkg/ecdsafile"
1212
)
1313

@@ -46,14 +46,12 @@ func NewFakeAuthenticator() (*FakeAuthenticator, error) {
4646
}
4747

4848
set := jwk.NewSet()
49-
pubKey := jwk.NewECDSAPublicKey()
50-
51-
err = pubKey.FromRaw(&privKey.PublicKey)
49+
pubKey, err := jwk.Import(&privKey.PublicKey)
5250
if err != nil {
5351
return nil, fmt.Errorf("parsing jwk key: %w", err)
5452
}
5553

56-
err = pubKey.Set(jwk.AlgorithmKey, jwa.ES256)
54+
err = pubKey.Set(jwk.AlgorithmKey, jwa.ES256())
5755
if err != nil {
5856
return nil, fmt.Errorf("setting key algorithm: %w", err)
5957
}
@@ -63,7 +61,10 @@ func NewFakeAuthenticator() (*FakeAuthenticator, error) {
6361
return nil, fmt.Errorf("setting key ID: %w", err)
6462
}
6563

66-
set.Add(pubKey)
64+
err = set.AddKey(pubKey)
65+
if err != nil {
66+
return nil, fmt.Errorf("adding public key to key set: %w", err)
67+
}
6768

6869
return &FakeAuthenticator{PrivateKey: privKey, KeySet: set}, nil
6970
}
@@ -78,7 +79,7 @@ func (f *FakeAuthenticator) ValidateJWS(jwsString string) (jwt.Token, error) {
7879
// SignToken takes a JWT and signs it with our private key, returning a JWS.
7980
func (f *FakeAuthenticator) SignToken(t jwt.Token) ([]byte, error) {
8081
hdr := jws.NewHeaders()
81-
if err := hdr.Set(jws.AlgorithmKey, jwa.ES256); err != nil {
82+
if err := hdr.Set(jws.AlgorithmKey, jwa.ES256()); err != nil {
8283
return nil, fmt.Errorf("setting algorithm: %w", err)
8384
}
8485
if err := hdr.Set(jws.TypeKey, "JWT"); err != nil {
@@ -87,7 +88,7 @@ func (f *FakeAuthenticator) SignToken(t jwt.Token) ([]byte, error) {
8788
if err := hdr.Set(jws.KeyIDKey, KeyID); err != nil {
8889
return nil, fmt.Errorf("setting Key ID: %w", err)
8990
}
90-
return jwt.Sign(t, jwa.ES256, f.PrivateKey, jwt.WithHeaders(hdr))
91+
return jwt.Sign(t, jwt.WithKey(jwa.ES256(), f.PrivateKey, jws.WithProtectedHeaders(hdr)))
9192
}
9293

9394
// CreateJWSWithClaims is a helper function to create JWT's with the specified

examples/authenticated-api/stdhttp/server/jwt_authenticator.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99

1010
"github.com/getkin/kin-openapi/openapi3filter"
11-
"github.com/lestrrat-go/jwx/jwt"
11+
"github.com/lestrrat-go/jwx/v3/jwt"
1212
)
1313

1414
// JWSValidator is used to validate JWS payloads and return a JWT if they're
@@ -88,30 +88,27 @@ func Authenticate(v JWSValidator, ctx context.Context, input *openapi3filter.Aut
8888
// as a list under the "perms" claim, short for permissions, to keep the token
8989
// shorter.
9090
func GetClaimsFromToken(t jwt.Token) ([]string, error) {
91-
rawPerms, found := t.Get(PermissionsClaim)
92-
if !found {
91+
if !t.Has(PermissionsClaim) {
9392
// If the perms aren't found, it means that the token has none, but it has
9493
// passed signature validation by now, so it's a valid token, so we return
9594
// the empty list.
9695
return make([]string, 0), nil
9796
}
9897

99-
// rawPerms will be an untyped JSON list, so we need to convert it to
100-
// a string list.
101-
rawList, ok := rawPerms.([]interface{})
102-
if !ok {
103-
return nil, fmt.Errorf("'%s' claim is unexpected type'", PermissionsClaim)
98+
var rawList []interface{}
99+
if err := t.Get(PermissionsClaim, &rawList); err != nil {
100+
return nil, fmt.Errorf("getting %q claim: %w", PermissionsClaim, err)
104101
}
105102

106103
claims := make([]string, len(rawList))
107-
108104
for i, rawClaim := range rawList {
109-
var ok bool
110-
claims[i], ok = rawClaim.(string)
105+
claim, ok := rawClaim.(string)
111106
if !ok {
112107
return nil, fmt.Errorf("%s[%d] is not a string", PermissionsClaim, i)
113108
}
109+
claims[i] = claim
114110
}
111+
115112
return claims, nil
116113
}
117114

examples/go.mod

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/gorilla/mux v1.8.1
1414
github.com/kataras/iris/v12 v12.2.11
1515
github.com/labstack/echo/v4 v4.15.1
16-
github.com/lestrrat-go/jwx v1.2.31
16+
github.com/lestrrat-go/jwx/v3 v3.0.13
1717
github.com/oapi-codegen/echo-middleware v1.0.2
1818
github.com/oapi-codegen/fiber-middleware v1.0.2
1919
github.com/oapi-codegen/gin-middleware v1.0.2
@@ -67,11 +67,12 @@ require (
6767
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
6868
github.com/labstack/gommon v0.4.2 // indirect
6969
github.com/leodido/go-urn v1.4.0 // indirect
70-
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect
71-
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
70+
github.com/lestrrat-go/blackmagic v1.0.4 // indirect
71+
github.com/lestrrat-go/dsig v1.0.0 // indirect
72+
github.com/lestrrat-go/dsig-secp256k1 v1.0.0 // indirect
7273
github.com/lestrrat-go/httpcc v1.0.1 // indirect
73-
github.com/lestrrat-go/iter v1.0.2 // indirect
74-
github.com/lestrrat-go/option v1.0.1 // indirect
74+
github.com/lestrrat-go/httprc/v3 v3.0.2 // indirect
75+
github.com/lestrrat-go/option/v2 v2.0.0 // indirect
7576
github.com/mailgun/raymond/v2 v2.0.48 // indirect
7677
github.com/mailru/easyjson v0.9.1 // indirect
7778
github.com/mattn/go-colorable v0.1.14 // indirect
@@ -85,13 +86,13 @@ require (
8586
github.com/oasdiff/yaml3 v0.0.0-20260224194419-61cd415a242b // indirect
8687
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
8788
github.com/perimeterx/marshmallow v1.1.5 // indirect
88-
github.com/pkg/errors v0.9.1 // indirect
8989
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
9090
github.com/quic-go/qpack v0.5.1 // indirect
9191
github.com/quic-go/quic-go v0.54.0 // indirect
9292
github.com/rivo/uniseg v0.4.4 // indirect
9393
github.com/russross/blackfriday/v2 v2.1.0 // indirect
9494
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
95+
github.com/segmentio/asm v1.2.1 // indirect
9596
github.com/sirupsen/logrus v1.9.1 // indirect
9697
github.com/speakeasy-api/jsonpath v0.6.0 // indirect
9798
github.com/speakeasy-api/openapi-overlay v0.10.3 // indirect
@@ -101,6 +102,7 @@ require (
101102
github.com/ugorji/go/codec v1.3.0 // indirect
102103
github.com/valyala/bytebufferpool v1.0.0 // indirect
103104
github.com/valyala/fasthttp v1.51.0 // indirect
105+
github.com/valyala/fastjson v1.6.7 // indirect
104106
github.com/valyala/fasttemplate v1.2.2 // indirect
105107
github.com/valyala/tcplisten v1.0.0 // indirect
106108
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect

0 commit comments

Comments
 (0)