-
Notifications
You must be signed in to change notification settings - Fork 13
/
link_mobility_provider.go
145 lines (119 loc) · 3.31 KB
/
link_mobility_provider.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
package sms
import (
"context"
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"github.com/coretrix/hitrix/pkg/helper"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/config"
)
const LinkMobility = "link-mobility"
type LinkMobilityProvider struct {
Service int
Key string
Secret string
Endpoint string
Shortcode int
}
func NewLinkMobilityProvider(configService config.IConfig, _ clock.IClock) (IProvider, error) {
service, ok := configService.Int("sms.link_mobility.service")
if !ok {
return nil, errors.New("missing sms.link_mobility.service")
}
key, ok := configService.String("sms.link_mobility.key")
if !ok {
return nil, errors.New("missing sms.link_mobility.key")
}
secret, ok := configService.String("sms.link_mobility.secret")
if !ok {
return nil, errors.New("missing sms.link_mobility.secret")
}
endpoint, ok := configService.String("sms.link_mobility.endpoint")
if !ok {
return nil, errors.New("missing sms.link_mobility.endpoint")
}
shortcode, ok := configService.Int("sms.link_mobility.shortcode")
if !ok {
return nil, errors.New("missing sms.link_mobility.shortcode")
}
return &LinkMobilityProvider{
Service: service,
Key: key,
Secret: secret,
Endpoint: endpoint,
Shortcode: shortcode,
}, nil
}
func NewLinkMobilityProviderNoConfig(_ config.IConfig, _ clock.IClock) (IProvider, error) {
return &LinkMobilityProvider{}, nil
}
func (g *LinkMobilityProvider) GetName() string {
return LinkMobility
}
type linkMobilityMsg struct {
From string `json:"sc"`
Message string `json:"text"`
To string `json:"msisdn"`
ServiceID string `json:"service_id"`
}
func (g *LinkMobilityProvider) SendSMSMessage(message *Message) (string, error) {
row := &linkMobilityMsg{
ServiceID: strconv.Itoa(g.Service),
From: strconv.Itoa(g.Shortcode),
To: message.Number,
Message: message.Text,
}
body := []*linkMobilityMsg{row}
headers := g.getHeaders(body)
responseBody, _, code, err := helper.Call(
context.Background(),
"POST",
g.Endpoint,
headers,
time.Duration(timeoutInSeconds)*time.Second,
body,
nil)
if err != nil {
return failure, err
}
if code != http.StatusOK {
return failure, fmt.Errorf("expected status code OK, but got %v Response: %s", code, string(responseBody))
}
responseBodyJSON := &struct {
Meta struct {
Code int `json:"code"`
} `json:"meta"`
}{}
err = json.Unmarshal(responseBody, responseBodyJSON)
if err != nil {
return failure, fmt.Errorf("cannot unmarshal response Response: %s", string(responseBody))
}
if responseBodyJSON.Meta.Code != 200 {
return failure, fmt.Errorf("unexpected status code Response: %s", string(responseBody))
}
return success, nil
}
func (g *LinkMobilityProvider) getHeaders(body []*linkMobilityMsg) map[string]string {
bodyByte, _ := json.Marshal(body)
hmacSignature := genHMAC512(bodyByte, []byte(g.Secret))
signature := hex.EncodeToString(hmacSignature)
return map[string]string{
"Content-Type": "application/json",
"x-api-key": g.Key,
"x-api-sign": signature,
"Expect": "",
}
}
func genHMAC512(ciphertext, secret []byte) []byte {
mac := hmac.New(sha512.New, secret)
mac.Write(ciphertext)
hmacSum := mac.Sum(nil)
return hmacSum
}