Skip to content

Commit 1b1e4ca

Browse files
committed
replacing OTP with new Verify call
1 parent c8643b4 commit 1b1e4ca

File tree

4 files changed

+190
-150
lines changed

4 files changed

+190
-150
lines changed

client.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ func (c *Client) HLR(id string) (*HLR, error) {
144144
func (c *Client) NewHLR(msisdn, reference string) (*HLR, error) {
145145
params := &url.Values{
146146
"msisdn": {msisdn},
147-
"reference": {reference}}
147+
"reference": {reference},
148+
}
148149

149150
hlr := &HLR{}
150151
if err := c.request(hlr, "hlr", params); err != nil {
@@ -227,41 +228,40 @@ func (c *Client) NewVoiceMessage(recipients []string, body string, params *Voice
227228
return message, nil
228229
}
229230

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)
231+
// NewVerify generates a new One-Time-Password for one recipient.
232+
func (c *Client) NewVerify(recipient string, params *VerifyParams) (*Verify, error) {
233+
urlParams := paramsForVerify(params)
233234
urlParams.Set("recipient", recipient)
234235

235-
message := &OtpMessage{}
236-
if err := c.request(message, "otp/generate", urlParams); err != nil {
236+
verify := &Verify{}
237+
if err := c.request(verify, "verify", urlParams); err != nil {
237238
if err == ErrResponse {
238-
return message, err
239+
return verify, err
239240
}
240241

241242
return nil, err
242243
}
243244

244-
return message, nil
245+
return verify, nil
245246
}
246247

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)
248+
// VerifyToken performs token value check against MessageBird API.
249+
func (c *Client) VerifyToken(id, token string) (*Verify, error) {
250+
params := &url.Values{}
251+
params.Set("token", token)
252252

253-
path := "otp/verify?" + urlParams.Encode()
253+
path := "verify/" + id + "?" + params.Encode()
254254

255-
message := &OtpMessage{}
256-
if err := c.request(message, path, nil); err != nil {
255+
verify := &Verify{}
256+
if err := c.request(verify, path, nil); err != nil {
257257
if err == ErrResponse {
258-
return message, err
258+
return verify, err
259259
}
260260

261261
return nil, err
262262
}
263263

264-
return message, nil
264+
return verify, nil
265265
}
266266

267267
// Lookup performs a new lookup for the specified number.

otp_message_test.go

Lines changed: 0 additions & 114 deletions
This file was deleted.

otp_message.go renamed to verify.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,55 @@ package messagebird
22

33
import (
44
"net/url"
5+
"strconv"
56
"time"
67
)
78

8-
type OtpMessage struct {
9-
Id string
10-
Recipient string
9+
// Verify object represents MessageBird server response.
10+
type Verify struct {
11+
ID string
12+
HRef string
1113
Reference string
1214
Status string
13-
Href map[string]string `json:"href"`
15+
Messages map[string]string
1416
CreatedDatetime *time.Time
1517
ValidUntilDatetime *time.Time
18+
Recipient int
1619
Errors []Error
1720
}
1821

19-
type OtpParams struct {
20-
Reference string
21-
Originator string
22-
Type string
23-
Language string
24-
Voice string
25-
Template string
26-
DataCoding string
22+
// VerifyParams handles optional verification parameters.
23+
type VerifyParams struct {
24+
Originator string
25+
Reference string
26+
Type string
27+
Template string
28+
DataCoding string
29+
Voice string
30+
Language string
31+
Timeout int
32+
TokenLength int
2733
}
2834

29-
// paramsForOtp converts the specified OtpParams struct to a
30-
// url.Values pointer and returns it.
31-
func paramsForOtp(params *OtpParams) *url.Values {
35+
func paramsForVerify(params *VerifyParams) *url.Values {
3236
urlParams := &url.Values{}
3337

3438
if params == nil {
3539
return urlParams
3640
}
3741

38-
if params.Reference != "" {
39-
urlParams.Set("reference", params.Reference)
40-
}
4142
if params.Originator != "" {
4243
urlParams.Set("originator", params.Originator)
4344
}
45+
46+
if params.Reference != "" {
47+
urlParams.Set("reference", params.Reference)
48+
}
49+
4450
if params.Type != "" {
4551
urlParams.Set("type", params.Type)
4652
}
53+
4754
if params.Template != "" {
4855
urlParams.Set("template", params.Template)
4956
}
@@ -52,6 +59,14 @@ func paramsForOtp(params *OtpParams) *url.Values {
5259
urlParams.Set("datacoding", params.DataCoding)
5360
}
5461

62+
if params.Timeout != 0 {
63+
urlParams.Set("timeout", strconv.Itoa(params.Timeout))
64+
}
65+
66+
if params.TokenLength != 0 {
67+
urlParams.Set("tokenLength", strconv.Itoa(params.TokenLength))
68+
}
69+
5570
// Specific params for voice messages
5671
if params.Language != "" {
5772
urlParams.Set("language", params.Language)

0 commit comments

Comments
 (0)