- 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

174
controllers/auth2.go Normal file
View File

@@ -0,0 +1,174 @@
package controllers
import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/auth2"
"git.rosy.net.cn/jx-callback/business/auth2/authprovider/weixin"
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"github.com/astaxie/beego"
)
var (
ErrNeedV2Token = errors.New("需要V2版的TOKEN")
)
type Auth2Controller struct {
beego.Controller
}
// @Title 发送验证码
// @Description 发送验证码
// @Param authID formData string true "手机号或邮件"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SendVerifyCode [post]
func (c *Auth2Controller) SendVerifyCode() {
c.callSendVerifyCode(func(params *tAuth2SendVerifyCodeParams) (retVal interface{}, errCode string, err error) {
err = auth2.SendVerifyCode(params.AuthID)
return retVal, "", err
})
}
// @Title 登录接口
// @Description 登录接口(微信与公众号登录不能直接调用此接口)
// @Param authType formData string true "登录类型,当前支持[password本地账号密码mobile手机短信weixin:微信登录weixinmp微信公众号登录weixinmini小程序登录]"
// @Param authSecret formData string true "不同登录类型的登录秘密"
// @Param authID formData string false "登录ID登录类型为password时依赖于authIDType其它为相应登录类型的id"
// @Param authIDType formData string false "只有在登录类型为password时才有意义分别为userID2用户名emailmobile"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /Login [post]
func (c *Auth2Controller) Login() {
c.callLogin(func(params *tAuth2LoginParams) (retVal interface{}, errCode string, err error) {
retVal, err = auth2.Login(params.AuthType, params.AuthID, params.AuthIDType, params.AuthSecret)
return retVal, "", err
})
}
// @Title 微信认证回调接口
// @Description 微信认证回调接口,自己不能直接调用
// @Param code query string true "客户同意后得到的code"
// @Param block query string true "回调地址"
// @Param state query string false "微信回调的登录状态"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /WeixinOAuth2 [get]
func (c *Auth2Controller) WeixinOAuth2() {
var redirectURL string
c.callWeixinOAuth2(func(params *tAuth2WeixinOAuth2Params) (retVal interface{}, errCode string, err error) {
authInfo, err := auth2.Login(weixin.AuthTypeWeixin, params.State, "", params.Code)
var callResult *CallResult
if err == nil {
callResult = &CallResult{
Code: model.ErrCodeSuccess,
Data: string(utils.MustMarshal(authInfo)),
}
} else {
callResult = &CallResult{
Code: model.ErrCodeGeneralFailed,
Desc: err.Error(),
}
}
redirectURL = fmt.Sprintf("%s?info=%s", params.Block, base64.StdEncoding.EncodeToString(utils.MustMarshal(callResult)))
return retVal, model.ErrorCodeIgnore, err
})
c.Redirect(redirectURL, http.StatusTemporaryRedirect)
}
// @Title 微信公众号认证回调接口
// @Description 微信公众号认证回调接口,自己不能直接调用
// @Param code query string true "客户同意后得到的code"
// @Param block query string true "回调地址"
// @Param state query string false "微信回调的登录状态"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /WeixinMPOAuth2 [get]
func (c *Auth2Controller) WeixinMPOAuth2() {
var redirectURL string
c.callWeixinMPOAuth2(func(params *tAuth2WeixinMPOAuth2Params) (retVal interface{}, errCode string, err error) {
authInfo, err := auth2.Login(weixin.AuthTypeMP, params.State, "", params.Code)
var callResult *CallResult
if err == nil {
callResult = &CallResult{
Code: model.ErrCodeSuccess,
Data: string(utils.MustMarshal(authInfo)),
}
} else {
callResult = &CallResult{
Code: model.ErrCodeGeneralFailed,
Desc: err.Error(),
}
}
redirectURL = fmt.Sprintf("%s?info=%s", params.Block, base64.StdEncoding.EncodeToString(utils.MustMarshal(callResult)))
return retVal, model.ErrorCodeIgnore, err
})
c.Redirect(redirectURL, http.StatusTemporaryRedirect)
}
// @Title 登出接口
// @Description 登出接口此接口兼容V1的TOKEN
// @Param token header string true "认证token"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /Logout [delete]
func (c *Auth2Controller) Logout() {
c.callLogout(func(params *tAuth2LogoutParams) (retVal interface{}, errCode string, err error) {
if authInfo, ok := params.Ctx.GetLoginInfo().(*auth2.AuthInfo); ok {
err = auth2.Logout(authInfo)
} else {
err = auth.Logout(params.Token)
}
return nil, "", err
})
}
// @Title 绑定认证方式
// @Description 绑定认证方式
// @Param token header string true "认证token"
// @Param authToken formData string true "之前通过login得到的新认证TOKEN"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /AddAuthBind [post]
func (c *Auth2Controller) AddAuthBind() {
c.callAddAuthBind(func(params *tAuth2AddAuthBindParams) (retVal interface{}, errCode string, err error) {
authInfo, err2 := c.getAuth2Info(params.Ctx)
if err := err2; err == nil {
newAuthInfo, err2 := auth2.GetUserInfo(params.AuthToken)
if err = err2; err == nil {
err = auth2.AddAuthBind(authInfo, newAuthInfo)
}
}
return retVal, "", err
})
}
// @Title 删除认证方式
// @Description 删除认证方式
// @Param token header string true "认证token"
// @Param authType formData string true "登录类型,当前支持[weixin:微信登录weixinmp微信公众号登录weixinmini小程序登录]"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /RemoveAuthBind [post]
func (c *Auth2Controller) RemoveAuthBind() {
c.callRemoveAuthBind(func(params *tAuth2RemoveAuthBindParams) (retVal interface{}, errCode string, err error) {
authInfo, err2 := c.getAuth2Info(params.Ctx)
if err := err2; err == nil {
err = auth2.UnbindAuth(authInfo, params.AuthType)
}
return retVal, "", err
})
}
func (c *Auth2Controller) getAuth2Info(ctx *jxcontext.Context) (authInfo *auth2.AuthInfo, err error) {
if authInfo, ok := ctx.GetLoginInfo().(*auth2.AuthInfo); ok {
return authInfo, err
}
return nil, ErrNeedV2Token
}