Skip to content

Commit

Permalink
Add support for phone verification.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcu committed Jul 21, 2016
1 parent b9acd8d commit cf5da24
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ if status == authy.OneTouchStatusApproved {
}
```


# Phone Verification

## Start a phone verification

To start a phone verification use the following code:
```go
verification, err := authyAPI.StartPhoneVerification(1, "555-555-5555",
authy.SMS)
```

## Check phone verification

To check a phone verification use the following code:
```go
verification, err := authyAPI.CheckPhoneVerification(1, "555-555-5555",
"000000")

if verification.Success {
}
```

## Contributing

Get the code:
Expand Down
42 changes: 41 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ var (
client = &http.Client{}
)

var (
const (
longPollingDelay = 2 * time.Second
)

const (
// SMS indicates the message will be delivered via SMS
SMS = "sms"

// Voice indicates the message will be delivered via phone call
Voice = "call"
)

// Details for OneTouch transaction
type Details map[string]string

Expand Down Expand Up @@ -179,6 +187,38 @@ func (authy *Authy) WaitForApprovalRequest(uuid string, maxDuration time.Duratio
return OneTouchStatusExpired, nil
}

// StartPhoneVerification starts the phone verification process.
func (authy *Authy) StartPhoneVerification(countryCode int, phoneNumber string, via string, params url.Values) (*PhoneVerificationStart, error) {
params.Set("country_code", strconv.Itoa(countryCode))
params.Set("phone_number", phoneNumber)
params.Set("via", via)

path := fmt.Sprintf("/protected/json/phones/verification/start")
response, err := authy.DoRequest("POST", path, params)
if err != nil {
return nil, err
}

defer response.Body.Close()
return NewPhoneVerificationStart(response)
}

// CheckPhoneVerification checks the given verification code.
func (authy *Authy) CheckPhoneVerification(countryCode int, phoneNumber string, verificationCode string, params url.Values) (*PhoneVerificationCheck, error) {
params.Set("country_code", strconv.Itoa(countryCode))
params.Set("phone_number", phoneNumber)
params.Set("verification_code", verificationCode)

path := fmt.Sprintf("/protected/json/phones/verification/check")
response, err := authy.DoRequest("GET", path, params)
if err != nil {
return nil, err
}

defer response.Body.Close()
return NewPhoneVerificationCheck(response)
}

// DoRequest performs a HTTP request to the Authy API
func (authy *Authy) DoRequest(method string, path string, params url.Values) (*http.Response, error) {
apiURL := authy.buildURL(path)
Expand Down
57 changes: 57 additions & 0 deletions phone_verification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package authy

import (
"encoding/json"
"io/ioutil"
"net/http"
)

// PhoneVerificationStart encapsulates the response from the Authy API when requesting a phone verification.
type PhoneVerificationStart struct {
HTTPResponse *http.Response
UUID string `json:"uuid"`
Message string `json:"message"`
Success bool `json:"success"`
Carrier string `json:"carrier"`
}

// NewPhoneVerificationStart receives a http request, parses the body and return an instance of PhoneVerification
func NewPhoneVerificationStart(response *http.Response) (*PhoneVerificationStart, error) {
phoneVerification := &PhoneVerificationStart{HTTPResponse: response}
body, err := ioutil.ReadAll(response.Body)

if err != nil {
return phoneVerification, err
}

err = json.Unmarshal(body, &phoneVerification)
if err != nil {
return phoneVerification, err
}

return phoneVerification, nil
}

// PhoneVerificationCheck encapsulates the response from the Authy API when checking a phone verification.
type PhoneVerificationCheck struct {
HTTPResponse *http.Response
Message string `json:"message"`
Success bool `json:"success"`
}

// NewPhoneVerificationCheck receives a http request, parses the body and return an instance of PhoneVerification
func NewPhoneVerificationCheck(response *http.Response) (*PhoneVerificationCheck, error) {
phoneVerification := &PhoneVerificationCheck{HTTPResponse: response}
body, err := ioutil.ReadAll(response.Body)

if err != nil {
return phoneVerification, err
}

err = json.Unmarshal(body, &phoneVerification)
if err != nil {
return phoneVerification, err
}

return phoneVerification, nil
}
25 changes: 25 additions & 0 deletions phone_verification_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package authy

import (
"github.com/stretchr/testify/assert"
"net/url"
"testing"
)

func Test_StartPhoneVerification(t *testing.T) {
api := NewSandboxAuthyAPI("bf12974d70818a08199d17d5e2bae630")

verification, err := api.StartPhoneVerification(1, "555-555-5555", SMS, url.Values{})

assert.Nil(t, err)
assert.NotNil(t, verification)
}

func Test_CheckPhoneVerification(t *testing.T) {
api := NewSandboxAuthyAPI("bf12974d70818a08199d17d5e2bae630")

verification, err := api.CheckPhoneVerification(1, "555-555-5555", "000000", url.Values{})

assert.Nil(t, err)
assert.NotNil(t, verification)
}

0 comments on commit cf5da24

Please sign in to comment.