- dingding auth

This commit is contained in:
gazebo
2019-03-07 21:15:19 +08:00
parent 123ffd4353
commit ce109a60b3
16 changed files with 238 additions and 85 deletions

View File

@@ -13,10 +13,10 @@ type DefAuther struct {
}
// 此函数为空
func (a *DefAuther) AddAuthBind(authBind *model.AuthBind, userName string) (err error) {
dao.WrapAddIDCULDEntity(authBind, userName)
authBind.Status = model.AuthBindStatusNormal
err = dao.CreateEntity(nil, authBind)
func (a *DefAuther) AddAuthBind(authBindEx *auth2.AuthBindEx, userName string) (err error) {
dao.WrapAddIDCULDEntity(authBindEx, userName)
authBindEx.Status = model.AuthBindStatusNormal
err = dao.CreateEntity(nil, authBindEx.AuthBind)
return err
}

View File

@@ -0,0 +1,6 @@
package dingding
const (
AuthTypeDingDing = "dingding"
AuthTypeStaff = "ddstaff" // 钉钉企业登录
)

View File

@@ -0,0 +1,74 @@
package dingding
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"
"git.rosy.net.cn/jx-callback/globals/api"
)
type StaffAuther struct {
authprovider.DefAuther
}
var (
AutherObjStaff *StaffAuther
)
func init() {
AutherObjStaff = new(StaffAuther)
auth2.RegisterAuther(AuthTypeStaff, AutherObjStaff)
}
func (a *StaffAuther) VerifySecret(dummy, code string) (authBindEx *auth2.AuthBindEx, err error) {
globals.SugarLogger.Debugf("dingding staff VerifySecret code:%s", code)
userID, err := api.DingDingAPI.GetUserID(code)
if err == nil {
db := dao.GetDB()
var authBind *model.AuthBind
if authBind, err = dao.GetAuthBind(db, "", AuthTypeStaff, userID.UserID); dao.IsNoRowsError(err) {
userDetail, err2 := api.DingDingAPI.GetUserDetail(userID.UserID)
if err = err2; err == nil {
userHint := &auth2.UserBasic{
UserID2: userID.UserID,
Mobile: utils.Interface2String(userDetail["mobile"]),
Email: utils.Interface2String(userDetail["email"]),
Name: utils.Interface2String(userDetail["name"]),
}
unionID := utils.Interface2String(userDetail["unionid"])
if unionID != "" {
var authBindList []*model.AuthBind
if authBindList, err = dao.GetAuthBindsByAuthID2(db, unionID, []string{AuthTypeDingDing, AuthTypeStaff}); err == nil && len(authBindList) > 0 {
authBind = authBindList[0]
authBind.Type = AuthTypeStaff
authBind.AuthID = userID.UserID
authBind.DetailData = string(utils.MustMarshal(userDetail))
authBindEx = &auth2.AuthBindEx{
AuthBind: *authBind,
UserHint: userHint,
}
err = a.AddAuthBind(authBindEx, "admin")
} else if dao.IsNoRowsError(err) {
err = nil
}
}
if err == nil && authBindEx == nil {
authBindEx = &auth2.AuthBindEx{
AuthBind: model.AuthBind{
Type: AuthTypeStaff,
AuthID: userID.UserID,
AuthID2: unionID,
DetailData: string(utils.MustMarshal(userDetail)),
},
UserHint: userHint,
}
}
}
}
}
return authBindEx, err
}

View File

@@ -9,7 +9,6 @@ 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"
"github.com/KenmyZhang/aliyun-communicate"
@@ -58,7 +57,7 @@ func (a *Auther) SendVerifyCode(mobileNumber string) error {
return err
}
func (a *Auther) VerifySecret(mobileNumber, code string) (authBind *model.AuthBind, err error) {
func (a *Auther) VerifySecret(mobileNumber, code string) (authBindEx *auth2.AuthBindEx, err error) {
globals.SugarLogger.Debugf("VerifySecret mobileNumber:%s, code:%s", mobileNumber, code)
err = ErrVerifyCodeIsWrong
@@ -76,7 +75,7 @@ func (a *Auther) VerifySecret(mobileNumber, code string) (authBind *model.AuthBi
}
// 此函数为空
func (a *Auther) AddAuthBind(authBind *model.AuthBind, userName string) (err error) {
func (a *Auther) AddAuthBind(authBindEx *auth2.AuthBindEx, userName string) (err error) {
return err
}

View File

@@ -34,14 +34,19 @@ func init() {
auth2.RegisterAuther(AuthType, AutherObj)
}
func (a *Auther) VerifySecret(userID, passMD5 string) (authBind *model.AuthBind, err error) {
func (a *Auther) VerifySecret(userID, passMD5 string) (authBindEx *auth2.AuthBindEx, err error) {
globals.SugarLogger.Debugf("localpass VerifySecret userID:%s", userID)
var authBind *model.AuthBind
if authBind, err = dao.GetAuthBind(dao.GetDB(), "", AuthType, userID); err == nil {
err = a.checkPassword(authBind, passMD5)
if err = a.checkPassword(authBind, passMD5); err == nil {
authBindEx = &auth2.AuthBindEx{
AuthBind: *authBind,
}
}
} else if dao.IsNoRowsError(err) {
err = auth2.ErrUserAuthTypeNotExist
}
return authBind, err
return authBindEx, err
}
// 特殊接口
@@ -58,12 +63,14 @@ func (a *Auther) ChangePassword(userID, oldPassMD5, newPassMD5 string) (err erro
}, "admin", nil)
}
} else if dao.IsNoRowsError(err) {
err = a.AddAuthBind(&model.AuthBind{
UserID: userID,
Type: AuthType,
AuthID: userID,
AuthSecret: encryptPwd,
AuthSecret2: salt,
err = a.AddAuthBind(&auth2.AuthBindEx{
AuthBind: model.AuthBind{
UserID: userID,
Type: AuthType,
AuthID: userID,
AuthSecret: encryptPwd,
AuthSecret2: salt,
},
}, "admin")
}
return err

View File

@@ -44,7 +44,7 @@ func init() {
auth2.RegisterAuther(AuthTypeMP, AutherObjMP)
}
func (a *Auther) VerifySecret(state, code string) (authBind *model.AuthBind, err error) {
func (a *Auther) VerifySecret(state, code string) (authBindEx *auth2.AuthBindEx, err error) {
globals.SugarLogger.Debugf("weixin VerifySecret code:%s", code)
if state == "" {
token, err2 := api.WeixinPageAPI.SNSRetrieveToken(code)
@@ -52,25 +52,29 @@ func (a *Auther) VerifySecret(state, code string) (authBind *model.AuthBind, err
wxUserinfo, err2 := api.WeixinPageAPI.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 authBind *model.AuthBind
if authBind, err = dao.GetAuthBind(db, "", a.authType, wxUserinfo.OpenID); dao.IsNoRowsError(err) && wxUserinfo.UnionID != "" {
var authBindList []*model.AuthBind
if wxUserinfo.UnionID != "" {
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
authBind.DetailData = string(utils.MustMarshal(wxUserinfo))
err = a.AddAuthBind(authBind, wxUserinfo.NickName)
} else if dao.IsNoRowsError(err) {
err = auth2.ErrUserAuthTypeNotExist
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
authBind.DetailData = string(utils.MustMarshal(wxUserinfo))
authBindEx = &auth2.AuthBindEx{
AuthBind: *authBind,
}
err = a.AddAuthBind(authBindEx, wxUserinfo.NickName)
} else if dao.IsNoRowsError(err) {
err = nil
}
if err == nil && len(authBindList) == 0 {
authBind = &model.AuthBind{
Type: a.authType,
AuthID: wxUserinfo.OpenID,
AuthID2: wxUserinfo.UnionID,
DetailData: string(utils.MustMarshal(wxUserinfo)),
if err == nil && authBindEx == nil {
authBindEx = &auth2.AuthBindEx{
AuthBind: model.AuthBind{
Type: a.authType,
AuthID: wxUserinfo.OpenID,
AuthID2: wxUserinfo.UnionID,
DetailData: string(utils.MustMarshal(wxUserinfo)),
},
}
}
}
@@ -79,5 +83,5 @@ func (a *Auther) VerifySecret(state, code string) (authBind *model.AuthBind, err
} else {
err = ErrStateIsWrong
}
return authBind, err
return authBindEx, err
}

View File

@@ -30,40 +30,44 @@ func init() {
auth2.RegisterAuther(AuthTypeMini, AutherObjMini)
}
func (a *MiniAuther) VerifySecret(dummy, jsCode string) (authBind *model.AuthBind, err error) {
func (a *MiniAuther) VerifySecret(dummy, jsCode string) (authBindEx *auth2.AuthBindEx, 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, "", AuthTypeMini, sessionInfo.OpenID); dao.IsNoRowsError(err) {
var authBind *model.AuthBind
if authBind, err = dao.GetAuthBind(db, "", AuthTypeMini, sessionInfo.OpenID); dao.IsNoRowsError(err) && sessionInfo.UnionID != "" {
var authBindList []*model.AuthBind
sessionKey := sessionInfo.SessionKey
sessionInfo.SessionKey = ""
if sessionInfo.UnionID != "" {
if authBindList, err = dao.GetAuthBindsByAuthID2(db, sessionInfo.UnionID, []string{AuthTypeWeixin, AuthTypeMini, AuthTypeMini}); err == nil && len(authBindList) > 0 {
authBind = authBindList[0]
authBind.Type = AuthTypeMini
authBind.AuthID = sessionInfo.OpenID
authBind.DetailData = string(utils.MustMarshal(sessionInfo))
authBind.UserData = sessionKey
err = a.AddAuthBind(authBind, "admin")
} else if dao.IsNoRowsError(err) {
err = auth2.ErrUserAuthTypeNotExist
if authBindList, err = dao.GetAuthBindsByAuthID2(db, sessionInfo.UnionID, []string{AuthTypeWeixin, AuthTypeMini, AuthTypeMini}); err == nil && len(authBindList) > 0 {
authBind = authBindList[0]
authBind.Type = AuthTypeMini
authBind.AuthID = sessionInfo.OpenID
authBind.DetailData = string(utils.MustMarshal(sessionInfo))
authBindEx = &auth2.AuthBindEx{
AuthBind: *authBind,
UserData: sessionKey,
}
err = a.AddAuthBind(authBindEx, "admin")
} else if dao.IsNoRowsError(err) {
err = nil
}
if err == nil && len(authBindList) == 0 {
authBind = &model.AuthBind{
Type: AuthTypeMini,
AuthID: sessionInfo.OpenID,
AuthID2: sessionInfo.UnionID,
DetailData: string(utils.MustMarshal(sessionInfo)),
UserData: sessionKey,
if err == nil && authBindEx == nil {
authBindEx = &auth2.AuthBindEx{
AuthBind: model.AuthBind{
Type: AuthTypeMini,
AuthID: sessionInfo.OpenID,
AuthID2: sessionInfo.UnionID,
DetailData: string(utils.MustMarshal(sessionInfo)),
},
UserData: sessionKey,
}
}
}
}
return authBind, err
return authBindEx, err
}
// 特殊接口