Skip to content

Commit a625c93

Browse files
author
Sam Wierema
committed
Add support for the new OTP (One-Time-Password) endpoint.
1 parent 322aabe commit a625c93

File tree

3 files changed

+211
-1
lines changed

3 files changed

+211
-1
lines changed

client.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
const (
21-
ClientVersion = "2.0.1"
21+
ClientVersion = "2.1.0"
2222
Endpoint = "https://rest.messagebird.com"
2323
)
2424

@@ -226,3 +226,40 @@ func (c *Client) NewVoiceMessage(recipients []string, body string, params *Voice
226226

227227
return message, nil
228228
}
229+
230+
// OtpGenerate generates a new One-Time-Password for one recipient
231+
func (c *Client) OtpGenerate(recipient string, params *OtpParams) (*OtpMessage, error) {
232+
urlParams := paramsForOtp(params)
233+
urlParams.Set("recipient", recipient)
234+
235+
message := &OtpMessage{}
236+
if err := c.request(message, "otp/generate", urlParams); err != nil {
237+
if err == ErrResponse {
238+
return message, err
239+
}
240+
241+
return nil, err
242+
}
243+
244+
return message, nil
245+
}
246+
247+
// OtpVerify verifies the token that was generated with OtpGenerate
248+
func (c *Client) OtpVerify(recipient string, token string, params *OtpParams) (*OtpMessage, error) {
249+
urlParams := paramsForOtp(params)
250+
urlParams.Set("recipient", recipient)
251+
urlParams.Set("token", token)
252+
253+
path := "otp/verify?" + urlParams.Encode()
254+
255+
message := &OtpMessage{}
256+
if err := c.request(message, path, nil); err != nil {
257+
if err == ErrResponse {
258+
return message, err
259+
}
260+
261+
return nil, err
262+
}
263+
264+
return message, nil
265+
}

otp_message.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package messagebird
2+
3+
import (
4+
"net/url"
5+
"time"
6+
)
7+
8+
type OtpMessage struct {
9+
Id string
10+
Recipient string
11+
Reference string
12+
Status string
13+
Href map[string]string `json:"href"`
14+
CreatedDatetime *time.Time
15+
ValidUntilDatetime *time.Time
16+
Errors []Error
17+
}
18+
19+
type OtpParams struct {
20+
Reference string
21+
Originator string
22+
Type string
23+
Language string
24+
Voice string
25+
Template string
26+
}
27+
28+
// paramsForOtp converts the specified OtpParams struct to a
29+
// url.Values pointer and returns it.
30+
func paramsForOtp(params *OtpParams) *url.Values {
31+
urlParams := &url.Values{}
32+
33+
if params == nil {
34+
return urlParams
35+
}
36+
37+
if params.Reference != "" {
38+
urlParams.Set("reference", params.Reference)
39+
}
40+
if params.Originator != "" {
41+
urlParams.Set("originator", params.Originator)
42+
}
43+
if params.Type != "" {
44+
urlParams.Set("type", params.Type)
45+
}
46+
if params.Template != "" {
47+
urlParams.Set("template", params.Template)
48+
}
49+
50+
// Specific params for voice messages
51+
if params.Language != "" {
52+
urlParams.Set("language", params.Language)
53+
}
54+
if params.Voice != "" {
55+
urlParams.Set("voice", params.Voice)
56+
}
57+
58+
return urlParams
59+
}

otp_message_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package messagebird
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
var OtpGenerateObject []byte = []byte(`{
9+
"id": "76d429606554b5827a46b12o29500954",
10+
"recipient": "31630174123",
11+
"reference": null,
12+
"status": "sent",
13+
"href": {
14+
"message": "https://rest.messagebird.com/messages/5b823a806554b5827b0cb66b58154766"
15+
},
16+
"createdDatetime": "2015-05-07T12:18:47+00:00",
17+
"validUntilDatetime": "2015-05-07T12:19:17+00:00"
18+
}`)
19+
20+
func TestOtpGenerate(t *testing.T) {
21+
SetServerResponse(200, OtpGenerateObject)
22+
23+
result, err := mbClient.OtpGenerate("31630174123", nil)
24+
if err != nil {
25+
t.Fatalf("Didn't expect error while generating an OTP: %s", err)
26+
}
27+
28+
if result.Id != "76d429606554b5827a46b12o29500954" {
29+
t.Errorf("Unexpected OTP message id: %s", result.Id)
30+
}
31+
32+
if result.Recipient != "31630174123" {
33+
t.Errorf("Unexpected OTP message recipient: %s", result.Recipient)
34+
}
35+
36+
if result.Reference != "" {
37+
t.Errorf("Unexpected OTP message reference: %s", result.Reference)
38+
}
39+
40+
if result.Status != "sent" {
41+
t.Errorf("Unexpected OTP message status: %s", result.Status)
42+
}
43+
44+
if result.CreatedDatetime == nil || result.CreatedDatetime.Format(time.RFC3339) != "2015-05-07T12:18:47Z" {
45+
t.Errorf("Unexpected OTP message created datetime: %s", result.CreatedDatetime)
46+
}
47+
48+
if result.ValidUntilDatetime == nil || result.ValidUntilDatetime.Format(time.RFC3339) != "2015-05-07T12:19:17Z" {
49+
t.Errorf("Unexpected OTP message valid until datetime: %s", result.ValidUntilDatetime)
50+
}
51+
52+
message, exists := result.Href["message"]
53+
if !exists {
54+
t.Errorf("Unexpected OTP message href value: %s", result.Href)
55+
}
56+
57+
if exists && message != "https://rest.messagebird.com/messages/5b823a806554b5827b0cb66b58154766" {
58+
t.Errorf("Unexpected OTP message href message: %s", message)
59+
}
60+
}
61+
62+
var OtpVerifyObject []byte = []byte(`{
63+
"id": "8b912ea03554b7a3d5d6e22o95082672",
64+
"recipient": "31630174123",
65+
"reference": null,
66+
"status": "verified",
67+
"href": {
68+
"message": "https://rest.messagebird.com/messages/8668de203554b7a3d62b5e7b62054920"
69+
},
70+
"createdDatetime": "2015-05-07T14:44:13+00:00",
71+
"validUntilDatetime": "2015-05-07T14:44:43+00:00"
72+
}`)
73+
74+
func TestOtpVerify(t *testing.T) {
75+
SetServerResponse(200, OtpVerifyObject)
76+
77+
result, err := mbClient.OtpVerify("31630174123", "302443", nil)
78+
if err != nil {
79+
t.Fatalf("Didn't expect error while verifying an OTP: %s", err)
80+
}
81+
82+
if result.Id != "8b912ea03554b7a3d5d6e22o95082672" {
83+
t.Errorf("Unexpected OTP message id: %s", result.Id)
84+
}
85+
86+
if result.Recipient != "31630174123" {
87+
t.Errorf("Unexpected OTP message recipient: %s", result.Recipient)
88+
}
89+
90+
if result.Reference != "" {
91+
t.Errorf("Unexpected OTP message reference: %s", result.Reference)
92+
}
93+
94+
if result.Status != "verified" {
95+
t.Errorf("Unexpected OTP message status: %s", result.Status)
96+
}
97+
98+
if result.CreatedDatetime == nil || result.CreatedDatetime.Format(time.RFC3339) != "2015-05-07T14:44:13Z" {
99+
t.Errorf("Unexpected OTP message created datetime: %s", result.CreatedDatetime)
100+
}
101+
102+
if result.ValidUntilDatetime == nil || result.ValidUntilDatetime.Format(time.RFC3339) != "2015-05-07T14:44:43Z" {
103+
t.Errorf("Unexpected OTP message valid until datetime: %s", result.ValidUntilDatetime)
104+
}
105+
106+
message, exists := result.Href["message"]
107+
if !exists {
108+
t.Errorf("Unexpected OTP message href value: %s", result.Href)
109+
}
110+
111+
if exists && message != "https://rest.messagebird.com/messages/8668de203554b7a3d62b5e7b62054920" {
112+
t.Errorf("Unexpected OTP message href message: %s", message)
113+
}
114+
}

0 commit comments

Comments
 (0)