- 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
}

View File

@@ -0,0 +1,55 @@
package password
import (
"errors"
"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"
)
const (
AuthType = auth2.AuthTypePassword
)
type Auther struct {
auth2.DefAuther
}
var (
ErrUserAndPassNotMatch = errors.New("用户名密码不匹配")
)
func init() {
auth2.RegisterAuther(AuthType, new(Auther))
}
func (a *Auther) VerifySecret(userID, passMD5 string) (authBind *model.AuthBind, err error) {
if authBind, err = dao.GetAuthBind(nil, "", AuthType, userID, ""); err == nil {
err = a.checkPassword(authBind, passMD5)
} else if dao.IsNoRowsError(err) {
err = auth2.ErrUserNotExist
}
return authBind, err
}
// 特殊接口
func (a *Auther) ChangePassword(userID, oldPassMD5, newPassMD5 string) (err error) {
var authBind *model.AuthBind
if authBind, err = dao.GetAuthBind(nil, "", AuthType, userID, ""); err == nil {
if err = a.checkPassword(authBind, oldPassMD5); err == nil || authBind.AuthSecret == "" {
authBind.AuthSecret = newPassMD5
_, err = dao.UpdateEntity(nil, authBind, "AuthSecret")
}
} else if dao.IsNoRowsError(err) {
err = auth2.ErrUserNotExist
}
return err
}
func (a *Auther) checkPassword(authBind *model.AuthBind, passMD5 string) (err error) {
if authBind.AuthSecret != passMD5 {
return ErrUserAndPassNotMatch
}
return nil
}

View File

@@ -0,0 +1,80 @@
package weixin
import (
"errors"
"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 (
AuthTypeWeixin = "weixin"
AuthTypeMP = "weixinmp"
AuthTypeMini = "weixinmini"
)
type Auther struct {
auth2.DefAuther
authType string
}
var (
AutherObjWX *Auther
AutherObjMP *Auther
)
var (
ErrStateIsWrong = errors.New("登录state非法")
)
func init() {
AutherObjWX = &Auther{
authType: AuthTypeWeixin,
}
auth2.RegisterAuther(AuthTypeWeixin, AutherObjWX)
AutherObjMP = &Auther{
authType: AuthTypeMP,
}
auth2.RegisterAuther(AuthTypeMP, AutherObjMP)
}
func (a *Auther) VerifySecret(state, code string) (authBind *model.AuthBind, err error) {
globals.SugarLogger.Debugf("weixin VerifySecret code:%s", code)
if state == "" {
token, err2 := api.WeixinAPI.SNSRetrieveToken(code)
if err = err2; err == nil {
wxUserinfo, err2 := api.WeixinAPI.SNSGetUserInfo(token.AccessToken, token.OpenID)
if err = err2; err == nil {
db := dao.GetDB()
if authBind, err = dao.GetAuthBind(db, "", a.authType, wxUserinfo.OpenID, ""); dao.IsNoRowsError(err) {
var authBindList []*model.AuthBind
if wxUserinfo.UnionID != "" {
if authBindList, err = dao.GetAuthBindsByWXUnionID(db, wxUserinfo.UnionID); err == nil && len(authBindList) > 0 {
authBind = authBindList[0]
authBind.Type = a.authType
authBind.AuthID = wxUserinfo.OpenID
authBind.DetailData = string(utils.MustMarshal(wxUserinfo))
err = a.AddAuthBind(authBind, wxUserinfo.NickName)
}
}
if err == nil && len(authBindList) == 0 {
authBind = &model.AuthBind{
Type: a.authType,
AuthID: wxUserinfo.OpenID,
AuthID2: wxUserinfo.UnionID,
DetailData: string(utils.MustMarshal(wxUserinfo)),
}
}
}
}
}
} else {
err = ErrStateIsWrong
}
return authBind, err
}

View File

@@ -0,0 +1,76 @@
package weixin
import (
"encoding/base64"
"errors"
"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"
)
type MiniAuther struct {
auth2.DefAuther
}
var (
ErrAuthTypeShouldBeMini = errors.New("当前操作要求是小程序登录方式")
)
var (
AutherObjMini *MiniAuther
)
func init() {
AutherObjMini = new(MiniAuther)
auth2.RegisterAuther(AuthTypeMP, AutherObjMini)
}
func (a *MiniAuther) VerifySecret(dummy, jsCode string) (authBind *model.AuthBind, err error) {
globals.SugarLogger.Debugf("weixin mini VerifySecret jsCode:%s", jsCode)
sessionInfo, err := api.WeixinMiniAPI.SNSCode2Session(jsCode)
if err == nil {
db := dao.GetDB()
if authBind, err = dao.GetAuthBind(db, "", AuthTypeMP, sessionInfo.OpenID, ""); dao.IsNoRowsError(err) {
var authBindList []*model.AuthBind
if sessionInfo.UnionID != "" {
if authBindList, err = dao.GetAuthBindsByWXUnionID(db, sessionInfo.UnionID); err == nil && len(authBindList) > 0 {
authBind = authBindList[0]
authBind.Type = AuthTypeMP
authBind.AuthID = sessionInfo.OpenID
authBind.DetailData = string(utils.MustMarshal(sessionInfo))
authBind.UserData = sessionInfo.SessionKey
err = a.AddAuthBind(authBind, "admin")
}
}
if err == nil && len(authBindList) == 0 {
authBind = &model.AuthBind{
Type: AuthTypeMP,
AuthID: sessionInfo.OpenID,
AuthID2: sessionInfo.UnionID,
DetailData: string(utils.MustMarshal(sessionInfo)),
UserData: sessionInfo.SessionKey,
}
}
}
}
return authBind, err
}
// 特殊接口
func (a *MiniAuther) DecryptData(authInfo *auth2.AuthInfo, encryptedData, iv string) (decryptedDataBase64 string, err error) {
globals.SugarLogger.Debugf("weixin mini DecryptData encryptedData:%s, iv:%s", encryptedData, iv)
if authInfo.AuthBindInfo.Type != AuthTypeMini {
return "", ErrAuthTypeShouldBeMini
}
sessionKey := authInfo.AuthBindInfo.UserData.(string)
decryptedData, err := api.WeixinMiniAPI.SNSDecodeMiniProgramData(encryptedData, sessionKey, iv)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(decryptedData), nil
}