-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
141 lines (118 loc) · 3.26 KB
/
client.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
package ezpay
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/nats-io/nuid"
log "github.com/sirupsen/logrus"
)
var (
TokenAlgorithm = "HS256"
TokenFields = []string{"exp", "nonce"}
TokenExpiresIn = 30 * 60
BaseURL = "https://revenue.ezboti.com/api/v1/server"
DefaultContentType = "text/plain"
)
var (
ErrFailedResponse = errors.New("failed response")
)
type Client struct {
ProjectID string
ProjectSecret string
PaywallID string
BaseURL string
}
// NewClient creates a new ezpay client
// 每个 Client 实例对应一个项目,一个项目对应一个 paywall。
// 不同的项目可以实例化不同的 Client 进行调用。
func NewClient(cfg *Config) (*Client, error) {
return &Client{
ProjectID: cfg.ProjectID,
ProjectSecret: cfg.ProjectSecret,
PaywallID: cfg.PaywallID,
BaseURL: BaseURL,
}, nil
}
func (c *Client) decodeToken(token string) (jwt.MapClaims, error) {
var claims jwt.MapClaims
_, err := jwt.ParseWithClaims(token, &claims, func(token *jwt.Token) (interface{}, error) {
hmac, ok := token.Method.(*jwt.SigningMethodHMAC)
if !ok {
log.Errorf("unexpected signing method: %v", token.Header["alg"])
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
if hmac != jwt.SigningMethodHS256 {
log.Errorf("unexpected signing method: %v", token.Header["alg"])
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(c.ProjectSecret), nil
})
if err != nil {
log.Errorf("ParseWithClaims Error, err=%+v", err)
return nil, err
}
for _, field := range TokenFields {
if _, ok := claims[field]; !ok {
log.Errorf("token did not contain %s field", field)
return nil, fmt.Errorf("token did not contain %s field", field)
}
}
return claims, nil
}
func (c *Client) encodeToken(payload map[string]interface{}) (string, error) {
claims := jwt.MapClaims{
"exp": time.Now().Unix() + int64(TokenExpiresIn),
"nonce": nuid.Next(),
}
for k, v := range payload {
claims[k] = v
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
token.Header["project_id"] = c.ProjectID
signed, err := token.SignedString([]byte(c.ProjectSecret))
if err != nil {
return "", err
}
return signed, nil
}
func (c *Client) Call(ctx context.Context, api string, params interface{}) (interface{}, error) {
payload := map[string]interface{}{
"method": api,
"params": params,
}
content, _ := c.encodeToken(payload)
url := c.BaseURL + "/" + api
req, err := http.NewRequestWithContext(ctx, "POST", url, strings.NewReader(content))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", DefaultContentType)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Errorf("Do Error, err=%+v", err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Errorf("ReadAll Error, err=%+v", err)
return nil, err
}
respBody, err := c.decodeToken(string(body))
if err != nil {
return nil, err
}
if err := respBody.Valid(); err != nil {
return nil, err
}
if resp.StatusCode == http.StatusBadRequest {
return respBody["result"], ErrFailedResponse
}
return respBody["result"], nil
}