Skip to content

Commit

Permalink
feat: change verify code enable to use (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
icey-yu authored Feb 12, 2025
1 parent 4ece40e commit d361085
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 55 deletions.
4 changes: 2 additions & 2 deletions config/chat-rpc-chat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ verifyCode:
superCode: "666666"
len: 6
phone:
use: ""
use: "superCode" # superCode: user superCode; ali: use ali verify code;
ali:
endpoint: ""
accessKeyId: ""
accessKeySecret: ""
signName: ""
verificationCodeTemplateCode: ""
mail:
enable: false
use: "superCode" # superCode: user superCode; mail: use mail verify code;
title: ""
senderMail: ""
senderAuthorizationCode: ""
Expand Down
98 changes: 75 additions & 23 deletions internal/rpc/chat/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ import (
"github.com/openimsdk/chat/pkg/protocol/chat"
)

type verifyType int

const (
phone verifyType = iota
mail
)

func (o *chatSvr) verifyCodeJoin(areaCode, phoneNumber string) string {
return areaCode + " " + phoneNumber
}
Expand Down Expand Up @@ -85,24 +92,38 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe
if o.SMS == nil && o.Mail == nil {
return &chat.SendVerifyCodeResp{}, nil // super code
}
if req.Email != "" {
switch o.conf.Mail.Use {
case constant.VerifySuperCode:
return &chat.SendVerifyCodeResp{}, nil // super code
case constant.Email:
default:
return nil, errs.ErrInternalServer.WrapMsg("email verification code is not enabled")
}
}

if req.AreaCode == "" {
switch o.conf.Phone.Use {
case constant.VerifySuperCode:
return &chat.SendVerifyCodeResp{}, nil // super code
case constant.VerifyALi:
default:
return nil, errs.ErrInternalServer.WrapMsg("phone verification code is not enabled")
}
}

isEmail := req.Email != ""
var (
code = o.genVerifyCode()
account string
sendCode func() error
)
if isEmail {
if o.Mail == nil {
return nil, errs.ErrInternalServer.WrapMsg("email verification code is not enabled")
}
sendCode = func() error {
return o.Mail.SendMail(ctx, req.Email, code)
}
account = req.Email
} else {
if o.SMS == nil {
return nil, errs.ErrInternalServer.WrapMsg("mobile phone verification code is not enabled")
}
sendCode = func() error {
return o.SMS.SendCode(ctx, req.AreaCode, req.PhoneNumber, code)
}
Expand Down Expand Up @@ -136,16 +157,35 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe
return &chat.SendVerifyCodeResp{}, nil
}

func (o *chatSvr) verifyCode(ctx context.Context, account string, verifyCode string) (string, error) {
func (o *chatSvr) verifyCode(ctx context.Context, account string, verifyCode string, type_ verifyType) (string, error) {
if verifyCode == "" {
return "", errs.ErrArgs.WrapMsg("verify code is empty")
}
if o.SMS == nil && o.Mail == nil {
if o.Code.SuperCode != verifyCode {
return "", eerrs.ErrVerifyCodeNotMatch.Wrap()
switch type_ {
case phone:
switch o.conf.Phone.Use {
case constant.VerifySuperCode:
if o.Code.SuperCode != verifyCode {
return "", eerrs.ErrVerifyCodeNotMatch.Wrap()
}
return "", nil
case constant.VerifyALi:
default:
return "", errs.ErrInternalServer.WrapMsg("phone verification code is not enabled", "use", o.conf.Phone.Use)
}
case mail:
switch o.conf.Mail.Use {
case constant.VerifySuperCode:
if o.Code.SuperCode != verifyCode {
return "", eerrs.ErrVerifyCodeNotMatch.Wrap()
}
return "", nil
case constant.VerifyMail:
default:
return "", errs.ErrInternalServer.WrapMsg("email verification code is not enabled")
}
return "", nil
}

last, err := o.Database.TakeLastVerifyCode(ctx, account)
if err != nil {
if dbutil.IsDBNotFound(err) {
Expand Down Expand Up @@ -179,12 +219,16 @@ func (o *chatSvr) VerifyCode(ctx context.Context, req *chat.VerifyCodeReq) (*cha
var account string
if req.PhoneNumber != "" {
account = o.verifyCodeJoin(req.AreaCode, req.PhoneNumber)
if _, err := o.verifyCode(ctx, account, req.VerifyCode, phone); err != nil {
return nil, err
}
} else {
account = req.Email
if _, err := o.verifyCode(ctx, account, req.VerifyCode, mail); err != nil {
return nil, err
}
}
if _, err := o.verifyCode(ctx, account, req.VerifyCode); err != nil {
return nil, err
}

return &chat.VerifyCodeResp{}, nil
}

Expand Down Expand Up @@ -247,11 +291,11 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
}
}
if req.User.Email == "" {
if _, err := o.verifyCode(ctx, o.verifyCodeJoin(req.User.AreaCode, req.User.PhoneNumber), req.VerifyCode); err != nil {
if _, err := o.verifyCode(ctx, o.verifyCodeJoin(req.User.AreaCode, req.User.PhoneNumber), req.VerifyCode, phone); err != nil {
return nil, err
}
} else {
if _, err := o.verifyCode(ctx, req.User.Email, req.VerifyCode); err != nil {
if _, err := o.verifyCode(ctx, req.User.Email, req.VerifyCode, mail); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -411,16 +455,24 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes
}
var verifyCodeID *string
if req.Password == "" {
var account string
var (
id string
)

if req.Email == "" {
account = o.verifyCodeJoin(req.AreaCode, req.PhoneNumber)
account := o.verifyCodeJoin(req.AreaCode, req.PhoneNumber)
id, err = o.verifyCode(ctx, account, req.VerifyCode, phone)
if err != nil {
return nil, err
}
} else {
account = req.Email
}
id, err := o.verifyCode(ctx, account, req.VerifyCode)
if err != nil {
return nil, err
account := req.Email
id, err = o.verifyCode(ctx, account, req.VerifyCode, mail)
if err != nil {
return nil, err
}
}

if id != "" {
verifyCodeID = &id
}
Expand Down
5 changes: 3 additions & 2 deletions internal/rpc/chat/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package chat

import (
"context"

"github.com/openimsdk/tools/errs"

"github.com/openimsdk/chat/pkg/common/constant"
Expand All @@ -35,9 +36,9 @@ func (o *chatSvr) ResetPassword(ctx context.Context, req *chat.ResetPasswordReq)
var verifyCodeID string
var err error
if req.Email == "" {
verifyCodeID, err = o.verifyCode(ctx, o.verifyCodeJoin(req.AreaCode, req.PhoneNumber), req.VerifyCode)
verifyCodeID, err = o.verifyCode(ctx, o.verifyCodeJoin(req.AreaCode, req.PhoneNumber), req.VerifyCode, phone)
} else {
verifyCodeID, err = o.verifyCode(ctx, req.Email, req.VerifyCode)
verifyCodeID, err = o.verifyCode(ctx, req.Email, req.VerifyCode, mail)
}

if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion internal/rpc/chat/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package chat

import (
"context"
"strings"
"time"

"github.com/openimsdk/chat/pkg/common/constant"
"github.com/openimsdk/chat/pkg/common/mctx"
"github.com/openimsdk/chat/pkg/common/rtc"
"github.com/openimsdk/chat/pkg/protocol/admin"
Expand Down Expand Up @@ -39,6 +41,9 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
return err
}
var srv chatSvr
config.RpcConfig.VerifyCode.Phone.Use = strings.ToLower(config.RpcConfig.VerifyCode.Phone.Use)
config.RpcConfig.VerifyCode.Mail.Use = strings.ToLower(config.RpcConfig.VerifyCode.Mail.Use)
srv.conf = config.RpcConfig.VerifyCode
switch config.RpcConfig.VerifyCode.Phone.Use {
case "ali":
ali := config.RpcConfig.VerifyCode.Phone.Ali
Expand All @@ -47,7 +52,7 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
return err
}
}
if mail := config.RpcConfig.VerifyCode.Mail; mail.Enable {
if mail := config.RpcConfig.VerifyCode.Mail; mail.Use == constant.VerifyMail {
srv.Mail = email.NewMail(mail.SMTPAddr, mail.SMTPPort, mail.SenderMail, mail.SenderAuthorizationCode, mail.Title)
}
srv.Database, err = database.NewChatDatabase(mgocli)
Expand Down Expand Up @@ -75,6 +80,7 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg

type chatSvr struct {
chat.UnimplementedChatServer
conf config.VerifyCode
Database database.ChatDatabaseInterface
Admin *chatClient.AdminClient
SMS sms.SMS
Expand Down
56 changes: 29 additions & 27 deletions pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,40 +112,42 @@ type Chat struct {
ListenIP string `mapstructure:"listenIP"`
Ports []int `mapstructure:"ports"`
} `mapstructure:"rpc"`
VerifyCode struct {
ValidTime int `mapstructure:"validTime"`
ValidCount int `mapstructure:"validCount"`
UintTime int `mapstructure:"uintTime"`
MaxCount int `mapstructure:"maxCount"`
SuperCode string `mapstructure:"superCode"`
Len int `mapstructure:"len"`
Phone struct {
Use string `mapstructure:"use"`
Ali struct {
Endpoint string `mapstructure:"endpoint"`
AccessKeyID string `mapstructure:"accessKeyId"`
AccessKeySecret string `mapstructure:"accessKeySecret"`
SignName string `mapstructure:"signName"`
VerificationCodeTemplateCode string `mapstructure:"verificationCodeTemplateCode"`
} `mapstructure:"ali"`
} `mapstructure:"phone"`
Mail struct {
Enable bool `mapstructure:"enable"`
Title string `mapstructure:"title"`
SenderMail string `mapstructure:"senderMail"`
SenderAuthorizationCode string `mapstructure:"senderAuthorizationCode"`
SMTPAddr string `mapstructure:"smtpAddr"`
SMTPPort int `mapstructure:"smtpPort"`
} `mapstructure:"mail"`
} `mapstructure:"verifyCode"`
LiveKit struct {
VerifyCode VerifyCode `mapstructure:"verifyCode"`
LiveKit struct {
URL string `mapstructure:"url"`
Key string `mapstructure:"key"`
Secret string `mapstructure:"secret"`
} `mapstructure:"liveKit"`
AllowRegister bool `mapstructure:"allowRegister"`
}

type VerifyCode struct {
ValidTime int `mapstructure:"validTime"`
ValidCount int `mapstructure:"validCount"`
UintTime int `mapstructure:"uintTime"`
MaxCount int `mapstructure:"maxCount"`
SuperCode string `mapstructure:"superCode"`
Len int `mapstructure:"len"`
Phone struct {
Use string `mapstructure:"use"`
Ali struct {
Endpoint string `mapstructure:"endpoint"`
AccessKeyID string `mapstructure:"accessKeyId"`
AccessKeySecret string `mapstructure:"accessKeySecret"`
SignName string `mapstructure:"signName"`
VerificationCodeTemplateCode string `mapstructure:"verificationCodeTemplateCode"`
} `mapstructure:"ali"`
} `mapstructure:"phone"`
Mail struct {
Use string `mapstructure:"use"`
Title string `mapstructure:"title"`
SenderMail string `mapstructure:"senderMail"`
SenderAuthorizationCode string `mapstructure:"senderAuthorizationCode"`
SMTPAddr string `mapstructure:"smtpAddr"`
SMTPPort int `mapstructure:"smtpPort"`
} `mapstructure:"mail"`
}

type Admin struct {
RPC struct {
RegisterIP string `mapstructure:"registerIP"`
Expand Down
7 changes: 7 additions & 0 deletions pkg/common/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,10 @@ const (
CredentialPhone
CredentialEmail
)

// verifyCode use
const (
VerifySuperCode = "supercode"
VerifyALi = "ali"
VerifyMail = "mail"
)

0 comments on commit d361085

Please sign in to comment.