作者 yangfu

扫码登录修改

... ... @@ -107,45 +107,36 @@ func (svr AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (inte
//AuthLoginQrcode 扫码登录
func (svr AuthService) AuthLoginQrcode(queryParam *query.QrcodeLoginStatusQuery) (interface{}, error) {
qrmsg := domain.QrcodeMessage{}
failLoginData := map[string]interface{}{
"isLogin": false,
"access": struct{}{},
}
err := qrmsg.ParseToken(queryParam.Key)
cache := cache.LoginQrcodeCache{}
qrcodeMessage, err := cache.Get(queryParam.Key)
if err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.TRANSACTION_ERROR, "二维码已失效")
}
qrCache := cache.LoginQrcodeCache{}
qrmsgCache, err := qrCache.Get(qrmsg.Id)
if err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.TRANSACTION_ERROR, "二维码已失效")
}
if !qrmsgCache.IsLogin {
if !qrcodeMessage.IsLogin {
return failLoginData, nil
}
loginToken := domain.LoginToken{
UserId: qrmsgCache.UserId,
UserBaseId: qrmsgCache.UserBaseId,
Account: qrmsgCache.Account,
UserId: qrcodeMessage.UserId,
UserBaseId: qrcodeMessage.UserBaseId,
Account: qrcodeMessage.Account,
Platform: domain.LoginPlatformWeb,
CompanyId: qrmsgCache.CompanyId,
OrgId: qrmsgCache.OrgId,
}
// accessToken, err := loginToken.GenerateAccessToken()
// if err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
qrCache.Remove(qrmsgCache.Id)
// _ = accessToken
CompanyId: qrcodeMessage.CompanyId,
OrgId: qrcodeMessage.OrgId,
}
cache.Remove(queryParam.Key)
result, err := svr.getToken(loginToken)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
data := map[string]interface{}{
"isLogin": qrmsgCache.IsLogin,
"isLogin": qrcodeMessage.IsLogin,
"access": result["token"],
}
return data, nil
... ... @@ -153,20 +144,15 @@ func (svr AuthService) AuthLoginQrcode(queryParam *query.QrcodeLoginStatusQuery)
//AuthLoginQrcodeBind 扫码登录-绑定
func (svr AuthService) AuthLoginQrcodeBinding(bindingCmd *command.QrcodeBindingCommand) (interface{}, error) {
qrmsg := domain.QrcodeMessage{}
err := qrmsg.ParseToken(bindingCmd.Key)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "您扫描的二维码无效,请确认后重新扫描")
}
qrCache := cache.LoginQrcodeCache{}
qrmsgCache, err := qrCache.Get(qrmsg.Id)
qrcodeMessage, err := qrCache.Get(bindingCmd.Key)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "您扫描的二维码无效,请确认后重新扫描")
}
if err := qrmsgCache.BindUser(bindingCmd.Operator); err != nil {
if err := qrcodeMessage.BindUser(bindingCmd.Operator); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := qrCache.Save(*qrmsgCache); err != nil {
if err := qrCache.Save(*qrcodeMessage); err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录失败,请重试")
}
... ... @@ -721,19 +707,18 @@ func (svr AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUs
//GetQrcode 获取扫码登录需要的二维码
func (svr AuthService) GetQrcode() (interface{}, error) {
qrmsg := domain.QrcodeMessage{}
_, err := qrmsg.GenerateImageBase64() //imgBase64
qrcodeMessage := domain.QrcodeMessage{}
_, err := qrcodeMessage.Init() //imgBase64
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
qrCache := cache.LoginQrcodeCache{}
err = qrCache.Save(qrmsg)
err = qrCache.Save(qrcodeMessage)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
data := map[string]interface{}{
//"image": imgBase64,
"key": qrmsg.Token,
"key": qrcodeMessage.Token,
}
return data, nil
}
... ...
... ... @@ -4,22 +4,22 @@ import (
"encoding/base64"
"fmt"
"github.com/forgoer/openssl"
"github.com/google/uuid"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
"time"
jwt "github.com/dgrijalva/jwt-go"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
"strings"
)
const (
qrcodeTokenSecret string = "bbe35ad433dd8e67"
qrcodeCodeExpire int64 = 60 * 30 //15分钟过期
QrcodeCodeExpire int64 = 60 * 5 //5分钟过期
)
var aecSecret = []byte("mmm.qrcode.ecb.1")
var qrcodeLogin = "/v1/auth/login/qrcode?key="
type QrcodeMessage struct {
jwt.StandardClaims
Id string `json:"id"`
Token string `json:"token"`
IsLogin bool `json:"isLogin"`
... ... @@ -34,46 +34,20 @@ type QrcodeMessage struct {
OrgId int64 `json:"orgId"`
}
func (qrmsg *QrcodeMessage) GenerateImageBase64() ([]byte, error) {
nowTime := time.Now().Unix()
qrmsg.StandardClaims = jwt.StandardClaims{
NotBefore: nowTime,
IssuedAt: nowTime,
ExpiresAt: nowTime + qrcodeCodeExpire,
Issuer: "allied_creation_gateway",
}
qrmsg.Id = fmt.Sprintf("%d", time.Now().UnixNano())
token := jwt.NewWithClaims(jwt.SigningMethodHS256, *qrmsg)
str, err := token.SignedString([]byte(qrcodeTokenSecret))
if err != nil {
return nil, err
}
func (qrmsg *QrcodeMessage) Init() ([]byte, error) {
str := strings.Replace(uuid.New().String(), "-", "", -1)
key := constant.ALLIED_CREATION_GATEWAY_HOST + qrcodeLogin + str
log.Logger.Debug("key:" + key)
encryptedData, err := openssl.AesECBEncrypt([]byte(key), aecSecret, openssl.PKCS7_PADDING)
if err != nil {
return nil, err
}
qrmsg.Id = str
qrmsg.Token = base64.StdEncoding.EncodeToString(encryptedData)
qrmsg.IsLogin = false
return encryptedData, err
}
func (qrmsg *QrcodeMessage) ParseToken(str string) error {
tokenClaims, err := jwt.ParseWithClaims(
str,
qrmsg,
func(token *jwt.Token) (interface{}, error) {
return []byte(loginTokenSecret), nil
})
if err != nil {
return err
}
if claim, ok := tokenClaims.Claims.(*QrcodeMessage); ok && tokenClaims.Valid {
*qrmsg = *claim
}
return nil
}
func (qrmsg *QrcodeMessage) BindUser(operator Operator) error {
if qrmsg.IsLogin {
return fmt.Errorf("登录中")
... ...
... ... @@ -17,11 +17,7 @@ func (lq LoginQrcodeCache) keyString(str string) string {
}
func (lq LoginQrcodeCache) Save(qrcode domain.QrcodeMessage) error {
nowTime := time.Now().Unix()
exp := qrcode.ExpiresAt - nowTime
if exp <= 0 {
exp = 60 * 5
}
var exp = domain.QrcodeCodeExpire
key := lq.keyString(qrcode.Id)
bt, _ := json.Marshal(qrcode)
result := clientRedis.Set(key, string(bt), time.Duration(exp)*time.Second)
... ...