From 1012a855ea73efc54ad8b3ed23446141ec918781 Mon Sep 17 00:00:00 2001 From: gazebo Date: Mon, 11 Mar 2019 10:37:57 +0800 Subject: [PATCH] - refactor verify code --- business/auth2/authprovider/defauther.go | 58 ++++++++++++++++++++ business/auth2/authprovider/mobile/mobile.go | 20 ++----- 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/business/auth2/authprovider/defauther.go b/business/auth2/authprovider/defauther.go index 5cf9cade1..adbbe752a 100644 --- a/business/auth2/authprovider/defauther.go +++ b/business/auth2/authprovider/defauther.go @@ -2,11 +2,25 @@ package authprovider import ( "errors" + "fmt" + "math/rand" + "strings" + "time" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/auth2" "git.rosy.net.cn/jx-callback/business/model" "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 { @@ -85,3 +99,47 @@ func (a *DefAuther) UnionFindAuthBind(curAuthType string, unionAuthTypeList []st } 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) +} diff --git a/business/auth2/authprovider/mobile/mobile.go b/business/auth2/authprovider/mobile/mobile.go index 3f0775e97..29e6a3f62 100644 --- a/business/auth2/authprovider/mobile/mobile.go +++ b/business/auth2/authprovider/mobile/mobile.go @@ -2,9 +2,6 @@ package mobile import ( "errors" - "fmt" - "math/rand" - "time" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/auth2" @@ -15,12 +12,8 @@ import ( ) const ( - DefVerifyCodeDuration = 5 * time.Minute - TestVerifyCode = "123456" -) - -const ( - AuthType = auth2.AuthTypeMobile + AuthType = auth2.AuthTypeMobile + TestVerifyCode = "123456" ) var ( @@ -42,15 +35,13 @@ func init() { // 特殊接口 func (a *Auther) SendVerifyCode(mobileNumber string) error { - code := fmt.Sprintf("%06d", rand.Intn(1000000)) - globals.SugarLogger.Debugf("SendVerifyCode mobileNumber:%s, code:%s", mobileNumber, code) - + code := a.GenerateVerifyCode(mobileNumber) smsClient := aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/") _, err := smsClient.Execute(globals.AliKey, globals.AliSecret, mobileNumber, "京西菜市", "SMS_84655036", string(utils.MustMarshal(map[string]interface{}{ "code": code, }))) if err == nil { - api.Cacher.Set(mobileNumber, code, DefVerifyCodeDuration) + a.SaveVerifyCode(mobileNumber, code) } else { 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 } else { if value := api.Cacher.Get(mobileNumber); value != nil { - if code == value.(string) { - api.Cacher.Del(mobileNumber) + if a.VerifyCode(mobileNumber, code) { err = nil } }