作者 yangfu

Merge remote-tracking branch 'origin/dev' into test

# Conflicts:
#	main.go
... ... @@ -2,11 +2,13 @@ package main
import (
"github.com/astaxie/beego"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/event"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
_ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
_ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg"
_ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
//"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/ginsvr"
_ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr"
)
... ... @@ -15,6 +17,11 @@ func main() {
log.Info("app stop!")
}()
log.Info("app start!")
log.Info(constant.POSTGRESQL_HOST, constant.POSTGRESQL_DB_NAME)
log.Info(constant.POSTGRESQL_DB_NAME)
//ginsvr.Run()
//注册事件
event.InitEventCenter()
beego.Run()
}
... ...
... ... @@ -2,8 +2,10 @@ package auth
import (
"fmt"
"github.com/tiptok/gocomm/xa/eda"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain/event"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/domain_service"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
... ... @@ -124,13 +126,11 @@ func AccessToken(request *protocol.AccessTokenRequest) (rsp *protocol.AccessToke
rsp.RefreshToken, _ = utils.GenerateTokenWithClaim(userClaims, protocol.RefreshTokenExipre*time.Second)
rsp.ExpiresIn = protocol.TokenExpire
//auth := userAuth.NewRedisUserAuth(userAuth.WithUserId(claim.UserId),
// userAuth.WithAccessToken(rsp.AccessToken),
// userAuth.WithRefreshToken(rsp.RefreshToken))
//if err = auth.AddAuth(); err != nil {
// log.Error(err)
// return
//}
eda.Publish(&event.AccessTokenEvent{
UserPhone: claim.Phone,
AccessToken: rsp.AccessToken,
RefreshToken: rsp.RefreshToken,
})
return
}
... ... @@ -192,12 +192,6 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT
return
}
//oldAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(claim.UserId))
//if err = oldAuth.Check(userAuth.NewOptions(userAuth.WithRefreshToken(request.RefreshToken))); err != nil {
// log.Error(err)
// err = protocol.NewErrWithMessage(4140, err)
// return
//}
userClaim := utils.UserTokenClaims{
UserId: userId,
Phone: claim.Phone,
... ... @@ -208,13 +202,19 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT
rsp.RefreshToken, _ = utils.GenerateTokenWithClaim(userClaim, protocol.RefreshTokenExipre*time.Second)
rsp.ExpiresIn = protocol.TokenExpire
//newAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(claim.UserId),
// userAuth.WithAccessToken(rsp.AccessToken),
// userAuth.WithRefreshToken(rsp.RefreshToken))
//if err = newAuth.AddAuth(); err != nil {
// log.Error(err)
// return
//}
if err = eda.Publish(&event.RefreshTokenEvent{
UserPhone: claim.Phone,
AccessToken: rsp.AccessToken,
RefreshToken: rsp.RefreshToken,
OldRefreshToken: request.RefreshToken,
OldAccessToken: "",
}); err != nil {
log.Error(err)
rsp.RefreshToken = ""
rsp.AccessToken = ""
err = protocol.NewErrWithMessage(4140, err)
return
}
err = transactionContext.CommitTransaction()
return
}
... ...
package event
import (
"github.com/tiptok/gocomm/xa/eda"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/event/subscriber"
devent "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain/event"
)
func InitEventCenter() {
eda.RegisterSubscribe(&devent.AccessTokenEvent{}, &subscriber.AuthCacheSubscribe{})
eda.RegisterSubscribe(&devent.RefreshTokenEvent{}, &subscriber.AuthCacheSubscribe{})
}
... ...
package subscriber
import (
"github.com/tiptok/gocomm/xa/eda"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/userAuth"
devent "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain/event"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"strconv"
)
type AuthCacheSubscribe struct {
}
func (s *AuthCacheSubscribe) HandleEvent(event eda.Event) error {
switch event.EventType() {
case devent.ACCESS_TOKEN_EVENT:
e, _ := event.(*devent.AccessTokenEvent)
return s.addAuth(e.UserPhone, e.AccessToken, e.RefreshToken)
case devent.REFRESH_TOKEN_EVENT:
e, _ := event.(*devent.RefreshTokenEvent)
if err := s.removeAuth(e.UserPhone, e.OldAccessToken, e.OldRefreshToken); err != nil {
return err
}
return s.addAuth(e.UserPhone, e.AccessToken, e.RefreshToken)
default:
break
}
return nil
}
func (s *AuthCacheSubscribe) addAuth(phone, accessToken, refreshToken string) error {
userPhone, _ := strconv.Atoi(phone)
auth := userAuth.NewRedisUserAuth(userAuth.WithUserId(int64(userPhone)),
userAuth.WithAccessToken(accessToken),
userAuth.WithRefreshToken(refreshToken))
if err := auth.AddAuth(); err != nil {
log.Error(err)
return err
}
return nil
}
func (s *AuthCacheSubscribe) removeAuth(phone, accessToken, refreshToken string) error {
userPhone, _ := strconv.Atoi(phone)
oldAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(int64(userPhone)))
if err := oldAuth.Check(
userAuth.NewOptions(
userAuth.WithRefreshToken(refreshToken),
userAuth.WithAccessToken(accessToken),
)); err != nil {
log.Error(err)
return err
}
return nil
}
... ...
package event
const ACCESS_TOKEN_EVENT = "access_token_event"
type AccessTokenEvent struct {
UserPhone string
AccessToken string
RefreshToken string
}
func (event *AccessTokenEvent) EventType() string {
return ACCESS_TOKEN_EVENT
}
... ...
package event
const REFRESH_TOKEN_EVENT = "refresh_token_event"
type RefreshTokenEvent struct {
UserPhone string
AccessToken string
RefreshToken string
OldAccessToken string
OldRefreshToken string
}
func (event *RefreshTokenEvent) EventType() string {
return REFRESH_TOKEN_EVENT
}
... ...
... ... @@ -204,7 +204,7 @@ func (svr *PgLoginService) GetCompanyList(funcGetCompanyIds func() []int64) []*d
if len(companies) == 0 {
return companyList
}
if _, v, e := CompanyRepository.Find(map[string]interface{}{"inCompanyIds": companies, "status": 1, "sortByCreateTime": domain.ASC}); e != nil {
if _, v, e := CompanyRepository.Find(map[string]interface{}{"inCompanyIds": companies, "status": 1, "sortByCreateTime": domain.DESC}); e != nil {
log.Error(e)
return companyList
} else {
... ...
... ... @@ -12,6 +12,6 @@ func init() {
beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequstLogFilter(Logger))
beego.InsertFilter("/*", beego.AfterExec, middleware.CreateResponseLogFilter(Logger), false)
beego.InsertFilter("/v1", beego.BeforeExec, middleware.CheckJWTToken)
beego.InsertFilter("/v2", beego.BeforeExec, middleware.CheckJWTToken)
beego.InsertFilter("/v1/*", beego.BeforeExec, middleware.CheckJWTToken)
beego.InsertFilter("/v2/*", beego.BeforeExec, middleware.CheckJWTToken)
}
... ...
... ... @@ -2,8 +2,10 @@ package middleware
import (
"github.com/astaxie/beego/context"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/userAuth"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
"strconv"
"strings"
)
... ... @@ -33,15 +35,16 @@ func CheckJWTToken(ctx *context.Context) {
} else {
ctx.Input.SetData("UserId", u.UserId)
//valid token
//tokenAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(u.UserId))
//err := tokenAuth.Check(
// userAuth.NewOptions(userAuth.WithAccessToken(token)),
//)
//if err != nil {
// msg = protocol.NewMesage(4141)
// return
//}
// 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
}
... ...