mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-04 00:55:59 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
# Conflicts: # cmd/api/main.go # internal/api/a2r/api2rpc.go # internal/apiresp/resp.go # internal/msgtransfer/online_history_msg_handler.go # internal/push/consumer_init.go # pkg/common/db/controller/conversation.go # pkg/common/mw/gin.go # pkg/statistics/statistics.go
This commit is contained in:
+94
-3
@@ -7,8 +7,43 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type GinMwOptions func( *gin.RouterGroup )
|
||||
|
||||
func WithRecovery() GinMwOptions {
|
||||
return func(group *gin.RouterGroup) {
|
||||
group.Use(gin.Recovery())
|
||||
}
|
||||
}
|
||||
|
||||
func WithCorsHandler() GinMwOptions {
|
||||
return func(group *gin.RouterGroup) {
|
||||
group.Use(CorsHandler())
|
||||
}
|
||||
}
|
||||
|
||||
func WithGinParseOperationID() GinMwOptions {
|
||||
return func(group *gin.RouterGroup) {
|
||||
group.Use(GinParseOperationID())
|
||||
}
|
||||
}
|
||||
|
||||
func WithGinParseToken(rdb redis.UniversalClient) GinMwOptions {
|
||||
return func(group *gin.RouterGroup) {
|
||||
group.Use(GinParseToken(rdb))
|
||||
}
|
||||
}
|
||||
|
||||
func NewRouterGroup(routerGroup *gin.RouterGroup, route string, options ...GinMwOptions) *gin.RouterGroup {
|
||||
routerGroup = routerGroup.Group(route)
|
||||
for _, option := range options {
|
||||
option(routerGroup)
|
||||
}
|
||||
return routerGroup
|
||||
}
|
||||
|
||||
func CorsHandler() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
@@ -33,7 +68,8 @@ func GinParseOperationID() gin.HandlerFunc {
|
||||
if operationID == "" {
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
c.String(400, "read request body error: "+err.Error())
|
||||
log.ZWarn(c, "read request body error", errs.ErrArgs.Wrap("read request body error: "+err.Error()))
|
||||
apiresp.GinError(c, errs.ErrArgs.Wrap("read request body error: "+err.Error()))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -41,12 +77,14 @@ func GinParseOperationID() gin.HandlerFunc {
|
||||
OperationID string `json:"operationID"`
|
||||
}{}
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
c.String(400, "get operationID error: "+err.Error())
|
||||
log.ZWarn(c, "json unmarshal error", errs.ErrArgs.Wrap(err.Error()))
|
||||
apiresp.GinError(c, errs.ErrArgs.Wrap("json unmarshal error"+err.Error()))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if req.OperationID == "" {
|
||||
c.String(400, "operationID empty")
|
||||
log.ZWarn(c, "header must have operationID", errs.ErrArgs.Wrap(err.Error()))
|
||||
apiresp.GinError(c, errs.ErrArgs.Wrap("header must have operationID"+err.Error()))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -61,3 +99,56 @@ func GinParseOperationID() gin.HandlerFunc {
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
func GinParseToken(rdb redis.UniversalClient) gin.HandlerFunc {
|
||||
dataBase := controller.NewAuthDatabase(cache.NewCacheModel(rdb), config.Config.TokenPolicy.AccessSecret, config.Config.TokenPolicy.AccessExpire)
|
||||
return func(c *gin.Context) {
|
||||
switch c.Request.Method {
|
||||
case http.MethodPost:
|
||||
token := c.Request.Header.Get(constant.Token)
|
||||
if token == "" {
|
||||
log.ZWarn(c, "header get token error", errs.ErrArgs.Wrap("header must have token"))
|
||||
apiresp.GinError(c, errs.ErrArgs.Wrap("header must have token"))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
claims, err := tokenverify.GetClaimFromToken(token)
|
||||
if err != nil {
|
||||
log.ZWarn(c, "jwt get token error", errs.ErrTokenUnknown.Wrap())
|
||||
apiresp.GinError(c, errs.ErrTokenUnknown.Wrap())
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
m, err := dataBase.GetTokensWithoutError(c, claims.UID, claims.Platform)
|
||||
if err != nil {
|
||||
log.ZWarn(c, "cache get token error", errs.ErrTokenNotExist.Wrap())
|
||||
apiresp.GinError(c, errs.ErrTokenNotExist.Wrap())
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if len(m) == 0 {
|
||||
log.ZWarn(c, "cache do not exist token error", errs.ErrTokenNotExist.Wrap())
|
||||
apiresp.GinError(c, errs.ErrTokenNotExist.Wrap())
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if v, ok := m[token]; ok {
|
||||
switch v {
|
||||
case constant.NormalToken:
|
||||
case constant.KickedToken:
|
||||
log.ZWarn(c, "cache kicked token error", errs.ErrTokenKicked.Wrap())
|
||||
apiresp.GinError(c, errs.ErrTokenKicked.Wrap())
|
||||
c.Abort()
|
||||
return
|
||||
default:
|
||||
log.ZWarn(c, "cache unknown token error", errs.ErrTokenUnknown.Wrap())
|
||||
apiresp.GinError(c, errs.ErrTokenUnknown.Wrap())
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
c.Set(constant.OpUserIDPlatformID, constant.PlatformNameToID(claims.Platform))
|
||||
c.Set(constant.OpUserID, claims.UID)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user