-
Notifications
You must be signed in to change notification settings - Fork 23
/
utils.go
254 lines (230 loc) · 6.27 KB
/
utils.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package plivo
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"reflect"
"sort"
"strings"
validator "gopkg.in/go-playground/validator.v9"
)
func Numbers(numbers ...string) string {
return strings.Join(numbers, "<")
}
func headersWithSep(headers map[string]string, keyValSep, itemSep string, escape bool) string {
v := url.Values{}
for key, value := range headers {
v.Set(key, value)
}
var buf bytes.Buffer
keys := make([]string, 0, len(v))
for k := range v {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
vs := v[k]
var prefix string
if escape {
prefix = url.QueryEscape(k) + keyValSep
} else {
prefix = k + keyValSep
}
for _, v := range vs {
if buf.Len() > 0 {
buf.WriteString(itemSep)
}
buf.WriteString(prefix)
if escape {
buf.WriteString(url.QueryEscape(v))
} else {
buf.WriteString(v)
}
}
}
return buf.String()
}
// Some code from encode.go from the Go Standard Library
func Headers(headers map[string]string) string {
return headersWithSep(headers, "=", ",", true)
}
// The old signature validation is deprecated. Will be marked deprecated in the next release.
func ComputeSignature(authToken, uri string, params map[string]string) string {
originalString := fmt.Sprintf("%s%s", uri, headersWithSep(params, "", "", false))
mac := hmac.New(sha1.New, []byte(authToken))
mac.Write([]byte(originalString))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
func ValidateSignature(authToken, uri string, params map[string]string, signature string) bool {
return ComputeSignature(authToken, uri, params) == signature
}
// Adding V2 signature validation
func ComputeSignatureV2(authToken, uri string, nonce string) string {
parsedUrl, err := url.Parse(uri)
if err != nil {
panic(err)
}
var originalString string = parsedUrl.Scheme + "://" + parsedUrl.Host + parsedUrl.Path + nonce
mac := hmac.New(sha256.New, []byte(authToken))
mac.Write([]byte(originalString))
var messageMAC string = base64.StdEncoding.EncodeToString(mac.Sum(nil))
return messageMAC
}
func ValidateSignatureV2(uri string, nonce string, signature string, authToken string) bool {
return ComputeSignatureV2(authToken, uri, nonce) == signature
}
func GenerateUrl(uri string, params map[string]string, method string) string {
parsedUrl, err := url.Parse(uri)
if err != nil {
panic(err)
}
uri = parsedUrl.Scheme + "://" + parsedUrl.Host + parsedUrl.Path
if len(params) > 0 || len(parsedUrl.RawQuery) > 0 {
uri += "?"
}
if len(parsedUrl.RawQuery) > 0 {
if method == "GET" {
queryParamMap := getMapFromQueryString(parsedUrl.Query())
for k, v := range params {
queryParamMap[k] = v
}
uri += GetSortedQueryParamString(queryParamMap, true)
} else {
uri += GetSortedQueryParamString(getMapFromQueryString(parsedUrl.Query()), true) + "." + GetSortedQueryParamString(params, false)
uri = strings.TrimRight(uri, ".")
}
} else {
if method == "GET" {
uri += GetSortedQueryParamString(params, true)
} else {
uri += GetSortedQueryParamString(params, false)
}
}
return uri
}
func getMapFromQueryString(query url.Values) map[string]string {
/*
Example: input "a=b&c=d&z=x"
output (string): {"z":x", "c": "d", "a": "b"}
*/
mp := make(map[string]string)
if len(query) == 0 {
return mp
}
for key, val := range query {
mp[key] = val[0]
}
return mp
}
func GetSortedQueryParamString(params map[string]string, queryParams bool) string {
url := ""
keys := make([]string, 0, len(params))
for key := range params {
keys = append(keys, key)
}
sort.Strings(keys)
if queryParams {
for _, key := range keys {
url += key + "=" + params[key] + "&"
}
url = strings.TrimRight(url, "&")
} else {
for _, key := range keys {
url += key + params[key]
}
}
return url
}
func ComputeSignatureV3(authToken, uri, method string, nonce string, params map[string]string) string {
var newUrl = GenerateUrl(uri, params, method) + "." + nonce
mac := hmac.New(sha256.New, []byte(authToken))
mac.Write([]byte(newUrl))
var messageMAC = base64.StdEncoding.EncodeToString(mac.Sum(nil))
return messageMAC
}
func ValidateSignatureV3(uri, nonce, method, signature, authToken string, params ...map[string]string) bool {
parameters := map[string]string{}
if len(params) != 0 {
parameters = params[0]
}
multipleSignatures := strings.Split(signature, ",")
return Find(ComputeSignatureV3(authToken, uri, method, nonce, parameters), multipleSignatures)
}
func CreateWhatsappTemplate(templateData string) (template Template, err error) {
err = json.Unmarshal([]byte(templateData), &template)
if err != nil {
return
}
err = validateWhatsappTemplate(template)
if err !=nil{
return
}
return
}
func validateWhatsappTemplate(template Template) (err error) {
validate := validator.New()
err = validate.Struct(template)
if err != nil {
return
}
if template.Components != nil {
for _, component := range template.Components {
err = validate.Struct(component)
if err != nil {
return
}
if component.Parameters != nil {
for _, parameter := range component.Parameters {
err = validate.Struct(parameter)
if err != nil {
return
}
if parameter.Currency != nil {
err = validate.Struct(parameter.Currency)
if err != nil {
return
}
}
if parameter.DateTime != nil {
err = validate.Struct(parameter.DateTime)
if err != nil {
return
}
}
}
}
}
}
return
}
func Find(val string, slice []string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
func checkAndFetchCallInsightsRequestDetails(param interface{}) (isCallInsightsRequest bool, requestPath string) {
isCallInsightsRequest = false
if reflect.TypeOf(param).Kind() == reflect.Map {
if reflect.TypeOf(param).Key().Kind() == reflect.String {
if _, ok := param.(map[string]interface{})[CallInsightsParams]; ok {
isCallInsightsRequest = true
requestPath = param.(map[string]interface{})[CallInsightsParams].(map[string]interface{})[CallInsightsRequestPath].(string)
}
}
}
return
}
func isVoiceRequest() (extraData map[string]interface{}) {
extraData = make(map[string]interface{})
extraData["is_voice_request"] = true
extraData["retry"] = 0
return extraData
}