forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
240 lines (198 loc) · 5.48 KB
/
endpoints.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package pubnub
import (
"bytes"
"fmt"
"log"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/pubnub/go/pnerr"
"github.com/pubnub/go/utils"
)
type endpointOpts interface {
jobQueue() chan *JobQItem
config() Config
client() *http.Client
context() Context
validate() error
buildPath() (string, error)
buildQuery() (*url.Values, error)
buildBody() ([]byte, error)
buildBodyMultipartFileUpload() (bytes.Buffer, *multipart.Writer, int64, error)
httpMethod() string
operationType() OperationType
telemetryManager() *TelemetryManager
}
// SetQueryParam appends the query params map to the query string
func SetQueryParam(q *url.Values, queryParam map[string]string) {
if queryParam != nil {
for key, value := range queryParam {
q.Set(key, utils.URLEncode(value))
}
}
}
// SetArrayTypeQueryParam appends to the query string the key val pair
func SetArrayTypeQueryParam(q *url.Values, val []string, key string) {
for _, value := range val {
q.Add(key, utils.URLEncode(value))
}
}
// SetQueryParamAsCommaSepString appends to the query string the comma separated string.
func SetQueryParamAsCommaSepString(q *url.Values, val []string, key string) {
q.Set(key, strings.Join(val, ","))
}
// SetPushEnvironment appends the push environment to the query string
func SetPushEnvironment(q *url.Values, env PNPushEnvironment) {
if string(env) != "" {
q.Set("environment", string(env))
} else {
q.Set("environment", string(PNPushEnvironmentDevelopment))
}
}
// SetPushTopic appends the topic to the query string
func SetPushTopic(q *url.Values, topic string) {
if topic != "" {
q.Set("topic", utils.URLEncode(topic))
}
}
func defaultQuery(uuid string, telemetryManager *TelemetryManager) *url.Values {
v := &url.Values{}
v.Set("pnsdk", "PubNub-Go/"+Version)
v.Set("uuid", uuid)
for queryName, queryParam := range telemetryManager.OperationLatency() {
v.Set(queryName, queryParam)
}
return v
}
func buildURL(o endpointOpts) (*url.URL, error) {
var stringifiedQuery string
var signature string
path, err := o.buildPath()
if err != nil {
return &url.URL{}, err
}
query, err := o.buildQuery()
if err != nil {
return &url.URL{}, err
}
if v := o.config().AuthKey; v != "" && query.Get("auth") == "" {
query.Set("auth", v)
}
if o.config().SecretKey != "" {
timestamp := time.Now().Unix()
query.Set("timestamp", strconv.Itoa(int(timestamp)))
if (!o.config().UsePAMV3) || ((o.operationType() == PNPublishOperation) && (o.httpMethod() == "POST")) {
signedInput := o.config().SubscribeKey + "\n" + o.config().PublishKey + "\n"
signedInput += fmt.Sprintf("%s\n", path)
signedInput += utils.PreparePamParams(query)
o.config().Log.Println("signedInput:", signedInput)
signature = utils.GetHmacSha256(o.config().SecretKey, signedInput)
} else {
signature = createSignatureV2(o, path, query)
}
}
if o.operationType() == PNPublishOperation {
v := query.Get("meta")
if v != "" {
query.Set("meta", utils.URLEncode(v))
}
}
if o.operationType() == PNSetStateOperation {
v := query.Get("state")
query.Set("state", utils.URLEncode(v))
}
if v := query.Get("uuid"); v != "" {
query.Set("uuid", utils.URLEncode(v))
}
if v := query.Get("filter-expr"); v != "" {
query.Set("filter-expr", utils.URLEncode(v))
}
if v := query.Get("filter"); v != "" {
query.Set("filter", utils.URLEncode(v))
}
if v := query.Get("include"); v != "" {
query.Set("include", utils.URLEncode(v))
}
if v := query.Get("sort"); v != "" {
query.Set("sort", utils.URLEncode(v))
}
i := 0
for k, v := range *query {
for j, value := range v {
if (i == len(*query)-1) && (j == len(v)-1) {
stringifiedQuery += fmt.Sprintf("%s=%s", k, value)
} else {
stringifiedQuery += fmt.Sprintf("%s=%s&", k, value)
}
j++
}
i++
}
if signature != "" {
stringifiedQuery += fmt.Sprintf("&signature=%s", signature)
}
secure := ""
if o.config().Secure {
secure = "s"
}
scheme := fmt.Sprintf("http%s", secure)
host := o.config().Origin
if o.httpMethod() != "POSTFORM" {
path = fmt.Sprintf("//%s%s", o.config().Origin, path)
} else {
p := strings.Split(path, "://")
scheme = p[0]
p2 := strings.Split(p[1], "?")
path = fmt.Sprintf("//%s", p2[0])
h := strings.Split(p[1], "/")
host = h[0]
stringifiedQuery = ""
}
retURL := &url.URL{
Opaque: path,
Scheme: scheme,
Host: host,
RawQuery: stringifiedQuery,
}
return retURL, nil
}
func createSignatureV2(o endpointOpts, path string, query *url.Values) string {
bodyString := ""
b, err := o.buildBody()
if err == nil {
bodyString = string(b)
} else {
o.config().Log.Println("buildBody error", err.Error())
}
sig := createSignatureV2FromStrings(
o.httpMethod(),
o.config().PublishKey,
o.config().SecretKey,
fmt.Sprintf("%s", path),
utils.PreparePamParams(query),
bodyString,
o.config().Log,
)
o.config().Log.Println("signaturev2:", sig)
return sig
}
func createSignatureV2FromStrings(httpMethod, pubKey, secKey, path, query, body string, l *log.Logger) string {
signedInputV2 := httpMethod + "\n"
signedInputV2 += pubKey + "\n"
signedInputV2 += path + "\n"
signedInputV2 += query + "\n"
signedInputV2 += body
if l != nil {
l.Println("signedInputV2:", signedInputV2)
}
encoded := utils.GetHmacSha256(secKey, signedInputV2)
encoded = strings.TrimRight(encoded, "=")
signatureV2 := "v2." + encoded
return signatureV2
}
func newValidationError(o endpointOpts, msg string) error {
return pnerr.NewValidationError(o.operationType().String(), msg)
}