Skip to content

Commit efcf9de

Browse files
Migrate github.com/SermoDigital to github.com/golang-jwt for 1.27.0 compat
1 parent fcb3492 commit efcf9de

16 files changed

Lines changed: 98 additions & 88 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.Parse(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.Parse(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.Parse(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.Parse(tokenStr)
4343
}

‎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.Parse(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
}

‎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.Parse(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

‎command/v7/oauth_token_command_test.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import (
88
. "code.cloudfoundry.org/cli/v9/command/v7"
99
"code.cloudfoundry.org/cli/v9/command/v7/v7fakes"
1010
"code.cloudfoundry.org/cli/v9/util/ui"
11-
"github.com/SermoDigital/jose/crypto"
12-
"github.com/SermoDigital/jose/jws"
11+
jwtv5 "github.com/golang-jwt/jwt/v5"
1312
. "github.com/onsi/ginkgo/v2"
1413
. "github.com/onsi/gomega"
1514
. "github.com/onsi/gomega/gbytes"
@@ -71,7 +70,7 @@ var _ = Describe("oauth-token command", func() {
7170

7271
When("the existing access token is invalid", func() {
7372
BeforeEach(func() {
74-
token := jws.NewJWT(jws.Claims{}, crypto.SigningMethodHS256)
73+
token := jwtv5.MapClaims{}
7574
fakeConfig.AccessTokenReturns("invalid-existing-access-token")
7675
fakeActor.ParseAccessTokenReturns(token, errors.New("Access token is invalid"))
7776
})
@@ -89,7 +88,7 @@ var _ = Describe("oauth-token command", func() {
8988

9089
When("the existing access token does not have an expiry time", func() {
9190
BeforeEach(func() {
92-
token := jws.NewJWT(jws.Claims{}, crypto.SigningMethodHS256)
91+
token := jwtv5.MapClaims{}
9392
fakeConfig.AccessTokenReturns("existing-access-token")
9493
fakeActor.ParseAccessTokenReturns(token, nil)
9594
})

‎command/v7/v7fakes/fake_actor.go‎

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)