- first edition of auth2
This commit is contained in:
80
business/auth2/authprovider/weixin/weixin.go
Normal file
80
business/auth2/authprovider/weixin/weixin.go
Normal 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
|
||||
}
|
||||
76
business/auth2/authprovider/weixin/weixin_mini.go
Normal file
76
business/auth2/authprovider/weixin/weixin_mini.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user