-
Notifications
You must be signed in to change notification settings - Fork 0
/
sts.go
79 lines (70 loc) 路 2.57 KB
/
sts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package service
import (
"context"
"net/http"
"github.com/google/uuid"
"github.com/google/wire"
"github.com/xh-polaris/service-idl-gen-go/kitex_gen/platform/sts"
"github.com/xh-polaris/meowchat-core-api/biz/adaptor"
"github.com/xh-polaris/meowchat-core-api/biz/application/dto/meowchat/core_api"
"github.com/xh-polaris/meowchat-core-api/biz/infrastructure/consts"
"github.com/xh-polaris/meowchat-core-api/biz/infrastructure/rpc/platform_sts"
)
type IStsService interface {
ApplySignedUrl(ctx context.Context, req *core_api.ApplySignedUrlReq) (*core_api.ApplySignedUrlResp, error)
ApplySignedUrlAsCommunity(ctx context.Context, req *core_api.ApplySignedUrlAsCommunityReq) (*core_api.ApplySignedUrlAsCommunityResp, error)
}
type StsService struct {
PlatformSts platform_sts.IPlatformSts
}
var StsServiceSet = wire.NewSet(
wire.Struct(new(StsService), "*"),
wire.Bind(new(IStsService), new(*StsService)),
)
func (s *StsService) ApplySignedUrl(ctx context.Context, req *core_api.ApplySignedUrlReq) (*core_api.ApplySignedUrlResp, error) {
user := adaptor.ExtractUserMeta(ctx)
if user.GetUserId() == "" {
return nil, consts.ErrNotAuthentication
}
resp := new(core_api.ApplySignedUrlResp)
userId := user.GetUserId()
data, err := s.PlatformSts.GenCosSts(ctx, &sts.GenCosStsReq{Path: "users/" + userId + "/*"})
if err != nil {
return nil, err
}
resp.SessionToken = data.SessionToken
if req.Prefix != nil {
*req.Prefix += "/"
}
data2, err := s.PlatformSts.GenSignedUrl(ctx, &sts.GenSignedUrlReq{
SecretId: data.SecretId,
SecretKey: data.SecretKey,
Method: http.MethodPut,
Path: "users/" + userId + "/" + req.GetPrefix() + uuid.New().String() + req.GetSuffix(),
})
resp.Url = data2.SignedUrl
return resp, nil
}
func (s *StsService) ApplySignedUrlAsCommunity(ctx context.Context, req *core_api.ApplySignedUrlAsCommunityReq) (*core_api.ApplySignedUrlAsCommunityResp, error) {
user := adaptor.ExtractUserMeta(ctx)
if user.GetUserId() == "" {
return nil, consts.ErrNotAuthentication
}
resp := new(core_api.ApplySignedUrlAsCommunityResp)
data, err := s.PlatformSts.GenCosSts(ctx, &sts.GenCosStsReq{Path: "communities/" + req.CommunityId + "/*"})
if err != nil {
return nil, err
}
resp.SessionToken = data.SessionToken
if req.Prefix != "" {
req.Prefix += "/"
}
data2, err := s.PlatformSts.GenSignedUrl(ctx, &sts.GenSignedUrlReq{
SecretId: data.SecretId,
SecretKey: data.SecretKey,
Method: http.MethodPut,
Path: "communities/" + req.CommunityId + "/" + req.Prefix + uuid.New().String() + req.Suffix,
})
resp.Url = data2.SignedUrl
return resp, nil
}