Browse Source

[add] 维护JWT...

bvbej 2 years ago
parent
commit
552b485ec6
5 changed files with 16 additions and 10 deletions
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 0 1
      pkg/ddm/benchmark.go
  4. 5 3
      pkg/token/token.go
  5. 8 6
      pkg/token/token_jwt.go

+ 1 - 0
go.mod

@@ -8,6 +8,7 @@ require (
 	github.com/gin-gonic/gin v1.8.1
 	github.com/go-playground/validator/v10 v10.11.0
 	github.com/go-redis/redis/v8 v8.11.5
+	github.com/golang-jwt/jwt/v4 v4.4.2
 	github.com/google/uuid v1.3.0
 	github.com/gorilla/websocket v1.5.0
 	github.com/hashicorp/consul/api v1.13.1

+ 2 - 0
go.sum

@@ -143,6 +143,8 @@ github.com/goccy/go-json v0.9.10 h1:hCeNmprSNLB8B8vQKWl6DpuH0t60oEs+TAk9a7CScKc=
 github.com/goccy/go-json v0.9.10/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
 github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
 github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
+github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs=
+github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
 github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=

+ 0 - 1
pkg/ddm/benchmark.go

@@ -2,7 +2,6 @@ package ddm
 
 import (
 	"github.com/mritd/chinaid"
-	_ "github.com/mritd/chinaid"
 )
 
 type BType uint8

+ 5 - 3
pkg/token/token.go

@@ -4,7 +4,7 @@ import (
 	"net/url"
 	"time"
 
-	"github.com/dgrijalva/jwt-go"
+	"github.com/golang-jwt/jwt/v4"
 )
 
 var _ Token = (*token)(nil)
@@ -23,17 +23,19 @@ type Token interface {
 
 type token struct {
 	secret string
+	domain []string
 }
 
 type claims struct {
 	UserID   uint64 `json:"uid"`
 	Platform string `json:"pfm"`
-	jwt.StandardClaims
+	jwt.RegisteredClaims
 }
 
-func New(secret string) Token {
+func New(secret string, domain []string) Token {
 	return &token{
 		secret: secret,
+		domain: domain,
 	}
 }
 

+ 8 - 6
pkg/token/token_jwt.go

@@ -1,9 +1,8 @@
 package token
 
 import (
+	"github.com/golang-jwt/jwt/v4"
 	"time"
-
-	"github.com/dgrijalva/jwt-go"
 )
 
 func (t *token) JwtSign(userId uint64, platform string, expireDuration time.Duration) (tokenString string, err error) {
@@ -18,10 +17,13 @@ func (t *token) JwtSign(userId uint64, platform string, expireDuration time.Dura
 	c := claims{
 		userId,
 		platform,
-		jwt.StandardClaims{
-			NotBefore: time.Now().Unix(),
-			IssuedAt:  time.Now().Unix(),
-			ExpiresAt: time.Now().Add(expireDuration).Unix(),
+		jwt.RegisteredClaims{
+			Issuer:    "BvBeJ",
+			Subject:   "JSON Web Token",
+			Audience:  jwt.ClaimStrings(t.domain),
+			ExpiresAt: jwt.NewNumericDate(time.Now().Add(expireDuration)),
+			NotBefore: jwt.NewNumericDate(time.Now()),
+			IssuedAt:  jwt.NewNumericDate(time.Now()),
 		},
 	}
 	tokenString, err = jwt.NewWithClaims(jwt.SigningMethodHS256, c).SignedString([]byte(t.secret))