- refactor verify code

This commit is contained in:
gazebo
2019-03-11 10:37:57 +08:00
parent 899e0d8362
commit 1012a855ea
2 changed files with 63 additions and 15 deletions

View File

@@ -2,11 +2,25 @@ package authprovider
import ( import (
"errors" "errors"
"fmt"
"math/rand"
"strings"
"time"
"git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/auth2" "git.rosy.net.cn/jx-callback/business/auth2"
"git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
)
const (
DefVerifyCodeDuration = 5 * time.Minute
VerifyCodeHeader = "VC"
VerifyCodeVer = "V2"
TokenTypeSep = "."
) )
type DefAuther struct { type DefAuther struct {
@@ -85,3 +99,47 @@ func (a *DefAuther) UnionFindAuthBind(curAuthType string, unionAuthTypeList []st
} }
return authBindEx, err return authBindEx, err
} }
// cache相关
func (a *DefAuther) SaveVerifyCode(keyID, verifyCode string) {
api.Cacher.Set(a.buildCacheKey(keyID), verifyCode, DefVerifyCodeDuration)
}
func (a *DefAuther) LoadVerifyCode(keyID string) (verifyCode string) {
if value := api.Cacher.Get(a.buildCacheKey(keyID)); value != nil {
return value.(string)
}
return ""
}
func (a *DefAuther) DeleteVerifyCode(keyID string) {
api.Cacher.Del(a.buildCacheKey(keyID))
}
// cache相关
func (a *DefAuther) GenerateVerifyCode(keyID string) (verifyCode string) {
verifyCode = a.LoadVerifyCode(keyID)
if verifyCode == "" {
verifyCode = fmt.Sprintf("%06d", rand.Intn(1000000))
}
globals.SugarLogger.Debugf("GenerateVerifyCode:%s", verifyCode)
return verifyCode
}
func (a *DefAuther) VerifyCode(keyID, verifyCode string) (isSame bool) {
savedVerifyCode := a.LoadVerifyCode(keyID)
if isSame = (verifyCode != "" && savedVerifyCode != "" && verifyCode == savedVerifyCode); isSame {
a.DeleteVerifyCode(keyID)
}
return isSame
}
func (a *DefAuther) buildCacheKey(keyID string) string {
return strings.Join([]string{
VerifyCodeHeader,
VerifyCodeVer,
keyID,
}, TokenTypeSep)
}

View File

@@ -2,9 +2,6 @@ package mobile
import ( import (
"errors" "errors"
"fmt"
"math/rand"
"time"
"git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/auth2" "git.rosy.net.cn/jx-callback/business/auth2"
@@ -15,12 +12,8 @@ import (
) )
const ( const (
DefVerifyCodeDuration = 5 * time.Minute AuthType = auth2.AuthTypeMobile
TestVerifyCode = "123456" TestVerifyCode = "123456"
)
const (
AuthType = auth2.AuthTypeMobile
) )
var ( var (
@@ -42,15 +35,13 @@ func init() {
// 特殊接口 // 特殊接口
func (a *Auther) SendVerifyCode(mobileNumber string) error { func (a *Auther) SendVerifyCode(mobileNumber string) error {
code := fmt.Sprintf("%06d", rand.Intn(1000000)) code := a.GenerateVerifyCode(mobileNumber)
globals.SugarLogger.Debugf("SendVerifyCode mobileNumber:%s, code:%s", mobileNumber, code)
smsClient := aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/") smsClient := aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/")
_, err := smsClient.Execute(globals.AliKey, globals.AliSecret, mobileNumber, "京西菜市", "SMS_84655036", string(utils.MustMarshal(map[string]interface{}{ _, err := smsClient.Execute(globals.AliKey, globals.AliSecret, mobileNumber, "京西菜市", "SMS_84655036", string(utils.MustMarshal(map[string]interface{}{
"code": code, "code": code,
}))) })))
if err == nil { if err == nil {
api.Cacher.Set(mobileNumber, code, DefVerifyCodeDuration) a.SaveVerifyCode(mobileNumber, code)
} else { } else {
globals.SugarLogger.Infof("SendVerifyCode mobileNumber:%s failed with error:%v", mobileNumber, err) globals.SugarLogger.Infof("SendVerifyCode mobileNumber:%s failed with error:%v", mobileNumber, err)
} }
@@ -65,8 +56,7 @@ func (a *Auther) VerifySecret(mobileNumber, code string) (authBindEx *auth2.Auth
err = nil err = nil
} else { } else {
if value := api.Cacher.Get(mobileNumber); value != nil { if value := api.Cacher.Get(mobileNumber); value != nil {
if code == value.(string) { if a.VerifyCode(mobileNumber, code) {
api.Cacher.Del(mobileNumber)
err = nil err = nil
} }
} }