Skip to content

Commit

Permalink
fix: jwt Parse problem
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jul 28, 2023
1 parent 133fe25 commit 170cdeb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
10 changes: 4 additions & 6 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"time"

"github.com/golang-jwt/jwt/v4"
"github.com/golang-jwt/jwt/v5"
"github.com/spf13/cast"
"gorm.io/gorm/clause"

Expand Down Expand Up @@ -40,10 +40,6 @@ type Auth struct {
}

func NewAuth(guard string, cache cache.Cache, config config.Config, orm orm.Orm) *Auth {
jwt.TimeFunc = func() time.Time {
return carbon.Now().ToStdTime()
}

return &Auth{
cache: cache,
config: config,
Expand Down Expand Up @@ -90,7 +86,9 @@ func (a *Auth) Parse(ctx http.Context, token string) (*contractsauth.Payload, er
jwtSecret := a.config.GetString("jwt.secret")
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (any, error) {
return []byte(jwtSecret), nil
})
}, jwt.WithTimeFunc(func() time.Time {
return carbon.Now().ToStdTime()
}))
if err != nil {
if errors.Is(err, jwt.ErrTokenExpired) && tokenClaims != nil {
claims, ok := tokenClaims.Claims.(*Claims)
Expand Down
16 changes: 15 additions & 1 deletion auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"time"

"github.com/golang-jwt/jwt/v4"
"github.com/golang-jwt/jwt/v5"
testifymock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"gorm.io/gorm/clause"
Expand Down Expand Up @@ -249,6 +249,20 @@ func (s *AuthTestSuite) TestParse_SuccessWithPrefix() {
s.mockConfig.AssertExpectations(s.T())
}

func (s *AuthTestSuite) TestParse_ExpiredAndInvalid() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Once()

ctx := http.Background()
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiIxIiwic3ViIjoidXNlciIsImV4cCI6MTY4OTk3MDE3MiwiaWF0IjoxNjg5OTY2NTcyfQ.GApXNbicqzjF2jHsSCJ1AdziHnI1grPuJ5ddSQjGJUQ"

s.mockCache.On("GetBool", "jwt:disabled:"+token, false).Return(false).Once()

_, err := s.auth.Parse(ctx, token)
s.ErrorIs(err, ErrorInvalidToken)

s.mockConfig.AssertExpectations(s.T())
}

func (s *AuthTestSuite) TestUser_NoParse() {
ctx := http.Background()
var user User
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/glebarez/sqlite v1.8.0
github.com/go-redis/redis/v8 v8.11.5
github.com/go-sql-driver/mysql v1.7.1
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/golang-migrate/migrate/v4 v4.16.2
github.com/golang-module/carbon/v2 v2.2.3
github.com/golang/protobuf v1.5.3
Expand Down Expand Up @@ -83,6 +83,7 @@ require (
github.com/go-stack/stack v1.8.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzw
github.com/golang-jwt/jwt/v4 v4.4.3/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang-migrate/migrate/v4 v4.16.2 h1:8coYbMKUyInrFk1lfGfRovTLAW7PhWp8qQDT2iKfuoA=
github.com/golang-migrate/migrate/v4 v4.16.2/go.mod h1:pfcJX4nPHaVdc5nmdCikFBWtm+UBpiZjRNNsyBbp0/o=
github.com/golang-module/carbon/v2 v2.2.3 h1:WvGIc5+qzq9drNzH+Gnjh1TZ0JgDY/IA+m2Dvk7Qm4Q=
Expand Down

0 comments on commit 170cdeb

Please sign in to comment.