From b3f28c3cb816b563cf7382a25ceb229a66ad326c Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 28 Jul 2023 15:39:57 +0800 Subject: [PATCH] fix --- .../websocket/internal/svc/servicecontext.go | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 server/websocket/internal/svc/servicecontext.go diff --git a/server/websocket/internal/svc/servicecontext.go b/server/websocket/internal/svc/servicecontext.go deleted file mode 100644 index cecf6811..00000000 --- a/server/websocket/internal/svc/servicecontext.go +++ /dev/null @@ -1,63 +0,0 @@ -package svc - -import ( - "errors" - "fmt" - "fusenapi/server/websocket/internal/config" - "net/http" - - "fusenapi/initalize" - "fusenapi/model/gmodel" - - "github.com/golang-jwt/jwt" - "gorm.io/gorm" -) - -type ServiceContext struct { - Config config.Config - - MysqlConn *gorm.DB - AllModels *gmodel.AllModelsGen - RabbitMq map[string]*initalize.QueueItem -} - -func NewServiceContext(c config.Config) *ServiceContext { - - return &ServiceContext{ - Config: c, - MysqlConn: initalize.InitMysql(c.SourceMysql), - AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), - RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), - } -} - -func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { - AuthKey := r.Header.Get("Authorization") - if AuthKey == "" { - return nil, nil - } - AuthKey = AuthKey[7:] - - if len(AuthKey) <= 50 { - return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) - } - - token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) { - // 检查签名方法是否为 HS256 - if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) - } - // 返回用于验证签名的密钥 - return []byte(svcCtx.Config.Auth.AccessSecret), nil - }) - if err != nil { - return nil, errors.New(fmt.Sprint("Error parsing token:", err)) - } - - // 验证成功返回 - if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { - return claims, nil - } - - return nil, errors.New(fmt.Sprint("Invalid token", err)) -}