mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
add
This commit is contained in:
parent
9f4c367249
commit
05b5a0ada3
@ -12,6 +12,7 @@ import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@ -31,6 +32,7 @@ func main() {
|
||||
authRouterGroup.POST("/password", register.SetPassword)
|
||||
authRouterGroup.POST("/login", register.Login)
|
||||
authRouterGroup.POST("/reset_password", register.ResetPassword)
|
||||
authRouterGroup.POST("/check_login", register.CheckLoginLimit)
|
||||
}
|
||||
demoRouterGroup := r.Group("/auth")
|
||||
{
|
||||
@ -39,7 +41,10 @@ func main() {
|
||||
demoRouterGroup.POST("/password", register.SetPassword)
|
||||
demoRouterGroup.POST("/login", register.Login)
|
||||
demoRouterGroup.POST("/reset_password", register.ResetPassword)
|
||||
demoRouterGroup.POST("/check_login", register.CheckLoginLimit)
|
||||
}
|
||||
|
||||
//deprecated
|
||||
cmsRouterGroup := r.Group("/cms_admin")
|
||||
{
|
||||
cmsRouterGroup.POST("/generate_invitation_code", register.GenerateInvitationCode)
|
||||
|
@ -3,7 +3,6 @@ package apiAuth
|
||||
import (
|
||||
api "Open_IM/pkg/base_info"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
||||
@ -67,13 +66,7 @@ func UserRegister(c *gin.Context) {
|
||||
if reply.CommonResp.ErrCode != 0 {
|
||||
errMsg := req.OperationID + " " + " UserRegister failed " + reply.CommonResp.ErrMsg + req.String()
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
if reply.CommonResp.ErrCode == constant.RegisterLimit {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterLimit, "errMsg": "用户注册被限制"})
|
||||
} else if reply.CommonResp.ErrCode == constant.InvitationError {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.InvitationError, "errMsg": "邀请码错误"})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
81
internal/demo/register/check_login.go
Normal file
81
internal/demo/register/check_login.go
Normal file
@ -0,0 +1,81 @@
|
||||
package register
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type CheckLoginLimitReq struct {
|
||||
OperationID string `json:"operationID"`
|
||||
UserID string `json:"userID"`
|
||||
}
|
||||
|
||||
type CheckLoginLimitResp struct {
|
||||
}
|
||||
|
||||
func CheckLoginLimit(c *gin.Context) {
|
||||
req := CheckLoginLimitReq{}
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), err.Error())
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.ErrArgs, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
ip := c.Request.Header.Get("X-Forward-For")
|
||||
if ip == "" {
|
||||
ip = c.ClientIP()
|
||||
}
|
||||
user, err := imdb.GetUserIPLimit(req.UserID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " imdb.GetUserByUserID failed " + err.Error() + req.UserID
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": errMsg})
|
||||
}
|
||||
|
||||
if err := imdb.UpdateIpReocord(req.UserID, ip); err != nil {
|
||||
log.NewError(req.OperationID, err.Error(), req.UserID, ip)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var Limited bool
|
||||
var LimitError error
|
||||
Limited, LimitError = imdb.IsLimitLoginIp(ip)
|
||||
if LimitError != nil {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), LimitError, ip)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": LimitError})
|
||||
return
|
||||
}
|
||||
if Limited {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), Limited, ip, req.UserID)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.LoginLimit, "errMsg": "ip limited Login"})
|
||||
return
|
||||
}
|
||||
Limited, LimitError = imdb.IsLimitUserLoginIp(user.UserID, ip)
|
||||
if LimitError != nil {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), LimitError, ip)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": LimitError})
|
||||
return
|
||||
}
|
||||
if Limited {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), Limited, ip, req.UserID)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.LoginLimit, "errMsg": "user ip limited Login"})
|
||||
return
|
||||
}
|
||||
Limited, LimitError = imdb.UserIsBlock(user.UserID)
|
||||
if LimitError != nil {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), LimitError, user.UserID)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": LimitError})
|
||||
return
|
||||
}
|
||||
if Limited {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), Limited, ip, req.UserID)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.LoginLimit, "errMsg": "user is block"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
|
||||
}
|
@ -8,6 +8,7 @@ import (
|
||||
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||
http2 "Open_IM/pkg/common/http"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
pbFriend "Open_IM/pkg/proto/friend"
|
||||
"Open_IM/pkg/utils"
|
||||
"encoding/json"
|
||||
@ -41,12 +42,14 @@ func SetPassword(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ip := c.Request.Header.Get("X-Forward-For")
|
||||
if ip == "" {
|
||||
ip = c.ClientIP()
|
||||
}
|
||||
log.NewDebug(params.OperationID, utils.GetSelfFuncName(), "ip:", ip)
|
||||
|
||||
ok, opUserID, _ := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), params.OperationID)
|
||||
if !ok || !utils.IsContain(opUserID, config.Config.Manager.AppManagerUid) {
|
||||
Limited, LimitError := imdb.IsLimitRegisterIp(ip)
|
||||
if LimitError != nil {
|
||||
log.Error(params.OperationID, utils.GetSelfFuncName(), LimitError, ip)
|
||||
@ -57,12 +60,15 @@ func SetPassword(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.RegisterLimit, "errMsg": "limited"})
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
openIMRegisterReq := api.UserRegisterReq{}
|
||||
var account string
|
||||
if params.Email != "" {
|
||||
account = params.Email
|
||||
openIMRegisterReq.Email = params.Email
|
||||
} else if params.PhoneNumber != "" {
|
||||
account = params.PhoneNumber
|
||||
openIMRegisterReq.PhoneNumber = params.PhoneNumber
|
||||
} else {
|
||||
account = params.UserID
|
||||
}
|
||||
@ -84,7 +90,7 @@ func SetPassword(c *gin.Context) {
|
||||
if config.Config.Demo.NeedInvitationCode && params.InvitationCode != "" {
|
||||
err := imdb.CheckInvitationCode(params.InvitationCode)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.InvitationError, "errMsg": "邀请码错误"})
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.InvitationError, "errMsg": "InvitationCode error"})
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -99,18 +105,13 @@ func SetPassword(c *gin.Context) {
|
||||
} else {
|
||||
userID = params.UserID
|
||||
}
|
||||
|
||||
url := config.Config.Demo.ImAPIURL + "/auth/user_register"
|
||||
openIMRegisterReq := api.UserRegisterReq{}
|
||||
openIMRegisterReq.OperationID = params.OperationID
|
||||
openIMRegisterReq.Platform = params.Platform
|
||||
openIMRegisterReq.UserID = userID
|
||||
openIMRegisterReq.Nickname = params.Nickname
|
||||
openIMRegisterReq.Secret = config.Config.Secret
|
||||
openIMRegisterReq.FaceURL = params.FaceURL
|
||||
// openIMRegisterReq.CreateIp = ip
|
||||
// openIMRegisterReq.LastLoginIp = ip
|
||||
// openIMRegisterReq.InvitationCode = params.InvitationCode
|
||||
openIMRegisterResp := api.UserRegisterResp{}
|
||||
log.NewDebug(params.OperationID, utils.GetSelfFuncName(), "register req:", openIMRegisterReq)
|
||||
bMsg, err := http2.Post(url, openIMRegisterReq, 2)
|
||||
@ -124,14 +125,10 @@ func SetPassword(c *gin.Context) {
|
||||
log.NewError(params.OperationID, "request openIM register error", account, "err", "resp: ", openIMRegisterResp.ErrCode)
|
||||
if err != nil {
|
||||
log.NewError(params.OperationID, utils.GetSelfFuncName(), err.Error())
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": "register limit"})
|
||||
return
|
||||
}
|
||||
if openIMRegisterResp.ErrCode == constant.RegisterLimit {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterLimit, "errMsg": "用户注册被限制"})
|
||||
return
|
||||
} else if openIMRegisterResp.ErrCode == constant.InvitationError {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.InvitationError, "errMsg": "邀请码错误"})
|
||||
return
|
||||
} else {
|
||||
if openIMRegisterResp.ErrCode != 0 {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": "register failed: " + openIMRegisterResp.ErrMsg})
|
||||
return
|
||||
}
|
||||
@ -150,8 +147,11 @@ func SetPassword(c *gin.Context) {
|
||||
imdb.FinishInvitationCode(params.InvitationCode, userID)
|
||||
}
|
||||
}
|
||||
if err := imdb.InsertIpRecord(userID, ip); err != nil {
|
||||
log.NewError(params.OperationID, utils.GetSelfFuncName(), userID, ip, err.Error())
|
||||
}
|
||||
|
||||
log.Info(params.OperationID, "end setPassword", account, params.Password)
|
||||
log.Info(params.OperationID, "end setuserInfo", account, params.Password)
|
||||
// demo onboarding
|
||||
if params.UserID == "" && config.Config.Demo.OnboardProcess {
|
||||
select {
|
||||
@ -168,6 +168,7 @@ func SetPassword(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// register add friend
|
||||
select {
|
||||
case ChImportFriend <- &pbFriend.ImportFriendReq{
|
||||
OperationID: params.OperationID,
|
||||
|
@ -29,70 +29,24 @@ func (rpc *rpcAuth) UserRegister(_ context.Context, req *pbAuth.UserRegisterReq)
|
||||
user.Birth = utils.UnixSecondToTime(int64(req.UserInfo.Birth))
|
||||
}
|
||||
log.Debug(req.OperationID, "copy ", user, req.UserInfo)
|
||||
Limited, LimitError := imdb.IsLimitRegisterIp(req.UserInfo.CreateIp)
|
||||
if LimitError != nil {
|
||||
return &pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: LimitError.Error()}}, nil
|
||||
}
|
||||
if Limited {
|
||||
return &pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.RegisterLimit, ErrMsg: "Register Limit"}}, nil
|
||||
}
|
||||
err := imdb.UserRegister(user)
|
||||
if err != nil {
|
||||
if err == constant.InvitationMsg {
|
||||
return &pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.InvitationError, ErrMsg: "邀请码错误"}}, nil
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{}})
|
||||
return &pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{}}, nil
|
||||
}
|
||||
|
||||
func (rpc *rpcAuth) UserToken(_ context.Context, req *pbAuth.UserTokenReq) (*pbAuth.UserTokenResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
|
||||
if config.Config.Demo.UseIPLimit {
|
||||
user, err := imdb.GetUserIPLimit(req.FromUserID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " imdb.GetUserByUserID failed " + err.Error() + req.FromUserID
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}}, nil
|
||||
}
|
||||
|
||||
var Limited bool
|
||||
var LimitError error
|
||||
Limited, LimitError = imdb.IsLimitLoginIp(req.LoginIp)
|
||||
if LimitError != nil {
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: LimitError.Error()}}, nil
|
||||
}
|
||||
if Limited {
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.LoginLimit, ErrMsg: "limited Login"}}, nil
|
||||
}
|
||||
Limited, LimitError = imdb.IsLimitUserLoginIp(user.UserID, req.LoginIp)
|
||||
if LimitError != nil {
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: LimitError.Error()}}, nil
|
||||
}
|
||||
if Limited {
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.LoginLimit, ErrMsg: "limited Login"}}, nil
|
||||
}
|
||||
Limited, LimitError = imdb.UserIsBlock(user.UserID)
|
||||
if LimitError != nil {
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: LimitError.Error()}}, nil
|
||||
}
|
||||
if Limited {
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.LoginLimit, ErrMsg: "limited Login"}}, nil
|
||||
}
|
||||
}
|
||||
|
||||
tokens, expTime, err := token_verify.CreateToken(req.FromUserID, int(req.Platform))
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{}, Token: tokens, ExpiredTime: expTime})
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{}, Token: tokens, ExpiredTime: expTime}, nil
|
||||
}
|
||||
|
@ -74,3 +74,15 @@ func GetRegisterUserNum(ip string) ([]string, error) {
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Model(&db.Register{}).Where("register_ip=?", ip).Pluck("user_id", &userIDList).Error
|
||||
return userIDList, err
|
||||
}
|
||||
|
||||
func InsertIpRecord(userID, createIp string) error {
|
||||
record := &db.UserIpRecord{UserID: userID, CreateIp: createIp, LastLoginTime: time.Now(), LoginTimes: 1}
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Model(&db.UserIpRecord{}).Create(record).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdateIpReocord(userID, ip string) error {
|
||||
record := &db.UserIpRecord{UserID: userID, LastLoginIp: ip, LastLoginTime: time.Now()}
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Model(&db.UserIpRecord{}).Updates(record).Updates("login_times = login_times + 1").Error
|
||||
return err
|
||||
}
|
||||
|
@ -43,9 +43,6 @@ func UserRegister(user db.User) error {
|
||||
if user.Birth.Unix() < 0 {
|
||||
user.Birth = utils.UnixSecondToTime(0)
|
||||
}
|
||||
// user.LastLoginTime = time.Now()
|
||||
// user.LoginTimes = 0
|
||||
// user.LastLoginIp = user.CreateIp
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Table("users").Create(&user).Error
|
||||
if err != nil {
|
||||
return err
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -72,15 +72,9 @@ message UserInfo{
|
||||
uint32 birth = 6;
|
||||
string email = 7;
|
||||
string ex = 8;
|
||||
string createIp = 9;
|
||||
uint32 createTime = 10;
|
||||
string LastLoginIp =11;
|
||||
uint32 LastLoginTime = 12;
|
||||
int32 LoginTimes = 13;
|
||||
int32 LoginLimit = 14;
|
||||
int32 appMangerLevel = 15;
|
||||
int32 globalRecvMsgOpt = 16;
|
||||
string invitationCode = 17;
|
||||
uint32 createTime = 9;
|
||||
int32 appMangerLevel = 10;
|
||||
int32 globalRecvMsgOpt = 11;
|
||||
}
|
||||
|
||||
message FriendInfo{
|
||||
|
Loading…
x
Reference in New Issue
Block a user