diff --git a/go.mod b/go.mod index f8caab7d..7947b357 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,8 @@ module github.com/openimsdk/chat go 1.21.2 +toolchain go1.21.9 + require ( github.com/gin-gonic/gin v1.9.1 github.com/golang-jwt/jwt/v4 v4.5.0 @@ -25,7 +27,7 @@ require ( require ( github.com/livekit/protocol v1.10.1 github.com/mitchellh/mapstructure v1.5.0 - github.com/openimsdk/gomake v0.0.9 + github.com/openimsdk/gomake v0.0.11 github.com/openimsdk/protocol v0.0.63 github.com/openimsdk/tools v0.0.49-alpha.3 github.com/redis/go-redis/v9 v9.5.1 diff --git a/go.sum b/go.sum index 9052f235..0f8859dd 100644 --- a/go.sum +++ b/go.sum @@ -178,8 +178,8 @@ github.com/nats-io/nkeys v0.4.6/go.mod h1:4DxZNzenSVd1cYQoAa8948QY3QDjrHfcfVADym github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/openimsdk/gomake v0.0.9 h1:ouf25ygN2PMQ68Gfgns/EQRPiLPnp+77SIr68GfE+n4= -github.com/openimsdk/gomake v0.0.9/go.mod h1:PndCozNc2IsQIciyn9mvEblYWZwJmAI+06z94EY+csI= +github.com/openimsdk/gomake v0.0.11 h1:jJ9286zKFfBeARkmfqMEcUYg9lJ+Cj9lylxP8W9uCFM= +github.com/openimsdk/gomake v0.0.11/go.mod h1:PndCozNc2IsQIciyn9mvEblYWZwJmAI+06z94EY+csI= github.com/openimsdk/protocol v0.0.63 h1:9DnweZe9nEYDFa4fGTbC9Cqi0gLUdtBhRo1NRP2X3WQ= github.com/openimsdk/protocol v0.0.63/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8= github.com/openimsdk/tools v0.0.49-alpha.3 h1:mXKU09asKTLjj0yCsfqqMizs3ibl1vLimW7ZYzAMw44= diff --git a/internal/rpc/chat/login.go b/internal/rpc/chat/login.go index 86f6bcc3..87d161a3 100644 --- a/internal/rpc/chat/login.go +++ b/internal/rpc/chat/login.go @@ -50,7 +50,7 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe if req.AreaCode == "" || req.PhoneNumber == "" { return nil, errs.ErrArgs.WrapMsg("area code or phone number is empty") } - if req.AreaCode[0] != '+' { + if !strings.HasPrefix(req.AreaCode, "+") { req.AreaCode = "+" + req.AreaCode } if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil { @@ -313,7 +313,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) ( } var registerType int32 if req.User.PhoneNumber != "" { - if req.User.AreaCode[0] != '+' { + if !strings.HasPrefix(req.User.AreaCode, "+") { req.User.AreaCode = "+" + req.User.AreaCode } if _, err := strconv.ParseUint(req.User.AreaCode[1:], 10, 64); err != nil { @@ -415,7 +415,7 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes if req.AreaCode == "" { return nil, errs.ErrArgs.WrapMsg("area code must") } - if req.AreaCode[0] != '+' { + if !strings.HasPrefix(req.AreaCode, "+") { req.AreaCode = "+" + req.AreaCode } if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil { diff --git a/pkg/protocol/admin/admin.proto b/pkg/protocol/admin/admin.proto index 87c4cbfd..4185cf4e 100644 --- a/pkg/protocol/admin/admin.proto +++ b/pkg/protocol/admin/admin.proto @@ -19,7 +19,6 @@ import "sdkws/sdkws.proto"; import "common/common.proto"; option go_package = "github.com/openimsdk/chat/pkg/protocol/admin"; -//登录 message LoginReq { string account = 1; string password = 2; @@ -103,8 +102,6 @@ message GetAdminInfoResp { int64 createTime = 8; } -// ################### 默认好友 ################### - message AddDefaultFriendReq { repeated string userIDs = 1; } @@ -139,9 +136,6 @@ message SearchDefaultFriendResp { repeated DefaultFriendAttribute users = 2; } -// ################### 默认群组 ################### - - message AddDefaultGroupReq { repeated string groupIDs = 1; } @@ -176,8 +170,6 @@ message SearchDefaultGroupResp { repeated string groupIDs = 2; } -// ################### 邀请码 ################### - message AddInvitationCodeReq { repeated string codes = 1; } @@ -237,8 +229,6 @@ message SearchInvitationCodeResp { repeated InvitationRegister list = 2; } -// ################### 用户登录ip限制 ################### - message SearchUserIPLimitLoginReq { string keyword = 1; openim.sdkws.RequestPagination pagination = 2; @@ -275,9 +265,6 @@ message DelUserIPLimitLoginReq { message DelUserIPLimitLoginResp { } - -// ################### 用户IP限制 ################### - message IPForbidden { string ip = 1; bool limitRegister = 2; @@ -312,7 +299,6 @@ message DelIPForbiddenReq { } message DelIPForbiddenResp {} -// ################### 用户限制 ################### message CheckRegisterForbiddenReq { string ip = 1; } @@ -328,7 +314,6 @@ message CheckLoginForbiddenReq { message CheckLoginForbiddenResp { } -// ################### 注销 ################### message CancellationUserReq{ string userID = 1; string reason = 2; @@ -336,7 +321,6 @@ message CancellationUserReq{ message CancellationUserResp {} -// ################### 封号、解封 ################### message BlockUserReq{ string userID = 1; string reason = 2; @@ -392,8 +376,6 @@ message FindUserBlockInfoResp{ repeated BlockInfo blocks = 2; } -// ################### TOKEN ################### - message CreateTokenReq{ string userID = 2; int32 userType = 3; @@ -413,8 +395,6 @@ message ParseTokenResp{ int64 expireTimeSeconds = 4; } -// ################### 小程序 ################### - message AddAppletReq { string id = 1; string name = 2; diff --git a/pkg/protocol/chat/chat.go b/pkg/protocol/chat/chat.go index 12b76c87..dde34766 100644 --- a/pkg/protocol/chat/chat.go +++ b/pkg/protocol/chat/chat.go @@ -17,6 +17,7 @@ package chat import ( "regexp" "strconv" + "strings" "github.com/openimsdk/chat/pkg/common/constant" pconstant "github.com/openimsdk/protocol/constant" @@ -36,7 +37,7 @@ func (x *UpdateUserInfoReq) Check() error { } func (x *FindUserPublicInfoReq) Check() error { - if x.UserIDs == nil { + if len(x.UserIDs) == 0 { return errs.ErrArgs.WrapMsg("userIDs is empty") } return nil @@ -56,7 +57,7 @@ func (x *SearchUserPublicInfoReq) Check() error { } func (x *FindUserFullInfoReq) Check() error { - if x.UserIDs == nil { + if len(x.UserIDs) == 0 { return errs.ErrArgs.WrapMsg("userIDs is empty") } return nil @@ -66,53 +67,52 @@ func (x *SendVerifyCodeReq) Check() error { if x.UsedFor < constant.VerificationCodeForRegister || x.UsedFor > constant.VerificationCodeForLogin { return errs.ErrArgs.WrapMsg("usedFor flied is empty") } - if x.Email == "" { - if x.AreaCode == "" { - return errs.ErrArgs.WrapMsg("AreaCode is empty") - } else if err := AreaCodeCheck(x.AreaCode); err != nil { + if x.PhoneNumber == "" && x.Email == "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is empty") + } + if x.PhoneNumber != "" && x.Email != "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is not empty") + } + if x.Email != "" { + if err := EmailCheck(x.Email); err != nil { return err } - if x.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("PhoneNumber is empty") - } else if err := PhoneNumberCheck(x.PhoneNumber); err != nil { + } + if x.PhoneNumber != "" { + if err := AreaCodeCheck(x.AreaCode); err != nil { return err } - } else { - if err := EmailCheck(x.Email); err != nil { + if err := PhoneNumberCheck(x.PhoneNumber); err != nil { return err } } - return nil } func (x *VerifyCodeReq) Check() error { - if x.Email == "" { - if x.AreaCode == "" { - return errs.ErrArgs.WrapMsg("AreaCode is empty") - } else if err := AreaCodeCheck(x.AreaCode); err != nil { + if x.PhoneNumber == "" && x.Email == "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is empty") + } + if x.PhoneNumber != "" && x.Email != "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is not empty") + } + if x.Email != "" { + if err := EmailCheck(x.Email); err != nil { return err } - if x.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("PhoneNumber is empty") - } else if err := PhoneNumberCheck(x.PhoneNumber); err != nil { + } + if x.PhoneNumber != "" { + if err := AreaCodeCheck(x.AreaCode); err != nil { return err } - } else { - if err := EmailCheck(x.Email); err != nil { + if err := PhoneNumberCheck(x.PhoneNumber); err != nil { return err } } - if x.VerifyCode == "" { - return errs.ErrArgs.WrapMsg("VerifyCode is empty") - } return nil } func (x *RegisterUserReq) Check() error { - //if x.VerifyCode == "" { - // return errs.ErrArgs.WrapMsg("VerifyCode is empty") - //} if x.User.Nickname == "" { return errs.ErrArgs.WrapMsg("Nickname is nil") } @@ -122,19 +122,22 @@ func (x *RegisterUserReq) Check() error { if x.User == nil { return errs.ErrArgs.WrapMsg("user is empty") } - if x.User.Email == "" { - if x.User.AreaCode == "" { - return errs.ErrArgs.WrapMsg("AreaCode is empty") - } else if err := AreaCodeCheck(x.User.AreaCode); err != nil { + if x.User.PhoneNumber == "" && x.User.Email == "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is empty") + } + if x.User.PhoneNumber != "" && x.User.Email != "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is not empty") + } + if x.User.Email != "" { + if err := EmailCheck(x.User.Email); err != nil { return err } - if x.User.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("PhoneNumber is empty") - } else if err := PhoneNumberCheck(x.User.PhoneNumber); err != nil { + } + if x.User.PhoneNumber != "" { + if err := AreaCodeCheck(x.User.AreaCode); err != nil { return err } - } else { - if err := EmailCheck(x.User.Email); err != nil { + if err := PhoneNumberCheck(x.User.PhoneNumber); err != nil { return err } } @@ -145,19 +148,16 @@ func (x *LoginReq) Check() error { if x.Platform < pconstant.IOSPlatformID || x.Platform > pconstant.AdminPlatformID { return errs.ErrArgs.WrapMsg("platform is invalid") } - if x.Email == "" { - if x.AreaCode == "" { - return errs.ErrArgs.WrapMsg("AreaCode is empty") - } else if err := AreaCodeCheck(x.AreaCode); err != nil { + if x.Email != "" { + if err := EmailCheck(x.Email); err != nil { return err } - if x.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("PhoneNumber is empty") - } else if err := PhoneNumberCheck(x.PhoneNumber); err != nil { + } + if x.PhoneNumber != "" { + if err := AreaCodeCheck(x.AreaCode); err != nil { return err } - } else { - if err := EmailCheck(x.Email); err != nil { + if err := PhoneNumberCheck(x.PhoneNumber); err != nil { return err } } @@ -168,25 +168,25 @@ func (x *ResetPasswordReq) Check() error { if x.Password == "" { return errs.ErrArgs.WrapMsg("password is empty") } - if x.Email == "" { - if x.AreaCode == "" { - return errs.ErrArgs.WrapMsg("AreaCode is empty") - } else if err := AreaCodeCheck(x.AreaCode); err != nil { + if x.PhoneNumber == "" && x.Email == "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is empty") + } + if x.PhoneNumber != "" && x.Email != "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is not empty") + } + if x.Email != "" { + if err := EmailCheck(x.Email); err != nil { return err } - if x.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("PhoneNumber is empty") - } else if err := PhoneNumberCheck(x.PhoneNumber); err != nil { + } + if x.PhoneNumber != "" { + if err := AreaCodeCheck(x.AreaCode); err != nil { return err } - } else { - if err := EmailCheck(x.Email); err != nil { + if err := PhoneNumberCheck(x.PhoneNumber); err != nil { return err } } - if x.VerifyCode == "" { - return errs.ErrArgs.WrapMsg("VerifyCode is empty") - } return nil } @@ -251,10 +251,22 @@ func EmailCheck(email string) error { } func AreaCodeCheck(areaCode string) error { - //pattern := `\+[1-9][0-9]{1,2}` - //if err := regexMatch(pattern, areaCode); err != nil { - // return errs.WrapMsg(err, "AreaCode is invalid") - //} + if areaCode == "" { + return errs.ErrArgs.WrapMsg("AreaCode is empty") + } + if strings.HasPrefix(areaCode, "+") { + areaCode = areaCode[1:] + } + if areaCode == "" { + return errs.ErrArgs.WrapMsg("invalid AreaCode") + } + val, err := strconv.ParseUint(areaCode, 10, 32) + if err != nil { + return errs.ErrArgs.WrapMsg("invalid AreaCode") + } + if val > 1000 { + return errs.ErrArgs.WrapMsg("invalid AreaCode") + } return nil } @@ -296,22 +308,23 @@ func (x *AddUserAccountReq) Check() error { return errs.ErrArgs.WrapMsg("user is empty") } - if x.User.Email == "" { - if x.User.AreaCode == "" || x.User.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("area code or phone number is empty") - } - if x.User.AreaCode[0] != '+' { - x.User.AreaCode = "+" + x.User.AreaCode - } - if _, err := strconv.ParseUint(x.User.AreaCode[1:], 10, 64); err != nil { - return errs.ErrArgs.WrapMsg("area code must be number") + if x.User.PhoneNumber == "" && x.User.Email == "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is empty") + } + if x.User.PhoneNumber != "" && x.User.Email != "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is not empty") + } + if x.User.Email != "" { + if err := EmailCheck(x.User.Email); err != nil { + return err } - if _, err := strconv.ParseUint(x.User.PhoneNumber, 10, 64); err != nil { - return errs.ErrArgs.WrapMsg("phone number must be number") + } + if x.User.PhoneNumber != "" { + if err := AreaCodeCheck(x.User.AreaCode); err != nil { + return err } - } else { - if err := EmailCheck(x.User.Email); err != nil { - return errs.ErrArgs.WrapMsg("email must be right") + if err := PhoneNumberCheck(x.User.PhoneNumber); err != nil { + return err } }