-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
200 lines (162 loc) · 4.4 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
package pubnub
import (
"fmt"
"log"
"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)
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))
}
}
}
// 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))
}
i := 0
for k, v := range *query {
if i == len(*query)-1 {
stringifiedQuery += fmt.Sprintf("%s=%s", k, v[0])
} else {
stringifiedQuery += fmt.Sprintf("%s=%s&", k, v[0])
}
i++
}
if signature != "" {
stringifiedQuery += fmt.Sprintf("&signature=%s", signature)
}
path = fmt.Sprintf("//%s%s", o.config().Origin, path)
secure := ""
if o.config().Secure {
secure = "s"
}
retURL := &url.URL{
Opaque: path,
Scheme: fmt.Sprintf("http%s", secure),
Host: o.config().Origin,
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)
}
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(string(o.operationType()), msg)
}