Skip to content

Commit

Permalink
fix: 修复了Token过期而没返回401的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx committed Mar 29, 2024
1 parent 5f47eab commit ec2a125
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 118 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

* 修复获取通知信息缺少信息的BUG

* 修复了Token过期而没返回401的BUG

### ✨ Features | 新功能

* 推荐用户添加是否关注的字段
Expand Down
18 changes: 6 additions & 12 deletions biz/adaptor/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"github.com/CloudStriver/cloudmind-core-api/biz/infrastructure/config"
"github.com/CloudStriver/go-pkg/utils/pconvertor"
"github.com/CloudStriver/go-pkg/utils/util"
"github.com/CloudStriver/go-pkg/utils/util/log"
"github.com/CloudStriver/service-idl-gen-go/kitex_gen/basic"
Expand All @@ -28,24 +29,17 @@ func ExtractContext(ctx context.Context) (*app.RequestContext, error) {
return c, nil
}

func ExtractMeta(ctx context.Context) (*basic.UserMeta, *basic.Extra) {
return ExtractUserMeta(ctx), ExtractExtra(ctx)
}

func ExtractUserMeta(ctx context.Context) (user *basic.UserMeta) {
func ExtractUserMeta(ctx context.Context) (user *basic.UserMeta, err error) {
user = new(basic.UserMeta)
var err error
defer func() {
if err != nil {
log.CtxInfo(ctx, "extract user meta fail, err=%v", err)
}
}()
c, err := ExtractContext(ctx)
if err != nil {
return
}
tokenString := c.GetHeader("Authorization")
token, err := jwt.Parse(string(tokenString), func(_ *jwt.Token) (interface{}, error) {
if pconvertor.Bytes2String(tokenString) == "" {
return
}
token, err := jwt.Parse(pconvertor.Bytes2String(tokenString), func(_ *jwt.Token) (interface{}, error) {
return jwt.ParseECPublicKeyFromPEM([]byte(config.GetConfig().Auth.PublicKey))
})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions biz/application/service/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ func (s *AuthService) SetPasswordByEmail(ctx context.Context, req *core_api.SetP

func (s *AuthService) SetPasswordByPassword(ctx context.Context, req *core_api.SetPasswordByPasswordReq) (resp *core_api.SetPasswordByPasswordResp, err error) {
resp = new(core_api.SetPasswordByPasswordResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
if _, err = s.CloudMindSts.SetPassword(ctx, &sts.SetPasswordReq{
Expand All @@ -378,8 +378,8 @@ func (s *AuthService) SetPasswordByPassword(ctx context.Context, req *core_api.S

func (s *AuthService) AskUploadAvatar(ctx context.Context, req *core_api.AskUploadAvatarReq) (resp *core_api.AskUploadAvatarResp, err error) {
resp = new(core_api.AskUploadAvatarResp)
user := adaptor.ExtractUserMeta(ctx)
if user.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
genCosStsResp, err := s.CloudMindSts.GenCosSts(ctx, &sts.GenCosStsReq{
Expand Down
38 changes: 22 additions & 16 deletions biz/application/service/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ type CommentService struct {

func (s *CommentService) GetComment(ctx context.Context, req *core_api.GetCommentReq) (resp *core_api.GetCommentResp, err error) {
resp = new(core_api.GetCommentResp)
userData := adaptor.ExtractUserMeta(ctx)
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil {
return resp, consts.ErrNotAuthentication
}
var res *comment.GetCommentResp
if res, err = s.PlatformComment.GetComment(ctx, &comment.GetCommentReq{CommentId: req.CommentId}); err != nil {
return resp, err
Expand All @@ -68,7 +71,10 @@ func (s *CommentService) GetComment(ctx context.Context, req *core_api.GetCommen

func (s *CommentService) GetComments(ctx context.Context, req *core_api.GetCommentsReq) (resp *core_api.GetCommentsResp, err error) {
resp = new(core_api.GetCommentsResp)
userData := adaptor.ExtractUserMeta(ctx)
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil {
return resp, consts.ErrNotAuthentication
}
var res *comment.GetCommentListResp
p := convertor.MakePaginationOptions(req.Limit, req.Offset, req.LastToken, req.Backward)
if res, err = s.PlatformComment.GetCommentList(ctx, &comment.GetCommentListReq{FilterOptions: &comment.CommentFilterOptions{OnlyUserId: req.OnlyUserId, OnlyAtUserId: req.OnlyAtUserId, OnlyCommentId: req.OnlyCommentId, OnlySubjectId: req.OnlySubjectId, OnlyRootId: req.OnlyRootId, OnlyFatherId: req.OnlyFatherId, OnlyState: req.OnlyState, OnlyAttrs: req.OnlyAttrs}, Pagination: p}); err != nil {
Expand Down Expand Up @@ -102,8 +108,8 @@ func (s *CommentService) GetComments(ctx context.Context, req *core_api.GetComme

func (s *CommentService) CreateComment(ctx context.Context, req *core_api.CreateCommentReq) (resp *core_api.CreateCommentResp, err error) {
resp = new(core_api.CreateCommentResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
var res *comment.CreateCommentResp
Expand All @@ -120,8 +126,8 @@ func (s *CommentService) CreateComment(ctx context.Context, req *core_api.Create

func (s *CommentService) UpdateComment(ctx context.Context, req *core_api.UpdateCommentReq) (resp *core_api.UpdateCommentResp, err error) {
resp = new(core_api.UpdateCommentResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand All @@ -139,8 +145,8 @@ func (s *CommentService) UpdateComment(ctx context.Context, req *core_api.Update

func (s *CommentService) DeleteComment(ctx context.Context, req *core_api.DeleteCommentReq) (resp *core_api.DeleteCommentResp, err error) {
resp = new(core_api.DeleteCommentResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
var ok bool
Expand Down Expand Up @@ -168,8 +174,8 @@ func (s *CommentService) DeleteComment(ctx context.Context, req *core_api.Delete

func (s *CommentService) SetCommentAttrs(ctx context.Context, req *core_api.SetCommentAttrsReq) (resp *core_api.SetCommentAttrsResp, err error) {
resp = new(core_api.SetCommentAttrsResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
var res *comment.GetCommentResp
Expand All @@ -186,8 +192,8 @@ func (s *CommentService) SetCommentAttrs(ctx context.Context, req *core_api.SetC

func (s *CommentService) GetCommentSubject(ctx context.Context, req *core_api.GetCommentSubjectReq) (resp *core_api.GetCommentSubjectResp, err error) {
resp = new(core_api.GetCommentSubjectResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
var res *comment.GetCommentSubjectResp
Expand All @@ -200,8 +206,8 @@ func (s *CommentService) GetCommentSubject(ctx context.Context, req *core_api.Ge

func (s *CommentService) UpdateCommentSubject(ctx context.Context, req *core_api.UpdateCommentSubjectReq) (resp *core_api.UpdateCommentSubjectResp, err error) {
resp = new(core_api.UpdateCommentSubjectResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
var res *comment.GetCommentSubjectResp
Expand All @@ -219,8 +225,8 @@ func (s *CommentService) UpdateCommentSubject(ctx context.Context, req *core_api

func (s *CommentService) DeleteCommentSubject(ctx context.Context, req *core_api.DeleteCommentSubjectReq) (resp *core_api.DeleteCommentSubjectResp, err error) {
resp = new(core_api.DeleteCommentSubjectResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
var res *comment.GetCommentSubjectResp
Expand Down
77 changes: 40 additions & 37 deletions biz/application/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ type FileService struct {

func (s *FileService) AskDownloadFile(ctx context.Context, req *core_api.AskDownloadFileReq) (resp *core_api.AskDownloadFileResp, err error) {
resp = new(core_api.AskDownloadFileResp)
user := adaptor.ExtractUserMeta(ctx)
if user.GetUserId() == "" {
user, err := adaptor.ExtractUserMeta(ctx)
if err != nil || user.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand Down Expand Up @@ -96,8 +96,8 @@ func (s *FileService) AskDownloadFile(ctx context.Context, req *core_api.AskDown

func (s *FileService) AskUploadFile(ctx context.Context, req *core_api.AskUploadFileReq) (resp *core_api.AskUploadFileResp, err error) {
resp = new(core_api.AskUploadFileResp)
user := adaptor.ExtractUserMeta(ctx)
if user.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
//userId := user.GetUserId()
Expand Down Expand Up @@ -135,8 +135,8 @@ func (s *FileService) AskUploadFile(ctx context.Context, req *core_api.AskUpload

func (s *FileService) GetPrivateFile(ctx context.Context, req *core_api.GetPrivateFileReq) (resp *core_api.GetPrivateFileResp, err error) {
resp = new(core_api.GetPrivateFileResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand All @@ -156,8 +156,8 @@ func (s *FileService) GetPrivateFile(ctx context.Context, req *core_api.GetPriva

func (s *FileService) GetPublicFile(ctx context.Context, req *core_api.GetPublicFileReq) (resp *core_api.GetPublicFileResp, err error) {
resp = new(core_api.GetPublicFileResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand Down Expand Up @@ -201,8 +201,8 @@ func (s *FileService) GetPublicFile(ctx context.Context, req *core_api.GetPublic

func (s *FileService) GetPrivateFiles(ctx context.Context, req *core_api.GetPrivateFilesReq) (resp *core_api.GetPrivateFilesResp, err error) {
resp = new(core_api.GetPrivateFilesResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
sort := lo.ToPtr(content.SortOptions_SortOptions_createAtDesc)
Expand Down Expand Up @@ -243,7 +243,10 @@ func (s *FileService) GetPrivateFiles(ctx context.Context, req *core_api.GetPriv

func (s *FileService) GetPublicFiles(ctx context.Context, req *core_api.GetPublicFilesReq) (resp *core_api.GetPublicFilesResp, err error) {
resp = new(core_api.GetPublicFilesResp)
userData := adaptor.ExtractUserMeta(ctx)
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil {
return resp, consts.ErrNotAuthentication
}
var res *content.GetFileListResp
var searchOptions *content.SearchOptions

Expand Down Expand Up @@ -304,8 +307,8 @@ func (s *FileService) GetPublicFiles(ctx context.Context, req *core_api.GetPubli

func (s *FileService) GetRecycleBinFiles(ctx context.Context, req *core_api.GetRecycleBinFilesReq) (resp *core_api.GetRecycleBinFilesResp, err error) {
resp = new(core_api.GetRecycleBinFilesResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand Down Expand Up @@ -350,8 +353,8 @@ func (s *FileService) GetFileBySharingCode(ctx context.Context, req *core_api.Ge

func (s *FileService) CreateFile(ctx context.Context, req *core_api.CreateFileReq) (resp *core_api.CreateFileResp, err error) {
resp = new(core_api.CreateFileResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
var res *content.CreateFileResp
Expand Down Expand Up @@ -392,8 +395,8 @@ func (s *FileService) CreateFile(ctx context.Context, req *core_api.CreateFileRe

func (s *FileService) UpdateFile(ctx context.Context, req *core_api.UpdateFileReq) (resp *core_api.UpdateFileResp, err error) {
resp = new(core_api.UpdateFileResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
file := &content.File{
Expand All @@ -409,8 +412,8 @@ func (s *FileService) UpdateFile(ctx context.Context, req *core_api.UpdateFileRe

func (s *FileService) MoveFile(ctx context.Context, req *core_api.MoveFileReq) (resp *core_api.MoveFileResp, err error) {
resp = new(core_api.MoveFileResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand Down Expand Up @@ -454,8 +457,8 @@ func (s *FileService) MoveFile(ctx context.Context, req *core_api.MoveFileReq) (

func (s *FileService) DeleteFile(ctx context.Context, req *core_api.DeleteFileReq) (resp *core_api.DeleteFileResp, err error) {
resp = new(core_api.DeleteFileResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
var res *content.GetFilesByIdsResp
Expand Down Expand Up @@ -497,8 +500,8 @@ func (s *FileService) DeleteFile(ctx context.Context, req *core_api.DeleteFileRe

func (s *FileService) EmptyRecycleBin(ctx context.Context, req *core_api.EmptyRecycleBinReq) (resp *core_api.EmptyRecycleBinResp, err error) {
resp = new(core_api.EmptyRecycleBinResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand All @@ -511,8 +514,8 @@ func (s *FileService) EmptyRecycleBin(ctx context.Context, req *core_api.EmptyRe

func (s *FileService) CompletelyRemoveFile(ctx context.Context, req *core_api.CompletelyRemoveFileReq) (resp *core_api.CompletelyRemoveFileResp, err error) {
resp = new(core_api.CompletelyRemoveFileResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
var res *content.GetFileResp
Expand All @@ -530,8 +533,8 @@ func (s *FileService) CompletelyRemoveFile(ctx context.Context, req *core_api.Co

func (s *FileService) GetShareList(ctx context.Context, req *core_api.GetShareListReq) (resp *core_api.GetShareListResp, err error) {
resp = new(core_api.GetShareListResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand All @@ -551,8 +554,8 @@ func (s *FileService) GetShareList(ctx context.Context, req *core_api.GetShareLi

func (s *FileService) CreateShareCode(ctx context.Context, req *core_api.CreateShareCodeReq) (resp *core_api.CreateShareCodeResp, err error) {
resp = new(core_api.CreateShareCodeResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand Down Expand Up @@ -591,8 +594,8 @@ func (s *FileService) CreateShareCode(ctx context.Context, req *core_api.CreateS

func (s *FileService) DeleteShareCode(ctx context.Context, req *core_api.DeleteShareCodeReq) (resp *core_api.DeleteShareCodeResp, err error) {
resp = new(core_api.DeleteShareCodeResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
if _, err = s.CloudMindContent.DeleteShareCode(ctx, &content.DeleteShareCodeReq{Code: req.OnlyCode}); err != nil {
Expand Down Expand Up @@ -623,8 +626,8 @@ func (s *FileService) ParsingShareCode(ctx context.Context, req *core_api.Parsin

func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *core_api.SaveFileToPrivateSpaceReq) (resp *core_api.SaveFileToPrivateSpaceResp, err error) {
resp = new(core_api.SaveFileToPrivateSpaceResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand Down Expand Up @@ -692,8 +695,8 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *core_api.

func (s *FileService) AddFileToPublicSpace(ctx context.Context, req *core_api.AddFileToPublicSpaceReq) (resp *core_api.AddFileToPublicSpaceResp, err error) {
resp = new(core_api.AddFileToPublicSpaceResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}

Expand Down Expand Up @@ -749,8 +752,8 @@ func (s *FileService) AddFileToPublicSpace(ctx context.Context, req *core_api.Ad

func (s *FileService) RecoverRecycleBinFile(ctx context.Context, req *core_api.RecoverRecycleBinFileReq) (resp *core_api.RecoverRecycleBinFileResp, err error) {
resp = new(core_api.RecoverRecycleBinFileResp)
userData := adaptor.ExtractUserMeta(ctx)
if userData.GetUserId() == "" {
userData, err := adaptor.ExtractUserMeta(ctx)
if err != nil || userData.GetUserId() == "" {
return resp, consts.ErrNotAuthentication
}
var res *content.GetFilesByIdsResp
Expand Down
Loading

0 comments on commit ec2a125

Please sign in to comment.