/
util.go
166 lines (138 loc) · 3.25 KB
/
util.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
package main
import (
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/jmcvetta/napping"
"io"
"log"
"math/rand"
"net/http"
"strconv"
"time"
)
var (
sessn napping.Session
headers http.Header
tsport http.Transport
clnt http.Client
allowedMethods map[string]bool
)
type SMSMessage struct {
Origin string `json:"origin"`
Destination string `json:"destination"`
Message string `json:"message"`
Campaign string `json:"campaign"`
SharedPool string `json:"sharedPool"`
}
type SMSResponse struct {
Errors []json.RawMessage
}
type httperr struct {
Message string
Errors []struct {
Resource string
Field string
Code string
}
}
func init() {
headers = make(http.Header)
// REST connection setup
tsport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
}
clnt = http.Client{Transport: tsport}
allowedMethods = map[string]bool{
"GET": true,
"POST": true,
"PUT": true,
"PATCH": true,
"DELETE": true,
"OPTIONS": true,
}
}
func GetAuthHeader(method string, uri string) (error, string) {
// Authorization: MAC id="your API key", ts="1325376000", nonce="random-string", mac="base64-encoded-hash"
if !allowedMethods[method] {
msg := fmt.Sprintf("invalid method: %v", method)
err := errors.New(msg)
return err, ""
}
now := time.Now().Unix()
tstr := strconv.FormatInt(now, 10)
nonce := createNonce(now)
var buffer bytes.Buffer
buffer.WriteString("MAC id=")
buffer.WriteString(fmt.Sprintf("%+q", apikey))
buffer.WriteString(", ts=")
buffer.WriteString(fmt.Sprintf("%+q", tstr))
buffer.WriteString(", nonce=")
buffer.WriteString(fmt.Sprintf("%+q", nonce))
buffer.WriteString(", mac=")
macstr := tstr + "\n"
macstr += nonce + "\n"
macstr += method + "\n" + uri + "\napi.smsglobal.com\n443\n\n"
mac := computeHmac256(macstr, secret)
buffer.WriteString(fmt.Sprintf("%+q", mac))
return nil, buffer.String()
}
func createNonce(now int64) string {
h := md5.New()
io.WriteString(h, strconv.FormatInt(now, 10))
io.WriteString(h, strconv.FormatInt(rand.Int63(), 10))
return fmt.Sprintf("%x", h.Sum(nil))
}
func computeHmac256(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func PostMsg(url string, payload interface{}, result interface{}) (error, *napping.Response) {
e := httperr{}
var (
err error
resp *napping.Response
)
err, auth := GetAuthHeader("POST", "/v1/sms/")
if err != nil {
return err, nil
}
if debug {
fmt.Println("auth: ", auth)
}
headers.Add("Authorization", auth)
headers.Add("Accept", "application/json")
sessn = napping.Session{
Client: &clnt,
Log: debug,
Header: &headers,
}
//
// Send request to server
//
resp, err = sessn.Post(url, &payload, &result, &e)
if err != nil {
return err, resp
}
if resp.Status() >= 300 {
return errors.New(e.Message), resp
} else {
// all is good in the world
return nil, resp
}
}
func printResponse(input interface{}) {
jsonresp, err := json.MarshalIndent(&input, "", "\t")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(jsonresp))
}