Skip to content

Commit

Permalink
fix: use jwt.NumericDate instead time.Time for jwt parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Apr 1, 2024
1 parent 4f3eb64 commit 074b95d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
13 changes: 7 additions & 6 deletions common/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"encoding/base64"
"encoding/json"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
"log/slog"
"strings"
"time"
)

type UserClaims struct {
ID int `json:"id,omitempty"`
UserID int `json:"user_id,omitempty"`
UID int `json:"uid,omitempty"`
IsAdmin bool `json:"is_admin"`
ExpiresAt *time.Time `json:"exp,omitempty"`
ID int `json:"id,omitempty"`
UserID int `json:"user_id,omitempty"`
UID int `json:"uid,omitempty"`
IsAdmin bool `json:"is_admin"`
ExpiresAt *jwt.NumericDate `json:"exp,omitempty"`
}

type User struct {
Expand Down Expand Up @@ -55,7 +56,7 @@ func ParseJWTToken(token string, user any) error {
// jwt encoding uses url safe base64 encoding, so RawURLEncoding should be used instead of RawStdEncoding
payloadBytes, err := base64.RawURLEncoding.DecodeString(payloadString) // the middle one is payload
if err != nil {
slog.Error("jwt parse error", "err", err, "payload_string", payloadString)
slog.Error("jwt parse error", "err", err, "payload_string", payloadString, "payload_bytes", string(payloadBytes))
return ErrInvalidJWTToken
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ github.com/gofiber/fiber/v2 v2.52.4/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtg
github.com/gofiber/swagger v1.0.0 h1:BzUzDS9ZT6fDUa692kxmfOjc1DZiloLiPK/W5z1H1tc=
github.com/gofiber/swagger v1.0.0/go.mod h1:QrYNF1Yrc7ggGK6ATsJ6yfH/8Zi5bu9lA7wB8TmCecg=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down

0 comments on commit 074b95d

Please sign in to comment.