-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
159 lines (134 loc) · 3.38 KB
/
rpc.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ucs
import (
"context"
"fmt"
"github.com/Macrow/ucs-go-sdk/pkg/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"time"
)
type RpcClient struct {
addr string
timeout int
options []grpc.DialOption
md metadata.MD
}
func (c *RpcClient) SetTimeout(timeout int) Client {
if timeout > 0 {
c.timeout = timeout
}
return c
}
func (c *RpcClient) SetToken(token string) Client {
c.md = metadata.Pairs("authorization", token)
return c
}
func (c *RpcClient) SetHttpHeaderNames(accessCodeHeader, randomKeyHeader string) Client {
return c
}
func (c *RpcClient) ValidateJwt() error {
return authentication(c)
}
func (c *RpcClient) ValidatePermOperationByCode(operationCode string) error {
return authorization(c, &pb.AuthorizationRequest{
Payload: &pb.AuthorizationRequest_OperationCode{
OperationCode: operationCode,
},
})
}
func (c *RpcClient) ValidatePermAction(service, path, method string) error {
return authorization(c, &pb.AuthorizationRequest{
Payload: &pb.AuthorizationRequest_Action{
Action: &pb.Action{
Service: service,
Path: path,
Method: method,
},
},
})
}
func (c *RpcClient) ValidatePermOrgById(orgId string) error {
return authorization(c, &pb.AuthorizationRequest{
Payload: &pb.AuthorizationRequest_OrgId{
OrgId: orgId,
},
})
}
func (c *RpcClient) RenewToken() (string, error) {
if c.options == nil {
return "", fmt.Errorf("please create client instance and set token first")
}
conn, err := grpc.Dial(c.addr, c.options...)
defer func() {
if conn != nil {
_ = conn.Close()
}
}()
if err != nil {
return "", fmt.Errorf(err.Error())
}
service := pb.NewAuthServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(c.timeout))
defer cancel()
ctx = metadata.NewOutgoingContext(ctx, c.md)
res, err := service.RenewToken(ctx, &pb.RenewTokenRequest{})
if err != nil {
return "", fmt.Errorf(err.Error())
}
if !res.Success {
return "", fmt.Errorf(res.Message)
}
return res.Token, nil
}
func authentication(c *RpcClient) error {
if c.options == nil {
return fmt.Errorf("please create client instance and set token first")
}
conn, err := grpc.Dial(c.addr, c.options...)
defer func() {
if conn != nil {
_ = conn.Close()
}
}()
if err != nil {
return fmt.Errorf(err.Error())
}
service := pb.NewAuthServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(c.timeout))
defer cancel()
ctx = metadata.NewOutgoingContext(ctx, c.md)
res, err := service.Authentication(ctx, &pb.AuthenticationRequest{})
if err != nil {
return fmt.Errorf(err.Error())
}
if !res.Success {
return fmt.Errorf(res.Message)
}
return nil
}
func authorization(c *RpcClient, req *pb.AuthorizationRequest) error {
if c.options == nil {
return fmt.Errorf("please create client instance first")
}
conn, err := grpc.Dial(c.addr, c.options...)
defer func() {
if conn != nil {
_ = conn.Close()
}
}()
if err != nil {
return fmt.Errorf(err.Error())
}
service := pb.NewAuthServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(c.timeout))
defer cancel()
ctx = metadata.NewOutgoingContext(ctx, c.md)
res, err := service.Authorization(ctx, req)
if err != nil {
return fmt.Errorf(err.Error())
}
if !res.Success {
return fmt.Errorf(res.Message)
}
return nil
}