Skip to content

Commit

Permalink
feat: jwt签名算法改为ECDSA,优化ParallelRun
Browse files Browse the repository at this point in the history
  • Loading branch information
foliet committed Dec 20, 2023
1 parent e16ff62 commit c57c69d
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 67 deletions.
5 changes: 2 additions & 3 deletions biz/adaptor/controller/core_api/system.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion biz/adaptor/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func ExtractUserMeta(ctx context.Context) (user *basic.UserMeta) {
}
tokenString := c.GetHeader("Authorization")
token, err := jwt.Parse(string(tokenString), func(_ *jwt.Token) (interface{}, error) {
return []byte(config.GetConfig().Auth.AccessSecret), nil
return jwt.ParseECPublicKeyFromPEM([]byte(config.GetConfig().Auth.PublicKey))
})
if err != nil {
return
Expand Down
10 changes: 7 additions & 3 deletions biz/application/service/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *AuthService) SignIn(ctx context.Context, req *core_api.SignInReq) (*cor
}

auth := s.Config.Auth
resp.AccessToken, resp.AccessExpire, err = generateJwtToken(req, rpcResp, auth.AccessSecret, auth.AccessExpire)
resp.AccessToken, resp.AccessExpire, err = generateJwtToken(req, rpcResp, auth.SecretKey, auth.AccessExpire)
if err != nil {
log.CtxError(ctx, "[generateJwtToken] fail, err=%v, config=%s, resp=%s", err, util.JSONF(s.Config.Auth), util.JSONF(rpcResp))
return nil, err
Expand All @@ -60,6 +60,10 @@ func (s *AuthService) SignIn(ctx context.Context, req *core_api.SignInReq) (*cor
}

func generateJwtToken(req *core_api.SignInReq, resp *sts.SignInResp, secret string, expire int64) (string, int64, error) {
key, err := jwt.ParseECPrivateKeyFromPEM([]byte(secret))
if err != nil {
return "", 0, err
}
iat := time.Now().Unix()
exp := iat + expire
claims := make(jwt.MapClaims)
Expand All @@ -73,9 +77,9 @@ func generateJwtToken(req *core_api.SignInReq, resp *sts.SignInResp, secret stri
OpenId: resp.GetOpenId(),
UnionId: resp.GetUnionId(),
}
token := jwt.New(jwt.SigningMethodHS256)
token := jwt.New(jwt.SigningMethodES256)
token.Claims = claims
tokenString, err := token.SignedString([]byte(secret))
tokenString, err := token.SignedString(key)
if err != nil {
return "", 0, err
}
Expand Down
7 changes: 3 additions & 4 deletions biz/application/service/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (s *CollectionService) GetImageByCat(ctx context.Context, req *core_api.Get
Url: image.Url,
CatId: image.CatId,
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
if user.GetUserId() == "" {
return
Expand All @@ -264,10 +264,9 @@ func (s *CollectionService) GetImageByCat(ctx context.Context, req *core_api.Get
},
func() {
_ = s.CatImageDomainService.LoadLikeCount(ctx, img)
},
})
})
resp.Images[i] = img
}
}))
})...)
return resp, nil
}
7 changes: 3 additions & 4 deletions biz/application/service/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *CommentService) GetComments(ctx context.Context, req *core_api.GetComme
CreateAt: item.CreateAt,
Text: item.Text,
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
_ = s.CommentDomainService.LoadIsCurrentUserLiked(ctx, c, userMeta.UserId)
},
Expand All @@ -87,11 +87,10 @@ func (s *CommentService) GetComments(ctx context.Context, req *core_api.GetComme
},
func() {
_ = s.CommentDomainService.LoadAuthor(ctx, c, item.AuthorId)
},
})
})
resp.Comments[i] = c
}
}))
})...)

return resp, nil
}
Expand Down
35 changes: 15 additions & 20 deletions biz/application/service/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (s *LikeService) GetLikedUsers(ctx context.Context, req *core_api.GetLikedU
u := &core_api.User{
Id: userId,
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
res, err := s.MeowchatUser.GetUserDetail(ctx, &genlike.GetUserDetailReq{UserId: userId})
if err != nil {
Expand All @@ -188,11 +188,10 @@ func (s *LikeService) GetLikedUsers(ctx context.Context, req *core_api.GetLikedU
return
}
_ = s.UserDomainService.LoadIsFollowing(ctx, u, userMeta.UserId)
},
})
})
resp.Users[i] = u
}
}))
})...)
return resp, nil
}

Expand Down Expand Up @@ -294,7 +293,7 @@ func (s *LikeService) GetUserLikeContents(ctx context.Context, req *core_api.Get
Tags: post.Post.Tags,
IsOfficial: post.Post.IsOfficial,
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
_ = s.PostDomainService.LoadAuthor(ctx, p, post.Post.UserId)
},
Expand All @@ -303,11 +302,10 @@ func (s *LikeService) GetUserLikeContents(ctx context.Context, req *core_api.Get
},
func() {
_ = s.PostDomainService.LoadCommentCount(ctx, p)
},
})
})
resp.Posts[i] = p
}
}))
})...)
} else if req.GetTargetType() == user.LikeType_Moment { //moment
resp.Moments = make([]*core_api.Moment, len(data.Likes))
util.ParallelRun(lo.Map(data.Likes, func(like *genlike.Like, i int) func() {
Expand All @@ -324,7 +322,7 @@ func (s *LikeService) GetUserLikeContents(ctx context.Context, req *core_api.Get
Text: moment.Moment.Text,
CommunityId: moment.Moment.CommunityId,
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
if moment.Moment.GetCatId() != "" {
_ = s.MomentDomainService.LoadCats(ctx, m, []string{moment.Moment.GetCatId()})
Expand All @@ -338,11 +336,10 @@ func (s *LikeService) GetUserLikeContents(ctx context.Context, req *core_api.Get
},
func() {
_ = s.MomentDomainService.LoadCommentCount(ctx, m)
},
})
})
resp.Moments[i] = m
}
}))
})...)
} else if req.GetTargetType() == user.LikeType_Comment { //comment
resp.Comments = make([]*core_api.Comment, len(data.Likes))
util.ParallelRun(lo.Map(data.Likes, func(like *genlike.Like, i int) func() {
Expand All @@ -356,7 +353,7 @@ func (s *LikeService) GetUserLikeContents(ctx context.Context, req *core_api.Get
CreateAt: _comment.Comment.CreateAt,
Text: _comment.Comment.Text,
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
if _comment.Comment.ReplyTo == "" {
return
Expand All @@ -371,19 +368,18 @@ func (s *LikeService) GetUserLikeContents(ctx context.Context, req *core_api.Get
},
func() {
_ = s.CommentDomainService.LoadAuthor(ctx, c, _comment.Comment.AuthorId)
},
})
})
resp.Comments[i] = c
}
}))
})...)
} else if req.GetTargetType() == user.LikeType_User { //user
resp.Users = make([]*core_api.User, len(data.Likes))
util.ParallelRun(lo.Map(data.Likes, func(like *genlike.Like, i int) func() {
return func() {
u := &core_api.User{
Id: like.GetTargetId(),
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
rpcResp, err := s.MeowchatUser.GetUserDetail(ctx, &genlike.GetUserDetailReq{UserId: like.TargetId})
if err != nil {
Expand All @@ -397,11 +393,10 @@ func (s *LikeService) GetUserLikeContents(ctx context.Context, req *core_api.Get
return
}
_ = s.UserDomainService.LoadIsFollowing(ctx, u, userMeta.GetUserId())
},
})
})
resp.Users[i] = u
}
}))
})...)
}
resp.Total = data.GetTotal()
resp.Token = data.GetToken()
Expand Down
12 changes: 5 additions & 7 deletions biz/application/service/moment.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *MomentService) GetMomentDetail(ctx context.Context, req *core_api.GetMo
return nil, err
}

util.ParallelRun([]func(){
util.ParallelRun(
func() {
if data.Moment.GetCatId() != "" {
_ = s.MomentDomainService.LoadCats(ctx, resp.Moment, []string{data.Moment.GetCatId()})
Expand All @@ -93,8 +93,7 @@ func (s *MomentService) GetMomentDetail(ctx context.Context, req *core_api.GetMo
},
func() {
_ = s.MomentDomainService.LoadIsCurrentUserLiked(ctx, resp.Moment, userMeta.UserId)
},
})
})

return resp, nil
}
Expand Down Expand Up @@ -145,7 +144,7 @@ func (s *MomentService) GetMomentPreviews(ctx context.Context, req *core_api.Get
util.ParallelRun(lo.Map(data.Moments, func(moment *content.Moment, i int) func() {
return func() {
// 并发获取用户信息、点赞数、评论数
util.ParallelRun([]func(){
util.ParallelRun(
func() {
_ = s.MomentDomainService.LoadAuthor(ctx, resp.Moments[i], moment.UserId)
},
Expand All @@ -159,10 +158,9 @@ func (s *MomentService) GetMomentPreviews(ctx context.Context, req *core_api.Get
if moment.GetCatId() != "" {
_ = s.MomentDomainService.LoadCats(ctx, resp.Moments[i], []string{moment.GetCatId()})
}
},
})
})
}
}))
})...)
return resp, nil
}

Expand Down
12 changes: 5 additions & 7 deletions biz/application/service/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (s *PlanService) GetPlanDetail(ctx context.Context, req *core_api.GetPlanDe
if err != nil {
return nil, err
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
if data.GetPlan().GetCatId() == "" {
return
Expand All @@ -261,8 +261,7 @@ func (s *PlanService) GetPlanDetail(ctx context.Context, req *core_api.GetPlanDe
Nickname: user.User.Nickname,
AvatarUrl: user.User.AvatarUrl,
}
},
})
})
return resp, nil
}

Expand Down Expand Up @@ -311,7 +310,7 @@ func (s *PlanService) GetPlanPreviews(ctx context.Context, req *core_api.GetPlan

util.ParallelRun(lo.Map(data.Plans, func(plan *content.Plan, i int) func() {
return func() {
util.ParallelRun([]func(){
util.ParallelRun(
func() {
user, err := s.User.GetUserDetail(ctx, &genuser.GetUserDetailReq{UserId: plan.InitiatorId})
if err != nil {
Expand All @@ -336,10 +335,9 @@ func (s *PlanService) GetPlanPreviews(ctx context.Context, req *core_api.GetPlan
if err == nil {
resp.Plans[i].Cat = c
}
},
})
})
}
}))
})...)
return resp, nil
}

Expand Down
12 changes: 5 additions & 7 deletions biz/application/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *PostService) GetPostDetail(ctx context.Context, req *core_api.GetPostDe
Tags: data.Post.Tags,
IsOfficial: data.Post.IsOfficial,
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
_ = s.PostDomainService.LoadAuthor(ctx, p, data.Post.UserId)
},
Expand All @@ -88,8 +88,7 @@ func (s *PostService) GetPostDetail(ctx context.Context, req *core_api.GetPostDe
},
func() {
_ = s.PostDomainService.LoadIsCurrentUserLiked(ctx, p, userMeta.UserId)
},
})
})
resp.Post = p
return resp, nil
}
Expand All @@ -115,7 +114,7 @@ func (s *PostService) GetPostPreviews(ctx context.Context, req *core_api.GetPost
Tags: data.Posts[i].Tags,
IsOfficial: data.Posts[i].IsOfficial,
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
_ = s.PostDomainService.LoadAuthor(ctx, p, data.Posts[i].UserId)
},
Expand All @@ -124,11 +123,10 @@ func (s *PostService) GetPostPreviews(ctx context.Context, req *core_api.GetPost
},
func() {
_ = s.PostDomainService.LoadCommentCount(ctx, p)
},
})
})
resp.Posts[i] = p
}
}))
})...)

return resp, nil
}
Expand Down
2 changes: 1 addition & 1 deletion biz/application/service/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (s *SystemService) ListNotification(ctx context.Context, req *core_api.List
}
}
}
}))
})...)

return resp, nil
}
Expand Down
7 changes: 3 additions & 4 deletions biz/application/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *UserService) GetUserInfo(ctx context.Context, req *core_api.GetUserInfo
AvatarUrl: data.User.AvatarUrl,
Motto: lo.ToPtr(data.User.Motto),
}
util.ParallelRun([]func(){
util.ParallelRun(
func() {
_ = s.UserService.LoadArticle(ctx, resp.User)
},
Expand All @@ -81,8 +81,7 @@ func (s *UserService) GetUserInfo(ctx context.Context, req *core_api.GetUserInfo
},
func() {
_ = s.UserService.LoadEnableDebug(ctx, resp.User)
},
})
})

return resp, nil
}
Expand Down Expand Up @@ -118,7 +117,7 @@ func (s *UserService) SearchUser(ctx context.Context, req *core_api.SearchUserRe
_ = s.UserService.LoadRoles(ctx, u)
resp.Users[i] = u
}
}))
})...)
return resp, nil
}

Expand Down
2 changes: 1 addition & 1 deletion biz/domain/service/moment.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *MomentDomainService) LoadCats(ctx context.Context, moment *core_api.Mom
}
}
}
}))
})...)
moment.Cats = lo.Reject(cats, func(cat *core_api.CatPreview, _ int) bool {
return cat == nil
})
Expand Down
Loading

0 comments on commit c57c69d

Please sign in to comment.