Skip to content

Commit 87917df

Browse files
Migrate github.com/SermoDigital to github.com/golang-jwt for 1.27.0 c… (#3852)
* Migrate github.com/SermoDigital to github.com/golang-jwt for 1.27.0 compat * Switched helper from Parse to ParseUnverified * Ensure to migrate remaining test cases as well * Adjustments after review; Pass accessToken directly instead of slicing it --------- Co-authored-by: Prem Kalle <38508878+prkalle@users.noreply.github.com>
1 parent 49cf122 commit 87917df

18 files changed

Lines changed: 127 additions & 117 deletions

File tree

‎actor/v7action/logging.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88

99
"code.cloudfoundry.org/cli/v9/actor/sharedaction"
10-
"github.com/SermoDigital/jose/jws"
10+
utiljwt "code.cloudfoundry.org/cli/v9/util/jwt"
1111
)
1212

1313
func (actor Actor) GetStreamingLogsForApplicationByNameAndSpace(appName string, spaceGUID string, client sharedaction.LogCacheClient) (<-chan sharedaction.LogMessage, <-chan error, context.CancelFunc, Warnings, error) {
@@ -92,17 +92,17 @@ func (actor Actor) refreshAccessTokenIfNecessary() (*time.Duration, error) {
9292
}
9393

9494
accessToken = strings.TrimPrefix(accessToken, "bearer ")
95-
token, err := jws.ParseJWT([]byte(accessToken))
95+
claims, err := utiljwt.ParseUnverified(accessToken)
9696
if err != nil {
9797
return nil, err
9898
}
9999

100100
var timeToRefresh time.Duration
101-
expiration, ok := token.Claims().Expiration()
102-
if !ok {
101+
expiration, err := claims.GetExpirationTime()
102+
if err != nil || expiration == nil {
103103
return nil, errors.New("Failed to get an expiry time from the current access token")
104104
}
105-
expiresIn := time.Until(expiration)
105+
expiresIn := time.Until(expiration.Time)
106106
if expiresIn >= 2*time.Minute {
107107
timeToRefresh = expiresIn - time.Minute
108108
} else {
@@ -115,14 +115,14 @@ func (actor Actor) tokenExpiryTime(accessToken string) (*time.Duration, error) {
115115
var expiresIn time.Duration
116116

117117
accessTokenString := strings.TrimPrefix(accessToken, "bearer ")
118-
token, err := jws.ParseJWT([]byte(accessTokenString))
118+
claims, err := utiljwt.ParseUnverified(accessTokenString)
119119
if err != nil {
120120
return nil, err
121121
}
122122

123-
expiration, ok := token.Claims().Expiration()
124-
if ok {
125-
expiresIn = time.Until(expiration)
123+
expiration, err := claims.GetExpirationTime()
124+
if err == nil && expiration != nil {
125+
expiresIn = time.Until(expiration.Time)
126126
}
127127
return &expiresIn, nil
128128
}

‎actor/v7action/token.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"strings"
55
"time"
66

7-
"github.com/SermoDigital/jose/jws"
8-
"github.com/SermoDigital/jose/jwt"
7+
utiljwt "code.cloudfoundry.org/cli/v9/util/jwt"
8+
jwtv5 "github.com/golang-jwt/jwt/v5"
99
)
1010

1111
func (actor Actor) RefreshAccessToken() (string, error) {
@@ -14,12 +14,12 @@ func (actor Actor) RefreshAccessToken() (string, error) {
1414
refreshToken := actor.Config.RefreshToken()
1515

1616
accessTokenString := strings.TrimPrefix(actor.Config.AccessToken(), "bearer ")
17-
token, err := jws.ParseJWT([]byte(accessTokenString))
17+
claims, err := utiljwt.ParseUnverified(accessTokenString)
1818

1919
if err == nil {
20-
expiration, ok := token.Claims().Expiration()
21-
if ok {
22-
expiresIn = time.Until(expiration)
20+
expiration, err := claims.GetExpirationTime()
21+
if err == nil && expiration != nil {
22+
expiresIn = time.Until(expiration.Time)
2323
}
2424
}
2525

@@ -37,7 +37,7 @@ func (actor Actor) RefreshAccessToken() (string, error) {
3737
return actor.Config.AccessToken(), nil
3838
}
3939

40-
func (actor Actor) ParseAccessToken(accessToken string) (jwt.JWT, error) {
40+
func (actor Actor) ParseAccessToken(accessToken string) (jwtv5.MapClaims, error) {
4141
tokenStr := strings.TrimPrefix(accessToken, "bearer ")
42-
return jws.ParseJWT([]byte(tokenStr))
42+
return utiljwt.ParseUnverified(tokenStr)
4343
}

‎api/cloudcontroller/wrapper/kubernetes_authentication_test.go‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import (
1818
"code.cloudfoundry.org/cli/v9/command/commandfakes"
1919
"code.cloudfoundry.org/cli/v9/integration/helpers"
2020

21-
"github.com/SermoDigital/jose/crypto"
22-
"github.com/SermoDigital/jose/jws"
21+
jwtv5 "github.com/golang-jwt/jwt/v5"
2322
. "github.com/onsi/ginkgo/v2"
2423
. "github.com/onsi/gomega"
2524
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -147,11 +146,13 @@ var _ = Describe("KubernetesAuthentication", func() {
147146
checkBearerTokenInAuthHeader := func() {
148147
actualReq := checkCalls()
149148

150-
token, err := jws.ParseJWTFromRequest(actualReq.Request)
149+
token, err := jwtv5.Parse(strings.TrimPrefix(actualReq.Header.Get("Authorization"), "Bearer "), func(_ *jwtv5.Token) (interface{}, error) {
150+
return keyPair.Public(), nil
151+
}, jwtv5.WithValidMethods([]string{jwtv5.SigningMethodRS256.Alg()}))
151152
Expect(err).NotTo(HaveOccurred())
152-
Expect(token.Validate(keyPair.Public(), crypto.SigningMethodRS256)).To(Succeed())
153+
Expect(token.Valid).To(BeTrue())
153154

154-
claims := token.Claims()
155+
claims := token.Claims.(jwtv5.MapClaims)
155156
Expect(claims).To(HaveKeyWithValue("another", "thing"))
156157
}
157158

@@ -179,13 +180,13 @@ var _ = Describe("KubernetesAuthentication", func() {
179180
var token []byte
180181

181182
BeforeEach(func() {
182-
jwt := jws.NewJWT(jws.Claims{
183+
claims := jwtv5.MapClaims{
183184
"exp": time.Now().Add(time.Hour).Unix(),
184185
"another": "thing",
185-
}, crypto.SigningMethodRS256)
186-
var err error
187-
token, err = jwt.Serialize(keyPair)
186+
}
187+
tokenString, err := jwtv5.NewWithClaims(jwtv5.SigningMethodRS256, claims).SignedString(keyPair)
188188
Expect(err).NotTo(HaveOccurred())
189+
token = []byte(tokenString)
189190

190191
kubeConfig.AuthInfos["auth-test"] = &api.AuthInfo{
191192
AuthProvider: &api.AuthProviderConfig{
@@ -368,13 +369,13 @@ var _ = Describe("KubernetesAuthentication", func() {
368369
var token []byte
369370

370371
BeforeEach(func() {
371-
jwt := jws.NewJWT(jws.Claims{
372+
claims := jwtv5.MapClaims{
372373
"exp": time.Now().Add(time.Hour).Unix(),
373374
"another": "thing",
374-
}, crypto.SigningMethodRS256)
375-
var err error
376-
token, err = jwt.Serialize(keyPair)
375+
}
376+
tokenString, err := jwtv5.NewWithClaims(jwtv5.SigningMethodRS256, claims).SignedString(keyPair)
377377
Expect(err).NotTo(HaveOccurred())
378+
token = []byte(tokenString)
378379
})
379380

380381
Context("inline tokens", func() {

‎api/cloudcontroller/wrapper/uaa_authentication.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"strings"
55
"time"
66

7-
"github.com/SermoDigital/jose/jws"
7+
utiljwt "code.cloudfoundry.org/cli/v9/util/jwt"
88

99
"code.cloudfoundry.org/cli/v9/api/cloudcontroller"
1010
"code.cloudfoundry.org/cli/v9/api/uaa"
@@ -81,12 +81,12 @@ func (t *UAAAuthentication) refreshTokenIfNecessary(accessToken string) error {
8181
var expiresIn time.Duration
8282

8383
tokenStr := strings.TrimPrefix(accessToken, "bearer ")
84-
token, err := jws.ParseJWT([]byte(tokenStr))
84+
claims, err := utiljwt.ParseUnverified(tokenStr)
8585

8686
if err == nil {
87-
expiration, ok := token.Claims().Expiration()
88-
if ok {
89-
expiresIn = time.Until(expiration)
87+
expiration, err := claims.GetExpirationTime()
88+
if err == nil && expiration != nil {
89+
expiresIn = time.Until(expiration.Time)
9090
}
9191
}
9292

‎api/cloudcontroller/wrapper/uaa_authentication_test.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import (
99

1010
"code.cloudfoundry.org/cli/v9/api/uaa"
1111

12-
"github.com/SermoDigital/jose/crypto"
13-
"github.com/SermoDigital/jose/jws"
12+
jwtv5 "github.com/golang-jwt/jwt/v5"
1413

1514
"code.cloudfoundry.org/cli/v9/api/cloudcontroller/ccerror"
1615

@@ -225,9 +224,8 @@ var _ = Describe("UAA Authentication", func() {
225224
})
226225

227226
func buildTokenString(expiration time.Time) (string, error) {
228-
c := jws.Claims{}
229-
c.SetExpiration(expiration)
230-
token := jws.NewJWT(c, crypto.Unsecured)
231-
tokenBytes, err := token.Serialize(nil)
232-
return string(tokenBytes), err
227+
claims := jwtv5.MapClaims{"exp": jwtv5.NewNumericDate(expiration)}
228+
token := jwtv5.NewWithClaims(jwtv5.SigningMethodNone, claims)
229+
tokenBytes, err := token.SignedString(jwtv5.UnsafeAllowNoneSignatureType)
230+
return tokenBytes, err
233231
}

‎api/shared/wrap_for_cf_on_k8s_test.go‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import (
1717
"code.cloudfoundry.org/cli/v9/command/commandfakes"
1818
"code.cloudfoundry.org/cli/v9/integration/helpers"
1919

20-
"github.com/SermoDigital/jose/crypto"
21-
"github.com/SermoDigital/jose/jws"
20+
jwtv5 "github.com/golang-jwt/jwt/v5"
2221
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2322
clientauthenticationv1beta1 "k8s.io/client-go/pkg/apis/clientauthentication/v1beta1"
2423
"k8s.io/client-go/tools/clientcmd/api"
@@ -148,11 +147,13 @@ var _ = Describe("WrapForCFOnK8sAuth", func() {
148147
checkBearerTokenInAuthHeader := func() {
149148
actualReq := checkCalls()
150149

151-
token, err := jws.ParseJWTFromRequest(actualReq)
150+
token, err := jwtv5.Parse(strings.TrimPrefix(actualReq.Header.Get("Authorization"), "Bearer "), func(_ *jwtv5.Token) (interface{}, error) {
151+
return keyPair.Public(), nil
152+
}, jwtv5.WithValidMethods([]string{jwtv5.SigningMethodRS256.Alg()}))
152153
Expect(err).NotTo(HaveOccurred())
153-
Expect(token.Validate(keyPair.Public(), crypto.SigningMethodRS256)).To(Succeed())
154+
Expect(token.Valid).To(BeTrue())
154155

155-
claims := token.Claims()
156+
claims := token.Claims.(jwtv5.MapClaims)
156157
Expect(claims).To(HaveKeyWithValue("another", "thing"))
157158
}
158159

@@ -180,13 +181,13 @@ var _ = Describe("WrapForCFOnK8sAuth", func() {
180181
var token []byte
181182

182183
BeforeEach(func() {
183-
jwt := jws.NewJWT(jws.Claims{
184+
claims := jwtv5.MapClaims{
184185
"exp": time.Now().Add(time.Hour).Unix(),
185186
"another": "thing",
186-
}, crypto.SigningMethodRS256)
187-
var err error
188-
token, err = jwt.Serialize(keyPair)
187+
}
188+
tokenString, err := jwtv5.NewWithClaims(jwtv5.SigningMethodRS256, claims).SignedString(keyPair)
189189
Expect(err).NotTo(HaveOccurred())
190+
token = []byte(tokenString)
190191

191192
kubeConfig.AuthInfos["auth-test"] = &api.AuthInfo{
192193
AuthProvider: &api.AuthProviderConfig{
@@ -371,13 +372,13 @@ var _ = Describe("WrapForCFOnK8sAuth", func() {
371372
var token []byte
372373

373374
BeforeEach(func() {
374-
jwt := jws.NewJWT(jws.Claims{
375+
claims := jwtv5.MapClaims{
375376
"exp": time.Now().Add(time.Hour).Unix(),
376377
"another": "thing",
377-
}, crypto.SigningMethodRS256)
378-
var err error
379-
token, err = jwt.Serialize(keyPair)
378+
}
379+
tokenString, err := jwtv5.NewWithClaims(jwtv5.SigningMethodRS256, claims).SignedString(keyPair)
380380
Expect(err).NotTo(HaveOccurred())
381+
token = []byte(tokenString)
381382
})
382383

383384
Context("inline tokens", func() {

‎cf/api/authentication/authentication.go‎

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

11-
"github.com/SermoDigital/jose/jws"
11+
utiljwt "code.cloudfoundry.org/cli/v9/util/jwt"
1212

1313
"code.cloudfoundry.org/cli/v9/cf/configuration/coreconfig"
1414
"code.cloudfoundry.org/cli/v9/cf/errors"
@@ -189,12 +189,12 @@ func (uaa UAARepository) RefreshAuthToken() (string, error) {
189189

190190
func (uaa UAARepository) RefreshToken(t string) (string, error) {
191191
tokenStr := strings.TrimPrefix(t, "bearer ")
192-
token, err := jws.ParseJWT([]byte(tokenStr))
192+
claims, err := utiljwt.ParseUnverified(tokenStr)
193193
if err != nil {
194194
return "", err
195195
}
196-
expiration, ok := token.Claims().Expiration()
197-
if ok && expiration.Sub(time.Now()) > accessTokenExpirationMargin {
196+
expiration, err := claims.GetExpirationTime()
197+
if err == nil && expiration != nil && expiration.Sub(time.Now()) > accessTokenExpirationMargin {
198198
return t, nil
199199
}
200200

‎cf/util/testhelpers/configuration/access_token.go‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
"fmt"
77
"time"
88

9-
"github.com/SermoDigital/jose/crypto"
10-
"github.com/SermoDigital/jose/jws"
9+
jwtv5 "github.com/golang-jwt/jwt/v5"
1110

1211
"code.cloudfoundry.org/cli/v9/cf/configuration/coreconfig"
1312
)
@@ -24,9 +23,8 @@ func EncodeAccessToken(tokenInfo coreconfig.TokenInfo) (accessToken string, err
2423

2524
// BuildTokenString builds a minimal JWT with the given time as expiration claim.
2625
func BuildTokenString(expiration time.Time) string {
27-
c := jws.Claims{}
28-
c.SetExpiration(expiration)
29-
token := jws.NewJWT(c, crypto.Unsecured)
30-
tokenBytes, _ := token.Serialize(nil)
26+
claims := jwtv5.MapClaims{"exp": jwtv5.NewNumericDate(expiration)}
27+
token := jwtv5.NewWithClaims(jwtv5.SigningMethodNone, claims)
28+
tokenBytes, _ := token.SignedString(jwtv5.UnsafeAllowNoneSignatureType)
3129
return string(tokenBytes)
3230
}

‎command/v7/actor.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"code.cloudfoundry.org/cli/v9/resources"
1616
"code.cloudfoundry.org/cli/v9/types"
1717
"code.cloudfoundry.org/cli/v9/util/configv3"
18-
"github.com/SermoDigital/jose/jwt"
18+
jwtv5 "github.com/golang-jwt/jwt/v5"
1919
)
2020

2121
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Actor
@@ -197,7 +197,7 @@ type Actor interface {
197197
MapRoute(routeGUID string, appGUID string, destinationProtocol string, destinationPort int) (v7action.Warnings, error)
198198
Marketplace(filter v7action.MarketplaceFilter) ([]v7action.ServiceOfferingWithPlans, v7action.Warnings, error)
199199
MoveRoute(routeGUID string, spaceGUID string) (v7action.Warnings, error)
200-
ParseAccessToken(accessToken string) (jwt.JWT, error)
200+
ParseAccessToken(accessToken string) (jwtv5.MapClaims, error)
201201
PollBuild(buildGUID string, appName string) (resources.Droplet, v7action.Warnings, error)
202202
PollPackage(pkg resources.Package) (resources.Package, v7action.Warnings, error)
203203
PollStart(app resources.Application, noWait bool, handleProcessStats func(string)) (v7action.Warnings, error)

‎command/v7/oauth_token_command.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func (cmd OauthTokenCommand) Execute(_ []string) error {
2626
return errors.New(cmd.UI.TranslateText("Access token is invalid."))
2727
}
2828

29-
expiration, success := token.Claims().Expiration()
30-
if !success {
29+
expiration, err := token.GetExpirationTime()
30+
if err != nil || expiration == nil {
3131
return errors.New(cmd.UI.TranslateText("Access token is missing expiration claim."))
3232
}
3333

0 commit comments

Comments
 (0)