From 36110aca3c1d85edb69d7d6e17ec7d80fd00690b Mon Sep 17 00:00:00 2001 From: hujiebin Date: Tue, 13 Dec 2022 20:28:50 +0800 Subject: [PATCH] =?UTF-8?q?20=E4=BA=BA=E4=B8=8A=E9=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ script/test_20_in_mic.go | 46 +++++++++++++++++++++++++++++++++ utils/jwt/jwt.go | 55 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 script/test_20_in_mic.go create mode 100644 utils/jwt/jwt.go diff --git a/go.mod b/go.mod index d2d7334..2e2c275 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/hilo-common go 1.17 require ( + github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect diff --git a/go.sum b/go.sum index ba12d8b..4a64fb9 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= diff --git a/script/test_20_in_mic.go b/script/test_20_in_mic.go new file mode 100644 index 0000000..f695ac6 --- /dev/null +++ b/script/test_20_in_mic.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "github.com/hilo-common/model" + "github.com/hilo-common/mysql" + "github.com/hilo-common/utils/jwt" + "net/http" + "strings" + "time" +) + +var micCodes = []string{"1000653", "1000516", "1000675", "1000890", "1000755", "1000593", "1000629", "1000634", "1000765", "1000839", "1000355", "1000591", "1000633", "1000224", "1000611", "1000689", "1000467", "1000384", "1000367", "1000623"} + +func main() { + var users []model.User + if err := mysql.TestDB.Model(model.User{}).Where("code in ?", micCodes).Find(&users).Error; err != nil { + panic(err) + } + for _, user := range users { + url := "https://test.apiv1.faceline.live/v1/imGroup/mic/in" + method := "POST" + + payload := strings.NewReader("groupUuid=HTGS%23a56194639&i=") + + client := &http.Client{} + req, err := http.NewRequest(method, url, payload) + + if err != nil { + fmt.Println(err) + return + } + token, _ := jwt.GenerateToken(user.Id, user.ExternalId, "hiloApi") + req.Header.Add("nonce", "hilo") + req.Header.Add("token", token) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + res, err := client.Do(req) + if err != nil { + fmt.Println(err) + return + } + res.Body.Close() + time.Sleep(time.Second * 2) + } +} diff --git a/utils/jwt/jwt.go b/utils/jwt/jwt.go new file mode 100644 index 0000000..0f22fd6 --- /dev/null +++ b/utils/jwt/jwt.go @@ -0,0 +1,55 @@ +package jwt + +import ( + "github.com/dgrijalva/jwt-go" + "time" +) + +// 载荷,增加用户别名 +type Claims struct { + UserId uint64 + ExternalId string + jwt.StandardClaims +} + +//生成token +func GenerateToken(userId uint64, externalId string, issuer string) (string, error) { + duration, err := time.ParseDuration("240h") + if err != nil { + return "", err + } + + expireTime := time.Now().Add(duration) + claims := Claims{ + UserId: userId, + ExternalId: externalId, + StandardClaims: jwt.StandardClaims{ + ExpiresAt: expireTime.Unix(), //过期时间 + Issuer: issuer, //签名的发行者 + }, + } + tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + token, err := tokenClaims.SignedString(GetJWTSecret()) + return token, err +} + +func GetJWTSecret() []byte { + return []byte("hilo1632") +} + +//解析token +func ParseToken(token string) (*Claims, error) { + tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) { + return GetJWTSecret(), nil + }) + if err != nil { + return nil, err + } + if tokenClaims != nil { + claims, ok := tokenClaims.Claims.(*Claims) + if ok && tokenClaims.Valid { + return claims, nil + } + } + return nil, err +} -- 2.22.0