Skip to content

Commit 0c63ed9

Browse files
authored
feat: upgrade golang-jwt version for apple provider (#583)
1 parent e55b014 commit 0c63ed9

File tree

4 files changed

+11
-20
lines changed

4 files changed

+11
-20
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/go-chi/chi/v5 v5.1.0
7-
github.com/golang-jwt/jwt/v4 v4.2.0
7+
github.com/golang-jwt/jwt/v5 v5.2.1
88
github.com/gorilla/mux v1.6.2
99
github.com/gorilla/pat v0.0.0-20180118222023-199c85a7f6d1
1010
github.com/gorilla/sessions v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
1313
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
1414
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
1515
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
16-
github.com/golang-jwt/jwt/v4 v4.2.0 h1:besgBTC8w8HjP6NzQdxwKH9Z5oQMZ24ThTrHp3cZ8eU=
17-
github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
16+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
17+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
1818
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
1919
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
2020
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=

providers/apple/apple.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"strings"
1414
"time"
1515

16-
"github.com/golang-jwt/jwt/v4"
16+
"github.com/golang-jwt/jwt/v5"
1717
"github.com/markbates/goth"
1818
"golang.org/x/oauth2"
1919
)

providers/apple/session.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"fmt"
1111
"time"
1212

13-
"github.com/golang-jwt/jwt/v4"
13+
"github.com/golang-jwt/jwt/v5"
1414
"github.com/lestrrat-go/jwx/jwk"
1515
"github.com/markbates/goth"
1616
"golang.org/x/oauth2"
@@ -48,7 +48,7 @@ func (s Session) Marshal() string {
4848
}
4949

5050
type IDTokenClaims struct {
51-
jwt.StandardClaims
51+
jwt.RegisteredClaims
5252
AccessTokenHash string `json:"at_hash"`
5353
AuthTime int `json:"auth_time"`
5454
Email string `json:"email"`
@@ -80,27 +80,18 @@ func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string,
8080
idToken, err := jwt.ParseWithClaims(idToken.(string), &IDTokenClaims{}, func(t *jwt.Token) (interface{}, error) {
8181
kid := t.Header["kid"].(string)
8282
claims := t.Claims.(*IDTokenClaims)
83-
vErr := new(jwt.ValidationError)
84-
if !claims.VerifyAudience(p.clientId, true) {
85-
vErr.Inner = fmt.Errorf("audience is incorrect")
86-
vErr.Errors |= jwt.ValidationErrorAudience
87-
}
88-
if !claims.VerifyIssuer(AppleAudOrIss, true) {
89-
vErr.Inner = fmt.Errorf("issuer is incorrect")
90-
vErr.Errors |= jwt.ValidationErrorIssuer
91-
}
92-
if vErr.Errors > 0 {
93-
return nil, vErr
83+
validator := jwt.NewValidator(jwt.WithAudience(p.clientId), jwt.WithIssuer(AppleAudOrIss))
84+
err := validator.Validate(claims)
85+
if err != nil {
86+
return nil, err
9487
}
9588

9689
// per OpenID Connect Core 1.0 §3.2.2.9, Access Token Validation
9790
hash := sha256.Sum256([]byte(s.AccessToken))
9891
halfHash := hash[0:(len(hash) / 2)]
9992
encodedHalfHash := base64.RawURLEncoding.EncodeToString(halfHash)
10093
if encodedHalfHash != claims.AccessTokenHash {
101-
vErr.Inner = fmt.Errorf(`identity token invalid`)
102-
vErr.Errors |= jwt.ValidationErrorClaimsInvalid
103-
return nil, vErr
94+
return nil, fmt.Errorf(`identity token invalid`)
10495
}
10596

10697
// get the public key for verifying the identity token signature

0 commit comments

Comments
 (0)