🔥 remove token encrypt code

This commit is contained in:
landaiqing
2024-11-03 17:13:08 +08:00
parent a0433b1dac
commit 89a9b21bff
7 changed files with 59 additions and 59 deletions

View File

@@ -3,6 +3,7 @@ package middleware
import ( import (
ginI18n "github.com/gin-contrib/i18n" ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"schisandra-cloud-album/common/result" "schisandra-cloud-album/common/result"
"schisandra-cloud-album/global" "schisandra-cloud-album/global"
) )

View File

@@ -2,14 +2,16 @@ package middleware
import ( import (
"encoding/json" "encoding/json"
"strings"
ginI18n "github.com/gin-contrib/i18n" ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"schisandra-cloud-album/common/constant" "schisandra-cloud-album/common/constant"
"schisandra-cloud-album/common/redis" "schisandra-cloud-album/common/redis"
"schisandra-cloud-album/common/result" "schisandra-cloud-album/common/result"
"schisandra-cloud-album/global" "schisandra-cloud-album/global"
"schisandra-cloud-album/utils" "schisandra-cloud-album/utils"
"strings"
) )
type TokenData struct { type TokenData struct {
@@ -21,7 +23,7 @@ type TokenData struct {
func JWTAuthMiddleware() gin.HandlerFunc { func JWTAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
// 默认Token放在请求头Authorization的Bearer中并以空格隔开 // 默认Token放在请求头Authorization的Bearer中并以空格隔开
authHeader := c.GetHeader(global.CONFIG.JWT.HeaderKey) authHeader := c.GetHeader(global.CONFIG.JWT.HeaderKey)
if authHeader == "" { if authHeader == "" {
result.FailWithCodeAndMessage(403, ginI18n.MustGetMessage(c, "AuthVerifyExpired"), c) result.FailWithCodeAndMessage(403, ginI18n.MustGetMessage(c, "AuthVerifyExpired"), c)
@@ -43,17 +45,6 @@ func JWTAuthMiddleware() gin.HandlerFunc {
c.Abort() c.Abort()
return return
} }
uid := c.GetHeader("X-UID")
if uid == "" {
result.FailWithCodeAndMessage(403, ginI18n.MustGetMessage(c, "AuthVerifyExpired"), c)
c.Abort()
return
}
if *parseToken.UserID != uid {
result.FailWithCodeAndMessage(403, ginI18n.MustGetMessage(c, "AuthVerifyExpired"), c)
c.Abort()
return
}
token := redis.Get(constant.UserLoginTokenRedisKey + *parseToken.UserID).Val() token := redis.Get(constant.UserLoginTokenRedisKey + *parseToken.UserID).Val()
if token == "" { if token == "" {
result.FailWithCodeAndMessage(403, ginI18n.MustGetMessage(c, "AuthVerifyExpired"), c) result.FailWithCodeAndMessage(403, ginI18n.MustGetMessage(c, "AuthVerifyExpired"), c)

View File

@@ -4,16 +4,18 @@ import (
"bytes" "bytes"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin"
"io" "io"
"net/http" "net/http"
"strconv"
"time"
ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin"
"schisandra-cloud-album/common/constant" "schisandra-cloud-album/common/constant"
"schisandra-cloud-album/common/redis" "schisandra-cloud-album/common/redis"
"schisandra-cloud-album/common/result" "schisandra-cloud-album/common/result"
"schisandra-cloud-album/global" "schisandra-cloud-album/global"
"strconv"
"time"
) )
func VerifySignature() gin.HandlerFunc { func VerifySignature() gin.HandlerFunc {

View File

@@ -1,12 +1,14 @@
package router package router
import ( import (
"time"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"schisandra-cloud-album/global" "schisandra-cloud-album/global"
"schisandra-cloud-album/middleware" "schisandra-cloud-album/middleware"
"schisandra-cloud-album/router/modules" "schisandra-cloud-album/router/modules"
"time"
) )
func InitRouter() *gin.Engine { func InitRouter() *gin.Engine {
@@ -22,7 +24,7 @@ func InitRouter() *gin.Engine {
router.Use(cors.New(cors.Config{ router.Use(cors.New(cors.Config{
AllowOrigins: []string{global.CONFIG.System.WebURL()}, AllowOrigins: []string{global.CONFIG.System.WebURL()},
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"}, AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization", "Accept-Language", "X-Sign", "X-Timestamp", "X-Nonce", "X-UID"}, AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization", "Accept-Language", "X-Sign", "X-Timestamp", "X-Nonce"},
AllowCredentials: true, AllowCredentials: true,
MaxAge: 12 * time.Hour, MaxAge: 12 * time.Hour,
})) }))

View File

@@ -5,9 +5,14 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"strconv"
"sync"
"time"
"github.com/acmestack/gorm-plus/gplus" "github.com/acmestack/gorm-plus/gplus"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"schisandra-cloud-album/common/constant" "schisandra-cloud-album/common/constant"
"schisandra-cloud-album/common/enum" "schisandra-cloud-album/common/enum"
"schisandra-cloud-album/common/redis" "schisandra-cloud-album/common/redis"
@@ -16,9 +21,6 @@ import (
"schisandra-cloud-album/model" "schisandra-cloud-album/model"
"schisandra-cloud-album/mq" "schisandra-cloud-album/mq"
"schisandra-cloud-album/utils" "schisandra-cloud-album/utils"
"strconv"
"sync"
"time"
) )
var commentReplyDao = impl.CommentReplyDaoImpl{} var commentReplyDao = impl.CommentReplyDaoImpl{}
@@ -404,17 +406,17 @@ func (CommentReplyServiceImpl) GetCommentListService(uid string, topicId string,
} }
likeMap[commentId] = exists // `exists` 为 true 则表示已点赞false 则表示未点赞 likeMap[commentId] = exists // `exists` 为 true 则表示已点赞false 则表示未点赞
} }
//queryLike, l := gplus.NewQuery[model.ScaCommentLikes]() // queryLike, l := gplus.NewQuery[model.ScaCommentLikes]()
//queryLike.Eq(&l.TopicId, topicId).Eq(&l.UserId, uid).In(&l.CommentId, commentIds) // queryLike.Eq(&l.TopicId, topicId).Eq(&l.UserId, uid).In(&l.CommentId, commentIds)
//likes, likesDB := gplus.SelectList(queryLike) // likes, likesDB := gplus.SelectList(queryLike)
//if likesDB.Error != nil { // if likesDB.Error != nil {
// global.LOG.Errorln(likesDB.Error) // global.LOG.Errorln(likesDB.Error)
// return // return
//} // }
//for _, like := range likes { // for _, like := range likes {
// likeMap[like.CommentId] = true // likeMap[like.CommentId] = true
// _ = redis.SAdd(redisKey, like.CommentId) // _ = redis.SAdd(redisKey, like.CommentId)
//} // }
} }
}() }()

View File

@@ -1,11 +1,11 @@
package utils package utils
import ( import (
"fmt"
"github.com/golang-jwt/jwt/v5"
"github.com/wumansgy/goEncrypt/aes"
"schisandra-cloud-album/global"
"time" "time"
"github.com/golang-jwt/jwt/v5"
"schisandra-cloud-album/global"
) )
type RefreshJWTPayload struct { type RefreshJWTPayload struct {
@@ -39,15 +39,15 @@ func GenerateAccessToken(payload AccessJWTPayload) (string, error) {
}, },
} }
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signedString, err := token.SignedString(MySecret) accessToken, err := token.SignedString(MySecret)
if err != nil { if err != nil {
return "", err return "", err
} }
accessToken, err := aes.AesCtrEncryptHex([]byte(signedString), []byte(global.CONFIG.Encrypt.Key), []byte(global.CONFIG.Encrypt.IV)) // accessToken, err := aes.AesCtrEncryptHex([]byte(signedString), []byte(global.CONFIG.Encrypt.Key), []byte(global.CONFIG.Encrypt.IV))
if err != nil { // if err != nil {
fmt.Println(err) // fmt.Println(err)
return "", err // return "", err
} // }
return accessToken, nil return accessToken, nil
} }
@@ -69,23 +69,23 @@ func GenerateRefreshToken(payload RefreshJWTPayload, days time.Duration) (string
global.LOG.Error(err) global.LOG.Error(err)
return "", 0 return "", 0
} }
refreshTokenEncrypted, err := aes.AesCtrEncryptHex([]byte(refreshTokenString), []byte(global.CONFIG.Encrypt.Key), []byte(global.CONFIG.Encrypt.IV)) // refreshTokenEncrypted, err := aes.AesCtrEncryptHex([]byte(refreshTokenString), []byte(global.CONFIG.Encrypt.Key), []byte(global.CONFIG.Encrypt.IV))
if err != nil { // if err != nil {
fmt.Println(err) // fmt.Println(err)
return "", 0 // return "", 0
} // }
return refreshTokenEncrypted, refreshClaims.ExpiresAt.Time.Unix() return refreshTokenString, refreshClaims.ExpiresAt.Time.Unix()
} }
// ParseAccessToken parses a JWT token and returns the payload // ParseAccessToken parses a JWT token and returns the payload
func ParseAccessToken(tokenString string) (*AccessJWTPayload, bool, error) { func ParseAccessToken(tokenString string) (*AccessJWTPayload, bool, error) {
MySecret = []byte(global.CONFIG.JWT.Secret) MySecret = []byte(global.CONFIG.JWT.Secret)
plaintext, err := aes.AesCtrDecryptByHex(tokenString, []byte(global.CONFIG.Encrypt.Key), []byte(global.CONFIG.Encrypt.IV)) // plaintext, err := aes.AesCtrDecryptByHex(tokenString, []byte(global.CONFIG.Encrypt.Key), []byte(global.CONFIG.Encrypt.IV))
if err != nil { // if err != nil {
global.LOG.Error(err) // global.LOG.Error(err)
return nil, false, err // return nil, false, err
} // }
token, err := jwt.ParseWithClaims(string(plaintext), &AccessJWTClaims{}, func(token *jwt.Token) (interface{}, error) { token, err := jwt.ParseWithClaims(tokenString, &AccessJWTClaims{}, func(token *jwt.Token) (interface{}, error) {
return MySecret, nil return MySecret, nil
}) })
if err != nil { if err != nil {
@@ -100,12 +100,12 @@ func ParseAccessToken(tokenString string) (*AccessJWTPayload, bool, error) {
// ParseRefreshToken parses a JWT token and returns the payload // ParseRefreshToken parses a JWT token and returns the payload
func ParseRefreshToken(tokenString string) (*RefreshJWTPayload, bool, error) { func ParseRefreshToken(tokenString string) (*RefreshJWTPayload, bool, error) {
MySecret = []byte(global.CONFIG.JWT.Secret) MySecret = []byte(global.CONFIG.JWT.Secret)
plaintext, err := aes.AesCtrDecryptByHex(tokenString, []byte(global.CONFIG.Encrypt.Key), []byte(global.CONFIG.Encrypt.IV)) // plaintext, err := aes.AesCtrDecryptByHex(tokenString, []byte(global.CONFIG.Encrypt.Key), []byte(global.CONFIG.Encrypt.IV))
if err != nil { // if err != nil {
global.LOG.Error(err) // global.LOG.Error(err)
return nil, false, err // return nil, false, err
} // }
token, err := jwt.ParseWithClaims(string(plaintext), &RefreshJWTClaims{}, func(token *jwt.Token) (interface{}, error) { token, err := jwt.ParseWithClaims(tokenString, &RefreshJWTClaims{}, func(token *jwt.Token) (interface{}, error) {
return MySecret, nil return MySecret, nil
}) })
if err != nil { if err != nil {

View File

@@ -2,9 +2,11 @@ package utils
import ( import (
"encoding/json" "encoding/json"
"github.com/gin-gonic/gin"
"schisandra-cloud-album/global"
"time" "time"
"github.com/gin-gonic/gin"
"schisandra-cloud-album/global"
) )
// ResponseData 返回数据 // ResponseData 返回数据