diff --git a/cmd/open_im_demo/main.go b/cmd/open_im_demo/main.go index 58ef79c9a..430b0faaf 100644 --- a/cmd/open_im_demo/main.go +++ b/cmd/open_im_demo/main.go @@ -21,6 +21,7 @@ func main() { authRouterGroup.POST("/verify", register.Verify) authRouterGroup.POST("/password", register.SetPassword) authRouterGroup.POST("/login", register.Login) + authRouterGroup.POST("/reset_password", register.ResetPassword) } log.NewPrivateLog("demo") ginPort := flag.Int("port", 42233, "get ginServerPort from cmd,default 42233 as port") diff --git a/internal/cms_api/statistics/statistics.go b/internal/cms_api/statistics/statistics.go index fd6485591..791e21c6d 100644 --- a/internal/cms_api/statistics/statistics.go +++ b/internal/cms_api/statistics/statistics.go @@ -32,7 +32,7 @@ func GetMessagesStatistics(c *gin.Context) { client := pb.NewUserClient(etcdConn) respPb, err := client.GetMessageStatistics(context.Background(), &reqPb) if err != nil { - openIMHttp.RespHttp200(c, constant.ErrServer, resp) + openIMHttp.RespHttp200(c, err, resp) log.NewError("0", utils.GetSelfFuncName(), err.Error()) return } @@ -78,7 +78,7 @@ func GetUserStatistics(c *gin.Context) { respPb, err := client.GetUserStatistics(context.Background(), &reqPb) if err != nil { log.NewError("0", utils.GetSelfFuncName(), err.Error()) - openIMHttp.RespHttp200(c, constant.ErrServer, nil) + openIMHttp.RespHttp200(c, err, nil) return } // utils.CopyStructFields(&resp, respPb) @@ -132,7 +132,7 @@ func GetGroupStatistics(c *gin.Context) { client := pb.NewUserClient(etcdConn) respPb, err := client.GetGroupStatistics(context.Background(), &reqPb) if err != nil { - openIMHttp.RespHttp200(c, constant.ErrServer, nil) + openIMHttp.RespHttp200(c, err, nil) return } // utils.CopyStructFields(&resp, respPb) @@ -179,7 +179,7 @@ func GetActiveUser(c *gin.Context) { client := pb.NewUserClient(etcdConn) respPb, err := client.GetActiveUser(context.Background(), &reqPb) if err != nil { - openIMHttp.RespHttp200(c, constant.ErrServer, nil) + openIMHttp.RespHttp200(c, err, nil) return } utils.CopyStructFields(&resp.ActiveUserList, respPb.Users) @@ -204,7 +204,7 @@ func GetActiveGroup(c *gin.Context) { respPb, err := client.GetActiveGroup(context.Background(), &reqPb) if err != nil { log.NewError("0", "BindJSON failed ", err.Error()) - openIMHttp.RespHttp200(c, constant.ErrServer, nil) + openIMHttp.RespHttp200(c, err, nil) return } for _, group := range respPb.Groups { diff --git a/internal/demo/register/reset_password.go b/internal/demo/register/reset_password.go new file mode 100644 index 000000000..0ff7115a0 --- /dev/null +++ b/internal/demo/register/reset_password.go @@ -0,0 +1,56 @@ +package register + +import ( + "Open_IM/pkg/common/config" + "Open_IM/pkg/common/constant" + "Open_IM/pkg/common/db" + "Open_IM/pkg/common/db/mysql_model/im_mysql_model" + "Open_IM/pkg/common/log" + "Open_IM/pkg/utils" + "github.com/gin-gonic/gin" + "net/http" +) + +type resetPasswordRequest struct { + VerificationCode string `json:"verificationCode"` + Email string `json:"email"` + PhoneNumber string `json:"phoneNumber"` + NewPassword string `json:"newPassword"` + OperationID string `json:"operationID"` +} + +func ResetPassword(c *gin.Context) { + var ( + req resetPasswordRequest + ) + if err := c.BindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()}) + return + } + var account string + if req.Email != "" { + account = req.Email + } else { + account = req.PhoneNumber + } + if req.VerificationCode != config.Config.Demo.SuperCode { + accountKey := account + "_" + constant.VerificationCodeForResetSuffix + v, err := db.DB.GetAccountCode(accountKey) + if err != nil || v != req.VerificationCode { + log.NewError(req.OperationID, "password Verification code error", account, req.VerificationCode, v) + c.JSON(http.StatusOK, gin.H{"errCode": constant.CodeInvalidOrExpired, "errMsg": "Verification code error!"}) + return + } + } + user, err := im_mysql_model.GetRegister(account) + if err != nil || user.Account == "" { + log.NewError(req.OperationID, utils.GetSelfFuncName(), "get register error", err.Error()) + c.JSON(http.StatusOK, gin.H{"errCode": constant.NotRegistered, "errMsg": "user not register!"}) + return + } + if err := im_mysql_model.ResetPassword(account, req.NewPassword); err != nil { + c.JSON(http.StatusOK, gin.H{"errCode": constant.ResetPasswordFailed, "errMsg": "reset password failed: "+err.Error()}) + return + } + c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "reset password success"}) +} diff --git a/internal/demo/register/send_code.go b/internal/demo/register/send_code.go index 5d6e3e20f..2b890a308 100644 --- a/internal/demo/register/send_code.go +++ b/internal/demo/register/send_code.go @@ -21,6 +21,7 @@ type paramsVerificationCode struct { Email string `json:"email"` PhoneNumber string `json:"phoneNumber"` OperationID string `json:"operationID" binding:"required"` + UsedFor int `json:"usedFor" binding:"required"` } func SendVerificationCode(c *gin.Context) { @@ -36,25 +37,32 @@ func SendVerificationCode(c *gin.Context) { } else { account = params.PhoneNumber } - _, err := im_mysql_model.GetRegister(account) - if err == nil { - log.NewError(params.OperationID, "The phone number has been registered", params) - c.JSON(http.StatusOK, gin.H{"errCode": constant.HasRegistered, "errMsg": ""}) - return - } - ok, err := db.DB.JudgeAccountEXISTS(account) - if ok || err != nil { - log.NewError(params.OperationID, "The phone number has been registered", params) - c.JSON(http.StatusOK, gin.H{"errCode": constant.RepeatSendCode, "errMsg": ""}) - return + var accountKey string + switch params.UsedFor { + case constant.VerificationCodeForRegister: + _, err := im_mysql_model.GetRegister(account) + if err == nil { + log.NewError(params.OperationID, "The phone number has been registered", params) + c.JSON(http.StatusOK, gin.H{"errCode": constant.HasRegistered, "errMsg": ""}) + return + } + ok, err := db.DB.JudgeAccountEXISTS(account) + if ok || err != nil { + log.NewError(params.OperationID, "The phone number has been registered", params) + c.JSON(http.StatusOK, gin.H{"errCode": constant.RepeatSendCode, "errMsg": ""}) + return + } + accountKey = account + "_" + constant.VerificationCodeForRegisterSuffix + + case constant.VerificationCodeForReset: + accountKey = account + "_" + constant.VerificationCodeForResetSuffix } - log.InfoByKv("begin sendSms", account) + log.NewInfo(params.OperationID, params.UsedFor,"begin store redis", accountKey) rand.Seed(time.Now().UnixNano()) code := 100000 + rand.Intn(900000) - log.NewInfo(params.OperationID, "begin store redis", account) - err = db.DB.SetAccountCode(account, code, config.Config.Demo.CodeTTL) + err := db.DB.SetAccountCode(accountKey, code, config.Config.Demo.CodeTTL) if err != nil { - log.NewError(params.OperationID, "set redis error", account, "err", err.Error()) + log.NewError(params.OperationID, "set redis error", accountKey, "err", err.Error()) c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"}) return } diff --git a/internal/demo/register/set_password.go b/internal/demo/register/set_password.go index a0558fa5a..7e27e04b3 100644 --- a/internal/demo/register/set_password.go +++ b/internal/demo/register/set_password.go @@ -11,7 +11,6 @@ import ( "Open_IM/pkg/utils" "encoding/json" "fmt" - "github.com/garyburd/redigo/redis" "github.com/gin-gonic/gin" "net/http" ) @@ -29,6 +28,7 @@ type ParamsSetPassword struct { func SetPassword(c *gin.Context) { params := ParamsSetPassword{} if err := c.BindJSON(¶ms); err != nil { + log.NewError(params.OperationID, utils.GetSelfFuncName(), "bind json failed", err.Error()) c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()}) return } @@ -39,7 +39,8 @@ func SetPassword(c *gin.Context) { account = params.PhoneNumber } if params.VerificationCode != config.Config.Demo.SuperCode { - v, err := redis.String(db.DB.Exec("GET", account)) + accountKey := account + "_" + constant.VerificationCodeForRegisterSuffix + v, err := db.DB.GetAccountCode(accountKey) if err != nil || v != params.VerificationCode { log.NewError(params.OperationID, "password Verification code error", account, params.VerificationCode) data := make(map[string]interface{}) @@ -65,7 +66,7 @@ func SetPassword(c *gin.Context) { err = json.Unmarshal(bMsg, &openIMRegisterResp) if err != nil || openIMRegisterResp.ErrCode != 0 { log.NewError(params.OperationID, "request openIM register error", account, "err", "") - c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": ""}) + c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": "register failed: "+openIMRegisterResp.ErrMsg}) return } log.Info(params.OperationID, "begin store mysql", account, params.Password) diff --git a/internal/demo/register/verify.go b/internal/demo/register/verify.go index d74ce1f66..c4e1b0c78 100644 --- a/internal/demo/register/verify.go +++ b/internal/demo/register/verify.go @@ -15,6 +15,7 @@ type paramsCertification struct { PhoneNumber string `json:"phoneNumber"` VerificationCode string `json:"verificationCode"` OperationID string `json:"operationID" binding:"required"` + UsedFor int `json:"usedFor" binding:"required"` } func Verify(c *gin.Context) { diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index 34c075a00..5b90f5132 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -124,6 +124,12 @@ const ( //Minio MinioDurationTimes = 3600 + + // verificationCode used for + VerificationCodeForRegister = 1 + VerificationCodeForReset = 2 + VerificationCodeForRegisterSuffix = "_forRegister" + VerificationCodeForResetSuffix = "_forReset" ) var ContentType2PushContent = map[int64]string{ diff --git a/pkg/common/constant/error.go b/pkg/common/constant/error.go index 923d0bbd8..339f2c953 100644 --- a/pkg/common/constant/error.go +++ b/pkg/common/constant/error.go @@ -80,6 +80,7 @@ const ( SmsSendCodeErr = 10008 CodeInvalidOrExpired = 10009 RegisterFailed = 10010 + ResetPasswordFailed = 10011 DatabaseError = 10002 ServerError = 10004 HttpError = 10005 diff --git a/pkg/common/db/mysql_model/im_mysql_model/demo_model.go b/pkg/common/db/mysql_model/im_mysql_model/demo_model.go index 5af615f97..2e5d7df86 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/demo_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/demo_model.go @@ -14,6 +14,7 @@ func GetRegister(account string) (*db.Register, error) { return &r, dbConn.Table("registers").Where("account = ?", account).Take(&r).Error } + func SetPassword(account, password, ex string) error { r := db.Register{ Account: account, @@ -25,5 +26,16 @@ func SetPassword(account, password, ex string) error { return err } return dbConn.Table("registers").Create(&r).Error +} +func ResetPassword(account, password string) error { + r := db.Register{ + Password:password, + } + dbConn, err := db.DB.MysqlDB.DefaultGormDB() + dbConn.LogMode(true) + if err != nil { + return err + } + return dbConn.Table("registers").Where("account = ?", account).Update(&r).Error } diff --git a/pkg/common/db/redisModel.go b/pkg/common/db/redisModel.go index a314090e4..08c11cf56 100644 --- a/pkg/common/db/redisModel.go +++ b/pkg/common/db/redisModel.go @@ -3,11 +3,13 @@ package db import ( "Open_IM/pkg/common/constant" log2 "Open_IM/pkg/common/log" + "fmt" "github.com/garyburd/redigo/redis" ) const ( - registerAccountTempCode = "REGISTER_ACCOUNT_TEMP_CODE" + AccountTempCode = "ACCOUNT_TEMP_CODE" + resetPwdTempCode = "RESET_PWD_TEMP_CODE" userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq appleDeviceToken = "DEVICE_TOKEN" userMinSeq = "REDIS_USER_MIN_SEQ:" @@ -35,19 +37,21 @@ func (d *DataBases) Exec(cmd string, key interface{}, args ...interface{}) (inte return con.Do(cmd, params...) } func (d *DataBases) JudgeAccountEXISTS(account string) (bool, error) { - key := registerAccountTempCode + account + key := AccountTempCode + account return redis.Bool(d.Exec("EXISTS", key)) } func (d *DataBases) SetAccountCode(account string, code, ttl int) (err error) { - key := registerAccountTempCode + account - _, err = d.Exec("Set", key, code, ttl) + key := AccountTempCode + account + _, err = d.Exec("SET", key, code) return err } func (d *DataBases) GetAccountCode(account string) (string, error) { - key := userIncrSeq + account + key := AccountTempCode + account + fmt.Println(key) return redis.String(d.Exec("GET", key)) } + //Perform seq auto-increment operation of user messages func (d *DataBases) IncrUserSeq(uid string) (uint64, error) { key := userIncrSeq + uid