- first edition of auth2

This commit is contained in:
gazebo
2019-03-01 17:43:17 +08:00
parent b90313bd49
commit 421240ac54
17 changed files with 1015 additions and 12 deletions

View File

@@ -0,0 +1,86 @@
package mobile
import (
"errors"
"fmt"
"math/rand"
"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/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/KenmyZhang/aliyun-communicate"
)
const (
DefVerifyCodeDuration = 5 * time.Minute
TestMobile = "91112345678"
TestVerifyCode = "123456"
)
const (
AuthType = auth2.AuthTypeMobile
)
var (
ErrVerifyCodeIsWrong = errors.New("验证码错")
)
type Auther struct {
auth2.DefAuther
}
var (
AutherObj *Auther
)
func init() {
AutherObj = new(Auther)
auth2.RegisterAuther(AuthType, AutherObj)
}
// 特殊接口
func (a *Auther) SendVerifyCode(mobileNumber string) error {
code := fmt.Sprintf("%06d", rand.Intn(1000000))
globals.SugarLogger.Debugf("SendVerifyCode mobileNumber:%s, code:%s", mobileNumber, code)
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)
} else {
globals.SugarLogger.Infof("SendVerifyCode mobileNumber:%s failed with error:%v", mobileNumber, err)
}
return err
}
func (a *Auther) VerifySecret(mobileNumber, code string) (authBind *model.AuthBind, err error) {
globals.SugarLogger.Debugf("VerifySecret mobileNumber:%s, code:%s", mobileNumber, code)
err = ErrVerifyCodeIsWrong
if mobileNumber == TestMobile && code == TestVerifyCode {
err = nil
} else {
if value := api.Cacher.Get(mobileNumber); value != nil {
if code == value.(string) {
api.Cacher.Del(mobileNumber)
err = nil
}
}
}
return nil, err
}
// 此函数为空
func (a *Auther) AddAuthBind(authBind *model.AuthBind, userName string) (err error) {
return err
}
// 此函数为空
func (a *Auther) UnbindAuth(authInfo *auth2.AuthInfo, authType string) (err error) {
return err
}