From 70f50115fec31e42d419b90c1de07fa77563cac4 Mon Sep 17 00:00:00 2001 From: hailong <605739610@qq.com> Date: Wed, 26 May 2021 19:24:06 +0800 Subject: [PATCH] push auth --- src/rpc/auth/Makefile | 26 +++++++++++++ src/rpc/auth/auth/rpcAuth.go | 61 ++++++++++++++++++++++++++++++ src/rpc/auth/auth/user_register.go | 20 ++++++++++ src/rpc/auth/auth/user_token.go | 29 ++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 src/rpc/auth/Makefile create mode 100644 src/rpc/auth/auth/rpcAuth.go create mode 100644 src/rpc/auth/auth/user_register.go create mode 100644 src/rpc/auth/auth/user_token.go diff --git a/src/rpc/auth/Makefile b/src/rpc/auth/Makefile new file mode 100644 index 000000000..12c10351d --- /dev/null +++ b/src/rpc/auth/Makefile @@ -0,0 +1,26 @@ +.PHONY: all build run gotool install clean help + +BINARY_NAME=open_im_auth +BIN_DIR=../../../bin/ +LAN_FILE=.go +GO_FILE:=${BINARY_NAME}${LAN_FILE} + +all: gotool build + +build: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${BINARY_NAME} ${GO_FILE} + +run: + @go run ./ + +gotool: + go fmt ./ + go vet ./ + +install: + make build + mv ${BINARY_NAME} ${BIN_DIR} + +clean: + @if [ -f ${BINARY_NAME} ] ; then rm ${BINARY_NAME} ; fi + diff --git a/src/rpc/auth/auth/rpcAuth.go b/src/rpc/auth/auth/rpcAuth.go new file mode 100644 index 000000000..b93d7b8fc --- /dev/null +++ b/src/rpc/auth/auth/rpcAuth.go @@ -0,0 +1,61 @@ +package rpcAuth + +import ( + "Open_IM/src/common/config" + log2 "Open_IM/src/common/log" + pbAuth "Open_IM/src/proto/auth" + "Open_IM/src/utils" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "google.golang.org/grpc" + "net" + "strconv" + "strings" +) + +type rpcAuth struct { + rpcPort int + rpcRegisterName string + etcdSchema string + etcdAddr []string +} + +func NewRpcAuthServer(port int) *rpcAuth { + return &rpcAuth{ + rpcPort: port, + rpcRegisterName: config.Config.RpcRegisterName.RpcGetTokenName, + etcdSchema: config.Config.Etcd.EtcdSchema, + etcdAddr: config.Config.Etcd.EtcdAddr, + } +} + +func (rpc *rpcAuth) Run() { + log2.Info("", "", "rpc get_token init...") + + address := utils.ServerIP + ":" + strconv.Itoa(rpc.rpcPort) + listener, err := net.Listen("tcp", address) + if err != nil { + log2.Error("", "", "listen network failed, err = %s, address = %s", err.Error(), address) + return + } + log2.Info("", "", "listen network success, address = %s", address) + + //grpc server + srv := grpc.NewServer() + defer srv.GracefulStop() + + //service registers with etcd + + pbAuth.RegisterAuthServer(srv, rpc) + err = getcdv3.RegisterEtcd(rpc.etcdSchema, strings.Join(rpc.etcdAddr, ","), utils.ServerIP, rpc.rpcPort, rpc.rpcRegisterName, 10) + if err != nil { + log2.Error("", "", "register rpc get_token to etcd failed, err = %s", err.Error()) + return + } + + err = srv.Serve(listener) + if err != nil { + log2.Info("", "", "rpc get_token fail, err = %s", err.Error()) + return + } + log2.Info("", "", "rpc get_token init success") +} diff --git a/src/rpc/auth/auth/user_register.go b/src/rpc/auth/auth/user_register.go new file mode 100644 index 000000000..aa2bc40f3 --- /dev/null +++ b/src/rpc/auth/auth/user_register.go @@ -0,0 +1,20 @@ +package rpcAuth + +import ( + "Open_IM/src/common/db/mysql_model/im_mysql_model" + "Open_IM/src/common/log" + pbAuth "Open_IM/src/proto/auth" + "context" +) + +func (rpc *rpcAuth) UserRegister(_ context.Context, pb *pbAuth.UserRegisterReq) (*pbAuth.UserRegisterResp, error) { + log.Info("", "", "rpc user_register start, [data: %s]", pb.String()) + + if err := im_mysql_model.UserRegister(pb); err != nil { + log.Error("", "", "rpc user_register error, [data: %s] [err: %s]", pb.String(), err.Error()) + return &pbAuth.UserRegisterResp{Success: false}, err + } + log.Info("", "", "rpc user_register success return") + + return &pbAuth.UserRegisterResp{Success: true}, nil +} diff --git a/src/rpc/auth/auth/user_token.go b/src/rpc/auth/auth/user_token.go new file mode 100644 index 000000000..59e91c33b --- /dev/null +++ b/src/rpc/auth/auth/user_token.go @@ -0,0 +1,29 @@ +package rpcAuth + +import ( + "Open_IM/src/common/db/mysql_model/im_mysql_model" + "Open_IM/src/common/log" + pbAuth "Open_IM/src/proto/auth" + "Open_IM/src/utils" + "context" +) + +func (rpc *rpcAuth) UserToken(_ context.Context, pb *pbAuth.UserTokenReq) (*pbAuth.UserTokenResp, error) { + log.Info("", "", "rpc user_token call start..., [pbTokenReq: %s]", pb.String()) + + _, err := im_mysql_model.FindUserByUID(pb.UID) + if err != nil { + log.Error("", "", "rpc user_token call..., im_mysql_model.AppServerFindFromUserByUserID fail [uid: %s] [err: %s]", pb.UID, err.Error()) + return &pbAuth.UserTokenResp{ErrCode: 500, ErrMsg: err.Error()}, err + } + log.Info("", "", "rpc user_token call..., im_mysql_model.AppServerFindFromUserByUserID") + + tokens, expTime, err := utils.CreateToken(pb.UID, "", pb.Platform) + if err != nil { + log.Error("", "", "rpc user_token call..., utils.CreateToken fail [uid: %s] [err: %s]", pb.UID, err.Error()) + return &pbAuth.UserTokenResp{ErrCode: 500, ErrMsg: err.Error()}, err + } + log.Info("", "", "rpc user_token success return, [uid: %s] [tokens: %s]", pb.UID, tokens) + + return &pbAuth.UserTokenResp{Token: tokens, ExpiredTime: expTime}, nil +}