作者 yangfu

增加配置 多端登录控制

... ... @@ -351,10 +351,12 @@ func CenterCompanys(header *protocol.RequestHeader, request *protocolx.CenterCom
break
case protocol.LoginByCredentials:
//凭证是否存在
if credential, e := userAuth.NewRedisUserCredential(request.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {
log.Debug("凭证过期或者已失效:", request.Phone)
err = protocol.NewErrWithMessage(4140, err)
return
if constant.DISENABLE_MULTI_DEVICE_LOGIN {
if credential, e := userAuth.NewRedisUserCredential(request.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {
log.Debug("凭证过期或者已失效:", request.Phone)
err = protocol.NewErrWithMessage(4140, err)
return
}
}
if _, err = utils.ParseJWTToken(request.Credentials); err != nil {
err = protocol.NewErrWithMessage(4140, err)
... ... @@ -419,10 +421,12 @@ func Companys(header *protocol.RequestHeader, request *protocolx.CompanysRequest
return
}
//凭证是否存在
if credential, e := userAuth.NewRedisUserCredential(claim.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {
log.Debug("凭证过期或者已失效:", claim.Phone)
err = protocol.NewErrWithMessage(4140, err)
return
if constant.DISENABLE_MULTI_DEVICE_LOGIN {
if credential, e := userAuth.NewRedisUserCredential(claim.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {
log.Debug("凭证过期或者已失效:", claim.Phone)
err = protocol.NewErrWithMessage(4140, err)
return
}
}
loginSvr.Init(claim.Phone)
rsp.Partner, _ = loginSvr.PartnerStaticInfo()
... ... @@ -458,11 +462,13 @@ func LoginV2(header *protocol.RequestHeader, request *protocol.LoginRequestV2) (
err = protocol.NewErrWithMessage(4140, err)
return
}
//凭证是否存在
if credential, e := userAuth.NewRedisUserCredential(claim.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {
log.Debug("凭证过期或者已失效:", claim.Phone)
err = protocol.NewErrWithMessage(4140, err)
return
if constant.DISENABLE_MULTI_DEVICE_LOGIN {
//凭证是否存在
if credential, e := userAuth.NewRedisUserCredential(claim.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {
log.Debug("凭证过期或者已失效:", claim.Phone)
err = protocol.NewErrWithMessage(4140, err)
return
}
}
if company, e := CompanyRepository.FindOne(map[string]interface{}{"id": request.Cid, "enable": 1}); e != nil || company == nil {
err = protocol.NewErrWithMessage(10007, err)
... ...
... ... @@ -3,10 +3,13 @@ package event
import (
"github.com/tiptok/gocomm/xa/eda"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/event/subscriber"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain/event"
)
func InitEventCenter() {
eda.RegisterSubscribe(&event.AccessTokenEvent{}, &subscriber.AuthCacheSubscribe{})
eda.RegisterSubscribe(&event.RefreshTokenEvent{}, &subscriber.AuthCacheSubscribe{})
if constant.DISENABLE_MULTI_DEVICE_LOGIN {
eda.RegisterSubscribe(&event.AccessTokenEvent{}, &subscriber.AuthCacheSubscribe{})
eda.RegisterSubscribe(&event.RefreshTokenEvent{}, &subscriber.AuthCacheSubscribe{})
}
}
... ...
... ... @@ -21,6 +21,9 @@ var DEFAULT_GUEST_COMPANY int = 10011
var SHARE_SHOP_PREVIEW_IMADGE = "https://media.fjmaimaimai.com/image/default/3E0C7050C13147CE8C7AF86C75F904E9-6-2.jpg" //分享店铺预览图
var WEHAT_MINI_PROGRAM_VERSION_TYPE = 2 //0:正式版 1:开发版 2:体验版
var DISENABLE_MULTI_DEVICE_LOGIN = false //不允许多端登录 (注:正式环境需要为true 测试环境设为false,方便多个设备测试)
func init() {
if os.Getenv("LOG_LEVEL") != "" {
LOG_LEVEL = os.Getenv("LOG_LEVEL")
... ...
... ... @@ -32,4 +32,7 @@ func init() {
if os.Getenv("DISABLE_SQL_GENERATE_PRINT") != "" {
DISABLE_SQL_GENERATE_PRINT = true
}
if POSTGRESQL_DB_NAME == "partner" {
DISENABLE_MULTI_DEVICE_LOGIN = true
}
}
... ...
... ... @@ -3,6 +3,7 @@ package middleware
import (
"github.com/astaxie/beego/context"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/userAuth"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
"strconv"
... ... @@ -39,15 +40,17 @@ func CheckJWTToken(ctx *context.Context) {
} else {
ctx.Input.SetData("UserId", u.UserId)
// valid token
userPhone, _ := strconv.Atoi(u.Phone)
tokenAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(int64(userPhone)))
err := tokenAuth.Check(
userAuth.NewOptions(userAuth.WithAccessToken(token)),
)
if err != nil {
msg = protocol.NewMesage(4141)
return
if constant.DISENABLE_MULTI_DEVICE_LOGIN {
// valid token
userPhone, _ := strconv.Atoi(u.Phone)
tokenAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(int64(userPhone)))
err := tokenAuth.Check(
userAuth.NewOptions(userAuth.WithAccessToken(token)),
)
if err != nil {
msg = protocol.NewMesage(4141)
return
}
}
}
return
... ...