2022-11-24 23:37:34 +08:00
|
|
|
// Package middleware
|
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
2023-02-23 17:53:04 +08:00
|
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
2022-11-24 23:37:34 +08:00
|
|
|
// @Author Ms <133814250@qq.com>
|
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
|
|
"hotgo/internal/consts"
|
|
|
|
"hotgo/internal/library/response"
|
2023-06-13 15:45:30 +08:00
|
|
|
"hotgo/utility/simple"
|
2022-11-24 23:37:34 +08:00
|
|
|
)
|
|
|
|
|
2023-05-12 16:20:22 +08:00
|
|
|
// WebSocketAuth websocket鉴权中间件
|
|
|
|
func (s *sMiddleware) WebSocketAuth(r *ghttp.Request) {
|
|
|
|
var (
|
2023-06-13 15:45:30 +08:00
|
|
|
ctx = r.Context()
|
|
|
|
path = gstr.Replace(r.URL.Path, simple.RouterPrefix(ctx, consts.AppWebSocket), "", 1)
|
2023-05-12 16:20:22 +08:00
|
|
|
)
|
2022-11-24 23:37:34 +08:00
|
|
|
|
2023-05-12 16:20:22 +08:00
|
|
|
// 不需要验证登录的路由地址
|
2023-06-13 15:45:30 +08:00
|
|
|
if s.IsExceptLogin(ctx, consts.AppWebSocket, path) {
|
2022-11-24 23:37:34 +08:00
|
|
|
r.Middleware.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-12 16:20:22 +08:00
|
|
|
// 将用户信息传递到上下文中
|
2023-06-13 15:45:30 +08:00
|
|
|
if err := s.DeliverUserContext(r); err != nil {
|
2022-11-24 23:37:34 +08:00
|
|
|
response.JsonExit(r, gcode.CodeNotAuthorized.Code(), err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Middleware.Next()
|
|
|
|
}
|