You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/rpc/auth/auth.go

141 lines
4.4 KiB

3 years ago
package auth
import (
"Open_IM/internal/common/check"
"Open_IM/internal/common/rpc_server"
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/controller"
3 years ago
"Open_IM/pkg/common/log"
2 years ago
promePkg "Open_IM/pkg/common/prometheus"
2 years ago
"Open_IM/pkg/common/tokenverify"
"Open_IM/pkg/common/tracelog"
3 years ago
pbAuth "Open_IM/pkg/proto/auth"
pbRelay "Open_IM/pkg/proto/relay"
3 years ago
"Open_IM/pkg/utils"
"context"
2 years ago
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
3 years ago
"google.golang.org/grpc"
)
func NewRpcAuthServer(port int) *rpcAuth {
r, err := rpc_server.NewRpcServer(config.Config.RpcRegisterIP, port, config.Config.RpcRegisterName.OpenImAuthName, config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema)
if err != nil {
panic(err)
}
return &rpcAuth{
RpcServer: r,
}
}
func (s *rpcAuth) Run() {
operationID := utils.OperationIDGenerator()
log.NewInfo(operationID, "rpc auth start...")
listener, address, err := rpc_server.GetTcpListen(config.Config.ListenIP, s.Port)
if err != nil {
panic(err)
}
log.NewInfo(operationID, "listen network success ", listener, address)
var grpcOpts []grpc.ServerOption
if config.Config.Prometheus.Enable {
promePkg.NewGrpcRequestCounter()
promePkg.NewGrpcRequestFailedCounter()
promePkg.NewGrpcRequestSuccessCounter()
promePkg.NewUserRegisterCounter()
promePkg.NewUserLoginCounter()
grpcOpts = append(grpcOpts, []grpc.ServerOption{
// grpc.UnaryInterceptor(promePkg.UnaryServerInterceptorProme),
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
}...)
}
srv := grpc.NewServer(grpcOpts...)
defer srv.GracefulStop()
pbAuth.RegisterAuthServer(srv, s)
err = srv.Serve(listener)
if err != nil {
panic(err)
}
log.NewInfo(operationID, "rpc auth ok")
}
func (s *rpcAuth) UserToken(ctx context.Context, req *pbAuth.UserTokenReq) (*pbAuth.UserTokenResp, error) {
resp := pbAuth.UserTokenResp{}
if _, err := check.GetUsersInfo(ctx, req.UserID); err != nil {
return nil, err
3 years ago
}
token, err := s.CreateToken(ctx, req.UserID, int(req.PlatformID), config.Config.TokenPolicy.AccessExpire)
3 years ago
if err != nil {
return nil, err
3 years ago
}
resp.Token = token
resp.ExpireTimeSeconds = config.Config.TokenPolicy.AccessExpire
return &resp, nil
3 years ago
}
2 years ago
func (s *rpcAuth) parseToken(ctx context.Context, tokensString, operationID string) (claims *tokenverify.Claims, err error) {
claims, err = tokenverify.GetClaimFromToken(tokensString)
if err != nil {
return nil, utils.Wrap(err, "")
}
m, err := s.GetTokens(ctx, claims.UID, claims.Platform)
3 years ago
if err != nil {
return nil, err
3 years ago
}
if v, ok := m[tokensString]; ok {
switch v {
case constant.NormalToken:
return claims, nil
case constant.KickedToken:
return nil, utils.Wrap(constant.ErrTokenKicked, "this token has been kicked by other same terminal ")
default:
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
}
}
return nil, utils.Wrap(constant.ErrTokenNotExist, "redis token map not find")
3 years ago
}
func (s *rpcAuth) ParseToken(ctx context.Context, req *pbAuth.ParseTokenReq) (*pbAuth.ParseTokenResp, error) {
resp := pbAuth.ParseTokenResp{}
claims, err := s.parseToken(ctx, req.Token, req.OperationID)
if err != nil {
return nil, err
}
resp.UserID = claims.UID
resp.Platform = claims.Platform
resp.ExpireTimeSeconds = claims.ExpiresAt.Unix()
return &resp, nil
}
func (s *rpcAuth) ForceLogout(ctx context.Context, req *pbAuth.ForceLogoutReq) (*pbAuth.ForceLogoutResp, error) {
resp := pbAuth.ForceLogoutResp{}
2 years ago
if err := tokenverify.CheckAdmin(ctx); err != nil {
return nil, err
}
if err := s.forceKickOff(ctx, req.UserID, req.PlatformID, tracelog.GetOperationID(ctx)); err != nil {
return nil, err
}
return &resp, nil
}
func (s *rpcAuth) forceKickOff(ctx context.Context, userID string, platformID int32, operationID string) error {
grpcCons, err := s.RegisterCenter.GetConns(config.Config.RpcRegisterName.OpenImRelayName)
if err != nil {
return err
}
2 years ago
for _, v := range grpcCons {
2 years ago
client := pbRelay.NewRelayClient(v)
2 years ago
kickReq := &pbRelay.KickUserOfflineReq{OperationID: operationID, KickUserIDList: []string{userID}, PlatformID: platformID}
2 years ago
log.NewInfo(operationID, "KickUserOffline ", client, kickReq.String())
_, err := client.KickUserOffline(ctx, kickReq)
2 years ago
return utils.Wrap(err, "")
}
return constant.ErrInternalServer.Wrap()
2 years ago
}
3 years ago
type rpcAuth struct {
*rpc_server.RpcServer
controller.AuthInterface
3 years ago
}