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

190 lines
8.2 KiB

3 years ago
package auth
import (
"Open_IM/pkg/common/constant"
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
"Open_IM/pkg/common/log"
2 years ago
promePkg "Open_IM/pkg/common/prometheus"
3 years ago
"Open_IM/pkg/common/token_verify"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbAuth "Open_IM/pkg/proto/auth"
pbRelay "Open_IM/pkg/proto/relay"
3 years ago
"Open_IM/pkg/utils"
"context"
2 years ago
"errors"
3 years ago
"net"
"strconv"
"strings"
2 years ago
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
3 years ago
"Open_IM/pkg/common/config"
"google.golang.org/grpc"
)
func (rpc *rpcAuth) UserRegister(_ context.Context, req *pbAuth.UserRegisterReq) (*pbAuth.UserRegisterResp, error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
var user imdb.User
3 years ago
utils.CopyStructFields(&user, req.UserInfo)
if req.UserInfo.BirthStr != "" {
time, err := utils.TimeStringToTime(req.UserInfo.BirthStr)
if err != nil {
log.NewError(req.OperationID, "TimeStringToTime failed ", err.Error(), req.UserInfo.BirthStr)
return &pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrArgs.ErrCode, ErrMsg: "TimeStringToTime failed:" + err.Error()}}, nil
}
user.Birth = time
3 years ago
}
3 years ago
log.Debug(req.OperationID, "copy ", user, req.UserInfo)
3 years ago
err := imdb.UserRegister(user)
if err != nil {
3 years ago
errMsg := req.OperationID + " imdb.UserRegister failed " + err.Error() + user.UserID
log.NewError(req.OperationID, errMsg, user)
return &pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}}, nil
3 years ago
}
2 years ago
promePkg.PromeInc(promePkg.UserRegisterCounter)
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{}})
3 years ago
return &pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{}}, nil
}
func (rpc *rpcAuth) UserToken(_ context.Context, req *pbAuth.UserTokenReq) (*pbAuth.UserTokenResp, error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
_, err := imdb.GetUserByUserID(req.FromUserID)
if err != nil {
log.NewError(req.OperationID, "not this user:", req.FromUserID, req.String())
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: err.Error()}}, nil
}
tokens, expTime, err := token_verify.CreateToken(req.FromUserID, int(req.Platform))
3 years ago
if err != nil {
3 years ago
errMsg := req.OperationID + " token_verify.CreateToken failed " + err.Error() + req.FromUserID + utils.Int32ToString(req.Platform)
log.NewError(req.OperationID, errMsg)
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}}, nil
3 years ago
}
2 years ago
promePkg.PromeInc(promePkg.UserLoginCounter)
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{}, Token: tokens, ExpiredTime: expTime})
3 years ago
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{}, Token: tokens, ExpiredTime: expTime}, nil
3 years ago
}
func (rpc *rpcAuth) ParseToken(_ context.Context, req *pbAuth.ParseTokenReq) (*pbAuth.ParseTokenResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
claims, err := token_verify.ParseToken(req.Token, req.OperationID)
if err != nil {
errMsg := "ParseToken failed " + err.Error() + req.OperationID + " token " + req.Token
log.Error(req.OperationID, errMsg, "token:", req.Token)
return &pbAuth.ParseTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: 4001, ErrMsg: errMsg}}, nil
}
resp := pbAuth.ParseTokenResp{CommonResp: &pbAuth.CommonResp{}, UserID: claims.UID, Platform: claims.Platform, ExpireTimeSeconds: uint32(claims.ExpiresAt.Unix())}
log.Info(req.OperationID, utils.GetSelfFuncName(), " rpc return ", resp.String())
return &resp, nil
}
2 years ago
func (rpc *rpcAuth) ForceLogout(_ context.Context, req *pbAuth.ForceLogoutReq) (*pbAuth.ForceLogoutResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
if !token_verify.IsManagerUserID(req.OpUserID) {
errMsg := req.OperationID + " IsManagerUserID false " + req.OpUserID
log.NewError(req.OperationID, errMsg)
return &pbAuth.ForceLogoutResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}}, nil
}
//if err := token_verify.DeleteToken(req.FromUserID, int(req.Platform)); err != nil {
// errMsg := req.OperationID + " DeleteToken failed " + err.Error() + req.FromUserID + utils.Int32ToString(req.Platform)
// log.NewError(req.OperationID, errMsg)
// return &pbAuth.ForceLogoutResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}}, nil
//}
if err := rpc.forceKickOff(req.FromUserID, req.Platform, req.OperationID); err != nil {
errMsg := req.OperationID + " forceKickOff failed " + err.Error() + req.FromUserID + utils.Int32ToString(req.Platform)
log.NewError(req.OperationID, errMsg)
return &pbAuth.ForceLogoutResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}}, nil
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{}})
return &pbAuth.ForceLogoutResp{CommonResp: &pbAuth.CommonResp{}}, nil
}
func (rpc *rpcAuth) forceKickOff(userID string, platformID int32, operationID string) error {
log.NewInfo(operationID, utils.GetSelfFuncName(), " args ", userID, platformID)
2 years ago
grpcCons := getcdv3.GetDefaultGatewayConn4Unique(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), operationID)
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())
2 years ago
_, err := client.KickUserOffline(context.Background(), kickReq)
return utils.Wrap(err, "")
}
return errors.New("no rpc node ")
2 years ago
}
3 years ago
type rpcAuth struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
}
func NewRpcAuthServer(port int) *rpcAuth {
3 years ago
log.NewPrivateLog(constant.LogFileName)
3 years ago
return &rpcAuth{
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImAuthName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
}
func (rpc *rpcAuth) Run() {
3 years ago
operationID := utils.OperationIDGenerator()
log.NewInfo(operationID, "rpc auth start...")
3 years ago
3 years ago
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
address := listenIP + ":" + strconv.Itoa(rpc.rpcPort)
3 years ago
listener, err := net.Listen("tcp", address)
if err != nil {
3 years ago
panic("listening err:" + err.Error() + rpc.rpcRegisterName)
3 years ago
}
3 years ago
log.NewInfo(operationID, "listen network success, ", address, listener)
2 years ago
var grpcOpts []grpc.ServerOption
if config.Config.Prometheus.Enable {
2 years ago
promePkg.NewGrpcRequestCounter()
promePkg.NewGrpcRequestFailedCounter()
promePkg.NewGrpcRequestSuccessCounter()
2 years ago
promePkg.NewUserRegisterCounter()
promePkg.NewUserLoginCounter()
2 years ago
grpcOpts = append(grpcOpts, []grpc.ServerOption{
2 years ago
// grpc.UnaryInterceptor(promePkg.UnaryServerInterceptorProme),
2 years ago
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
}...)
2 years ago
}
srv := grpc.NewServer(grpcOpts...)
3 years ago
defer srv.GracefulStop()
//service registers with etcd
pbAuth.RegisterAuthServer(srv, rpc)
2 years ago
rpcRegisterIP := config.Config.RpcRegisterIP
3 years ago
if config.Config.RpcRegisterIP == "" {
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
3 years ago
log.NewInfo("", "rpcRegisterIP", rpcRegisterIP)
2 years ago
3 years ago
err = getcdv3.RegisterEtcd(rpc.etcdSchema, strings.Join(rpc.etcdAddr, ","), rpcRegisterIP, rpc.rpcPort, rpc.rpcRegisterName, 10)
3 years ago
if err != nil {
3 years ago
log.NewError(operationID, "RegisterEtcd failed ", err.Error(),
3 years ago
rpc.etcdSchema, strings.Join(rpc.etcdAddr, ","), rpcRegisterIP, rpc.rpcPort, rpc.rpcRegisterName)
panic(utils.Wrap(err, "register auth module rpc to etcd err"))
3 years ago
}
3 years ago
log.NewInfo(operationID, "RegisterAuthServer ok ", rpc.etcdSchema, strings.Join(rpc.etcdAddr, ","), rpcRegisterIP, rpc.rpcPort, rpc.rpcRegisterName)
3 years ago
err = srv.Serve(listener)
if err != nil {
3 years ago
log.NewError(operationID, "Serve failed ", err.Error())
3 years ago
return
}
3 years ago
log.NewInfo(operationID, "rpc auth ok")
3 years ago
}