This commit is contained in:
gazebo
2019-03-03 22:20:07 +08:00
parent af38ab535b
commit 6793e7443d
16 changed files with 330 additions and 59 deletions

View File

@@ -0,0 +1,38 @@
package authprovider
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"
)
type DefAuther struct {
}
// 此函数为空
func (a *DefAuther) AddAuthBind(authBind *model.AuthBind, userName string) (err error) {
dao.WrapAddIDCULDEntity(authBind, userName)
err = dao.CreateEntity(nil, authBind)
return err
}
func (a *DefAuther) UnbindAuth(authInfo *auth2.AuthInfo, authType string) (err error) {
_, err = dao.DeleteEntityLogically(nil, &model.AuthBind{}, nil, authInfo.GetID(), map[string]interface{}{
"UserID": authInfo.GetID(),
"Type": authType,
model.FieldDeletedAt: utils.DefaultTimeValue,
})
return err
}
func (a *DefAuther) SendVerifyCode(authID string) error {
return errors.New("当前登录类型不支持此操作")
}
// 此函数为空
func (a *DefAuther) Logout(authInfo *auth2.AuthInfo) error {
return nil
}

View File

@@ -8,6 +8,7 @@ import (
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/auth2"
"git.rosy.net.cn/jx-callback/business/auth2/authprovider"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
@@ -16,7 +17,6 @@ import (
const (
DefVerifyCodeDuration = 5 * time.Minute
TestMobile = "91112345678"
TestVerifyCode = "123456"
)
@@ -29,7 +29,7 @@ var (
)
type Auther struct {
auth2.DefAuther
authprovider.DefAuther
}
var (
@@ -62,7 +62,7 @@ func (a *Auther) VerifySecret(mobileNumber, code string) (authBind *model.AuthBi
globals.SugarLogger.Debugf("VerifySecret mobileNumber:%s, code:%s", mobileNumber, code)
err = ErrVerifyCodeIsWrong
if mobileNumber == TestMobile && code == TestVerifyCode {
if auth2.TestMobileMap[mobileNumber] == 1 && code == TestVerifyCode {
err = nil
} else {
if value := api.Cacher.Get(mobileNumber); value != nil {

View File

@@ -1,9 +1,13 @@
package password
import (
"crypto/sha1"
"errors"
"fmt"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/auth2"
"git.rosy.net.cn/jx-callback/business/auth2/authprovider"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
)
@@ -13,7 +17,7 @@ const (
)
type Auther struct {
auth2.DefAuther
authprovider.DefAuther
}
var (
@@ -25,7 +29,7 @@ func init() {
}
func (a *Auther) VerifySecret(userID, passMD5 string) (authBind *model.AuthBind, err error) {
if authBind, err = dao.GetAuthBind(nil, "", AuthType, userID, ""); err == nil {
if authBind, err = dao.GetAuthBind(dao.GetDB(), "", AuthType, userID); err == nil {
err = a.checkPassword(authBind, passMD5)
} else if dao.IsNoRowsError(err) {
err = auth2.ErrUserNotExist
@@ -36,19 +40,30 @@ func (a *Auther) VerifySecret(userID, passMD5 string) (authBind *model.AuthBind,
// 特殊接口
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 == "" {
db := dao.GetDB()
if authBind, err = dao.GetAuthBind(db, "", AuthType, userID); err == nil {
if err = a.checkPassword(authBind, oldPassMD5); err == nil || authBind.AuthSecret == "" { // 如果原密码为空,不判断原密码,代表重置密码
authBind.AuthSecret = newPassMD5
_, err = dao.UpdateEntity(nil, authBind, "AuthSecret")
_, err = dao.UpdateEntity(db, authBind, "AuthSecret")
}
} else if dao.IsNoRowsError(err) {
err = auth2.ErrUserNotExist
salt := utils.GetUUID()
err = a.AddAuthBind(&model.AuthBind{
Type: AuthType,
AuthID: userID,
AuthSecret: a.encryptPassword(newPassMD5, salt),
AuthSecret2: salt,
}, "admin")
}
return err
}
func (a *Auther) encryptPassword(password, salt string) string {
return fmt.Sprintf("%x", sha1.Sum([]byte(password+salt)))
}
func (a *Auther) checkPassword(authBind *model.AuthBind, passMD5 string) (err error) {
if authBind.AuthSecret != passMD5 {
if authBind.AuthSecret != a.encryptPassword(passMD5, authBind.AuthSecret2) {
return ErrUserAndPassNotMatch
}
return nil

View File

@@ -5,6 +5,7 @@ import (
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/auth2"
"git.rosy.net.cn/jx-callback/business/auth2/authprovider"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
@@ -13,12 +14,12 @@ import (
const (
AuthTypeWeixin = "weixin"
AuthTypeMP = "weixinmp"
AuthTypeMP = "weixinsns"
AuthTypeMini = "weixinmini"
)
type Auther struct {
auth2.DefAuther
authprovider.DefAuther
authType string
}
@@ -51,10 +52,10 @@ func (a *Auther) VerifySecret(state, code string) (authBind *model.AuthBind, err
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) {
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 {
if authBindList, err = dao.GetAuthBindsByAuthID2(db, wxUserinfo.UnionID, []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini}); err == nil && len(authBindList) > 0 {
authBind = authBindList[0]
authBind.Type = a.authType
authBind.AuthID = wxUserinfo.OpenID

View File

@@ -6,6 +6,7 @@ import (
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/auth2"
"git.rosy.net.cn/jx-callback/business/auth2/authprovider"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
@@ -13,7 +14,7 @@ import (
)
type MiniAuther struct {
auth2.DefAuther
authprovider.DefAuther
}
var (
@@ -35,10 +36,10 @@ func (a *MiniAuther) VerifySecret(dummy, jsCode string) (authBind *model.AuthBin
sessionInfo, err := api.WeixinMiniAPI.SNSCode2Session(jsCode)
if err == nil {
db := dao.GetDB()
if authBind, err = dao.GetAuthBind(db, "", AuthTypeMP, sessionInfo.OpenID, ""); dao.IsNoRowsError(err) {
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 {
if authBindList, err = dao.GetAuthBindsByAuthID2(db, sessionInfo.UnionID, []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini}); err == nil && len(authBindList) > 0 {
authBind = authBindList[0]
authBind.Type = AuthTypeMP
authBind.AuthID = sessionInfo.OpenID