正在显示
9 个修改的文件
包含
136 行增加
和
32 行删除
@@ -2,6 +2,7 @@ package main | @@ -2,6 +2,7 @@ package main | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "github.com/astaxie/beego" | 4 | "github.com/astaxie/beego" |
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/event" | ||
5 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant" | 6 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant" |
6 | _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant" | 7 | _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant" |
7 | _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg" | 8 | _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg" |
@@ -18,5 +19,9 @@ func main() { | @@ -18,5 +19,9 @@ func main() { | ||
18 | log.Info("app start!") | 19 | log.Info("app start!") |
19 | log.Info(constant.POSTGRESQL_DB_NAME) | 20 | log.Info(constant.POSTGRESQL_DB_NAME) |
20 | //ginsvr.Run() | 21 | //ginsvr.Run() |
22 | + | ||
23 | + //注册事件 | ||
24 | + event.InitEventCenter() | ||
25 | + | ||
21 | beego.Run() | 26 | beego.Run() |
22 | } | 27 | } |
@@ -2,8 +2,10 @@ package auth | @@ -2,8 +2,10 @@ package auth | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | + "github.com/tiptok/gocomm/xa/eda" | ||
5 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory" | 6 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory" |
6 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" | 7 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" |
8 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain/event" | ||
7 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/domain_service" | 9 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/domain_service" |
8 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" | 10 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" |
9 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log" | 11 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log" |
@@ -124,13 +126,11 @@ func AccessToken(request *protocol.AccessTokenRequest) (rsp *protocol.AccessToke | @@ -124,13 +126,11 @@ func AccessToken(request *protocol.AccessTokenRequest) (rsp *protocol.AccessToke | ||
124 | rsp.RefreshToken, _ = utils.GenerateTokenWithClaim(userClaims, protocol.RefreshTokenExipre*time.Second) | 126 | rsp.RefreshToken, _ = utils.GenerateTokenWithClaim(userClaims, protocol.RefreshTokenExipre*time.Second) |
125 | rsp.ExpiresIn = protocol.TokenExpire | 127 | rsp.ExpiresIn = protocol.TokenExpire |
126 | 128 | ||
127 | - //auth := userAuth.NewRedisUserAuth(userAuth.WithUserId(claim.UserId), | ||
128 | - // userAuth.WithAccessToken(rsp.AccessToken), | ||
129 | - // userAuth.WithRefreshToken(rsp.RefreshToken)) | ||
130 | - //if err = auth.AddAuth(); err != nil { | ||
131 | - // log.Error(err) | ||
132 | - // return | ||
133 | - //} | 129 | + eda.Publish(&event.AccessTokenEvent{ |
130 | + UserPhone: claim.Phone, | ||
131 | + AccessToken: rsp.AccessToken, | ||
132 | + RefreshToken: rsp.RefreshToken, | ||
133 | + }) | ||
134 | return | 134 | return |
135 | } | 135 | } |
136 | 136 | ||
@@ -192,12 +192,6 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT | @@ -192,12 +192,6 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT | ||
192 | return | 192 | return |
193 | } | 193 | } |
194 | 194 | ||
195 | - //oldAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(claim.UserId)) | ||
196 | - //if err = oldAuth.Check(userAuth.NewOptions(userAuth.WithRefreshToken(request.RefreshToken))); err != nil { | ||
197 | - // log.Error(err) | ||
198 | - // err = protocol.NewErrWithMessage(4140, err) | ||
199 | - // return | ||
200 | - //} | ||
201 | userClaim := utils.UserTokenClaims{ | 195 | userClaim := utils.UserTokenClaims{ |
202 | UserId: userId, | 196 | UserId: userId, |
203 | Phone: claim.Phone, | 197 | Phone: claim.Phone, |
@@ -208,13 +202,19 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT | @@ -208,13 +202,19 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT | ||
208 | rsp.RefreshToken, _ = utils.GenerateTokenWithClaim(userClaim, protocol.RefreshTokenExipre*time.Second) | 202 | rsp.RefreshToken, _ = utils.GenerateTokenWithClaim(userClaim, protocol.RefreshTokenExipre*time.Second) |
209 | rsp.ExpiresIn = protocol.TokenExpire | 203 | rsp.ExpiresIn = protocol.TokenExpire |
210 | 204 | ||
211 | - //newAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(claim.UserId), | ||
212 | - // userAuth.WithAccessToken(rsp.AccessToken), | ||
213 | - // userAuth.WithRefreshToken(rsp.RefreshToken)) | ||
214 | - //if err = newAuth.AddAuth(); err != nil { | ||
215 | - // log.Error(err) | ||
216 | - // return | ||
217 | - //} | 205 | + if err = eda.Publish(&event.RefreshTokenEvent{ |
206 | + UserPhone: claim.Phone, | ||
207 | + AccessToken: rsp.AccessToken, | ||
208 | + RefreshToken: rsp.RefreshToken, | ||
209 | + OldRefreshToken: request.RefreshToken, | ||
210 | + OldAccessToken: "", | ||
211 | + }); err != nil { | ||
212 | + log.Error(err) | ||
213 | + rsp.RefreshToken = "" | ||
214 | + rsp.AccessToken = "" | ||
215 | + err = protocol.NewErrWithMessage(4140, err) | ||
216 | + return | ||
217 | + } | ||
218 | err = transactionContext.CommitTransaction() | 218 | err = transactionContext.CommitTransaction() |
219 | return | 219 | return |
220 | } | 220 | } |
pkg/application/event/event.go
0 → 100644
1 | +package event | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/tiptok/gocomm/xa/eda" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/event/subscriber" | ||
6 | + devent "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain/event" | ||
7 | +) | ||
8 | + | ||
9 | +func InitEventCenter() { | ||
10 | + eda.RegisterSubscribe(&devent.AccessTokenEvent{}, &subscriber.AuthCacheSubscribe{}) | ||
11 | + eda.RegisterSubscribe(&devent.RefreshTokenEvent{}, &subscriber.AuthCacheSubscribe{}) | ||
12 | +} |
1 | +package subscriber | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/tiptok/gocomm/xa/eda" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/userAuth" | ||
6 | + devent "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain/event" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log" | ||
8 | + "strconv" | ||
9 | +) | ||
10 | + | ||
11 | +type AuthCacheSubscribe struct { | ||
12 | +} | ||
13 | + | ||
14 | +func (s *AuthCacheSubscribe) HandleEvent(event eda.Event) error { | ||
15 | + switch event.EventType() { | ||
16 | + case devent.ACCESS_TOKEN_EVENT: | ||
17 | + e, _ := event.(*devent.AccessTokenEvent) | ||
18 | + return s.addAuth(e.UserPhone, e.AccessToken, e.RefreshToken) | ||
19 | + case devent.REFRESH_TOKEN_EVENT: | ||
20 | + e, _ := event.(*devent.RefreshTokenEvent) | ||
21 | + if err := s.removeAuth(e.UserPhone, e.OldAccessToken, e.OldRefreshToken); err != nil { | ||
22 | + return err | ||
23 | + } | ||
24 | + return s.addAuth(e.UserPhone, e.AccessToken, e.RefreshToken) | ||
25 | + default: | ||
26 | + break | ||
27 | + } | ||
28 | + return nil | ||
29 | +} | ||
30 | + | ||
31 | +func (s *AuthCacheSubscribe) addAuth(phone, accessToken, refreshToken string) error { | ||
32 | + userPhone, _ := strconv.Atoi(phone) | ||
33 | + auth := userAuth.NewRedisUserAuth(userAuth.WithUserId(int64(userPhone)), | ||
34 | + userAuth.WithAccessToken(accessToken), | ||
35 | + userAuth.WithRefreshToken(refreshToken)) | ||
36 | + if err := auth.AddAuth(); err != nil { | ||
37 | + log.Error(err) | ||
38 | + return err | ||
39 | + } | ||
40 | + return nil | ||
41 | +} | ||
42 | + | ||
43 | +func (s *AuthCacheSubscribe) removeAuth(phone, accessToken, refreshToken string) error { | ||
44 | + userPhone, _ := strconv.Atoi(phone) | ||
45 | + oldAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(int64(userPhone))) | ||
46 | + if err := oldAuth.Check( | ||
47 | + userAuth.NewOptions( | ||
48 | + userAuth.WithRefreshToken(refreshToken), | ||
49 | + userAuth.WithAccessToken(accessToken), | ||
50 | + )); err != nil { | ||
51 | + log.Error(err) | ||
52 | + return err | ||
53 | + } | ||
54 | + return nil | ||
55 | +} |
pkg/domain/event/access_token_event.go
0 → 100644
pkg/domain/event/refresh_token_event.go
0 → 100644
1 | +package event | ||
2 | + | ||
3 | +const REFRESH_TOKEN_EVENT = "refresh_token_event" | ||
4 | + | ||
5 | +type RefreshTokenEvent struct { | ||
6 | + UserPhone string | ||
7 | + AccessToken string | ||
8 | + RefreshToken string | ||
9 | + | ||
10 | + OldAccessToken string | ||
11 | + OldRefreshToken string | ||
12 | +} | ||
13 | + | ||
14 | +func (event *RefreshTokenEvent) EventType() string { | ||
15 | + return REFRESH_TOKEN_EVENT | ||
16 | +} |
@@ -204,7 +204,7 @@ func (svr *PgLoginService) GetCompanyList(funcGetCompanyIds func() []int64) []*d | @@ -204,7 +204,7 @@ func (svr *PgLoginService) GetCompanyList(funcGetCompanyIds func() []int64) []*d | ||
204 | if len(companies) == 0 { | 204 | if len(companies) == 0 { |
205 | return companyList | 205 | return companyList |
206 | } | 206 | } |
207 | - if _, v, e := CompanyRepository.Find(map[string]interface{}{"inCompanyIds": companies, "status": 1, "sortByCreateTime": domain.ASC}); e != nil { | 207 | + if _, v, e := CompanyRepository.Find(map[string]interface{}{"inCompanyIds": companies, "status": 1, "sortByCreateTime": domain.DESC}); e != nil { |
208 | log.Error(e) | 208 | log.Error(e) |
209 | return companyList | 209 | return companyList |
210 | } else { | 210 | } else { |
@@ -12,6 +12,6 @@ func init() { | @@ -12,6 +12,6 @@ func init() { | ||
12 | beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequstLogFilter(Logger)) | 12 | beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequstLogFilter(Logger)) |
13 | beego.InsertFilter("/*", beego.AfterExec, middleware.CreateResponseLogFilter(Logger), false) | 13 | beego.InsertFilter("/*", beego.AfterExec, middleware.CreateResponseLogFilter(Logger), false) |
14 | 14 | ||
15 | - beego.InsertFilter("/v1", beego.BeforeExec, middleware.CheckJWTToken) | ||
16 | - beego.InsertFilter("/v2", beego.BeforeExec, middleware.CheckJWTToken) | 15 | + beego.InsertFilter("/v1/*", beego.BeforeExec, middleware.CheckJWTToken) |
16 | + beego.InsertFilter("/v2/*", beego.BeforeExec, middleware.CheckJWTToken) | ||
17 | } | 17 | } |
@@ -2,8 +2,10 @@ package middleware | @@ -2,8 +2,10 @@ package middleware | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "github.com/astaxie/beego/context" | 4 | "github.com/astaxie/beego/context" |
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/userAuth" | ||
5 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" | 6 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" |
6 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol" | 7 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol" |
8 | + "strconv" | ||
7 | "strings" | 9 | "strings" |
8 | ) | 10 | ) |
9 | 11 | ||
@@ -33,15 +35,16 @@ func CheckJWTToken(ctx *context.Context) { | @@ -33,15 +35,16 @@ func CheckJWTToken(ctx *context.Context) { | ||
33 | } else { | 35 | } else { |
34 | ctx.Input.SetData("UserId", u.UserId) | 36 | ctx.Input.SetData("UserId", u.UserId) |
35 | 37 | ||
36 | - //valid token | ||
37 | - //tokenAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(u.UserId)) | ||
38 | - //err := tokenAuth.Check( | ||
39 | - // userAuth.NewOptions(userAuth.WithAccessToken(token)), | ||
40 | - //) | ||
41 | - //if err != nil { | ||
42 | - // msg = protocol.NewMesage(4141) | ||
43 | - // return | ||
44 | - //} | 38 | + // valid token |
39 | + userPhone, _ := strconv.Atoi(u.Phone) | ||
40 | + tokenAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(int64(userPhone))) | ||
41 | + err := tokenAuth.Check( | ||
42 | + userAuth.NewOptions(userAuth.WithAccessToken(token)), | ||
43 | + ) | ||
44 | + if err != nil { | ||
45 | + msg = protocol.NewMesage(4141) | ||
46 | + return | ||
47 | + } | ||
45 | } | 48 | } |
46 | return | 49 | return |
47 | } | 50 | } |
-
请 注册 或 登录 后发表评论