mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-24 02:16:16 +08:00
parse token
This commit is contained in:
parent
becd3eb8f1
commit
b8ef0b3513
@ -4,6 +4,7 @@ import (
|
|||||||
"OpenIM/internal/api"
|
"OpenIM/internal/api"
|
||||||
"OpenIM/pkg/common/cmd"
|
"OpenIM/pkg/common/cmd"
|
||||||
"OpenIM/pkg/common/config"
|
"OpenIM/pkg/common/config"
|
||||||
|
"OpenIM/pkg/common/db/cache"
|
||||||
"OpenIM/pkg/common/log"
|
"OpenIM/pkg/common/log"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -27,12 +28,16 @@ func run(port int) error {
|
|||||||
if port == 0 {
|
if port == 0 {
|
||||||
port = config.Config.Api.GinPort[0]
|
port = config.Config.Api.GinPort[0]
|
||||||
}
|
}
|
||||||
|
cache, err := cache.NewRedis()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
zk, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, 10, config.Config.Zookeeper.UserName, config.Config.Zookeeper.Password)
|
zk, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, 10, config.Config.Zookeeper.UserName, config.Config.Zookeeper.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.NewPrivateLog(constant.LogFileName)
|
log.NewPrivateLog(constant.LogFileName)
|
||||||
router := api.NewGinRouter(zk)
|
router := api.NewGinRouter(zk, cache)
|
||||||
var address string
|
var address string
|
||||||
if config.Config.Api.ListenIP != "" {
|
if config.Config.Api.ListenIP != "" {
|
||||||
address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port))
|
address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port))
|
||||||
|
@ -16,13 +16,13 @@ func Call[A, B, C any](
|
|||||||
) {
|
) {
|
||||||
var req A
|
var req A
|
||||||
if err := c.BindJSON(&req); err != nil {
|
if err := c.BindJSON(&req); err != nil {
|
||||||
log.ZWarn(c, "gin bind json error", err, req)
|
log.ZWarn(c, "gin bind json error", err, "req", req)
|
||||||
apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error())) // 参数错误
|
apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error())) // 参数错误
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if check, ok := any(&req).(interface{ Check() error }); ok {
|
if check, ok := any(&req).(interface{ Check() error }); ok {
|
||||||
if err := check.Check(); err != nil {
|
if err := check.Check(); err != nil {
|
||||||
log.ZWarn(c, "custom check error", err, req)
|
log.ZWarn(c, "custom check error", err, "req", req)
|
||||||
apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error())) // 参数校验失败
|
apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error())) // 参数校验失败
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -7,20 +7,22 @@ import (
|
|||||||
"OpenIM/pkg/common/prome"
|
"OpenIM/pkg/common/prome"
|
||||||
"OpenIM/pkg/discoveryregistry"
|
"OpenIM/pkg/discoveryregistry"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry) *gin.Engine {
|
func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.UniversalClient) *gin.Engine {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
f, _ := os.Create("../logs/api.log")
|
f, _ := os.Create("../logs/api.log")
|
||||||
gin.DefaultWriter = io.MultiWriter(f)
|
gin.DefaultWriter = io.MultiWriter(f)
|
||||||
// gin.SetMode(gin.DebugMode)
|
// gin.SetMode(gin.DebugMode)
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
log.Info("load config: ", config.Config)
|
log.Info("load config: ", config.Config)
|
||||||
r.Use(gin.Recovery(), mw.CorsHandler(), mw.GinParseOperationID())
|
r.Use(gin.Recovery(), mw.CorsHandler(), mw.GinParseOperationID(), mw.GinParseToken(cache))
|
||||||
|
|
||||||
if config.Config.Prometheus.Enable {
|
if config.Config.Prometheus.Enable {
|
||||||
prome.NewApiRequestCounter()
|
prome.NewApiRequestCounter()
|
||||||
prome.NewApiRequestFailedCounter()
|
prome.NewApiRequestFailedCounter()
|
||||||
|
@ -273,6 +273,8 @@ const (
|
|||||||
const OperationID = "operationID"
|
const OperationID = "operationID"
|
||||||
const OpUserID = "opUserID"
|
const OpUserID = "opUserID"
|
||||||
const ConnID = "connID"
|
const ConnID = "connID"
|
||||||
|
const OpUserIDPlatformID = "platformID"
|
||||||
|
const Token = "token"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UnreliableNotification = 1
|
UnreliableNotification = 1
|
||||||
|
@ -1,10 +1,17 @@
|
|||||||
package mw
|
package mw
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"OpenIM/internal/apiresp"
|
||||||
|
"OpenIM/pkg/common/config"
|
||||||
"OpenIM/pkg/common/constant"
|
"OpenIM/pkg/common/constant"
|
||||||
|
"OpenIM/pkg/common/db/cache"
|
||||||
|
"OpenIM/pkg/common/db/controller"
|
||||||
|
"OpenIM/pkg/common/tokenverify"
|
||||||
|
"OpenIM/pkg/errs"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
@ -61,3 +68,51 @@ func GinParseOperationID() gin.HandlerFunc {
|
|||||||
c.Next()
|
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 == "" {
|
||||||
|
apiresp.GinError(c, errs.ErrArgs.Wrap())
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
claims, err := tokenverify.GetClaimFromToken(token)
|
||||||
|
if err != nil {
|
||||||
|
apiresp.GinError(c, errs.ErrTokenUnknown.Wrap())
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m, err := dataBase.GetTokensWithoutError(c, claims.UID, claims.Platform)
|
||||||
|
if err != nil {
|
||||||
|
apiresp.GinError(c, errs.ErrTokenNotExist.Wrap())
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(m) == 0 {
|
||||||
|
apiresp.GinError(c, errs.ErrTokenNotExist.Wrap())
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if v, ok := m[token]; ok {
|
||||||
|
switch v {
|
||||||
|
case constant.NormalToken:
|
||||||
|
case constant.KickedToken:
|
||||||
|
apiresp.GinError(c, errs.ErrTokenKicked.Wrap())
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user