Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
implement api call to request tokens via SMS
Browse files Browse the repository at this point in the history
Signed-off-by: David A. Cuadrado <krawek@gmail.com>
  • Loading branch information
dcu committed May 21, 2012
1 parent f1a7386 commit a8579e7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
27 changes: 20 additions & 7 deletions README.rdoc
Expand Up @@ -10,7 +10,7 @@ Ruby library to access the Authy API
Authy.api_uri = 'https://api.authy.com/'


### Registering a user
### Registering a user

`Authy::API.register_user` requires the user e-mail address and cellphone. Optionally you can pass in the country_code or we will asume
USA. The call will return you the authy id for the user that you need to store in your database.
Expand All @@ -31,12 +31,25 @@ Assuming you have a `users` database with a `authy_id` field in the `users` data
`Authy::API.verify` takes the authy_id that you are verifying and the token that you want to verify. You should have the authy_id in your database

response = Authy::API.verify(:id => user.authy_id, :token => 'token-user-entered')

if response.ok?
//token was valid, user can sign in
else
//token is invalid


if response.ok?
//token was valid, user can sign in
else
//token is invalid


### Requesting a SMS token

`Authy::API.request_sms` takes the authy_id that you want to send a SMS token. This requires Authy SMS plugin to be enabled.

response = Authy::API.request_sms(:id => user.authy_id)

if response.ok?
//sms was sent
else
response.
//sms failed to send



## Contributing to authy
Expand Down
13 changes: 13 additions & 0 deletions lib/authy/api.rb
Expand Up @@ -17,12 +17,25 @@ def self.register_user(attributes)
Authy::User.new(response)
end

# options:
# :id user id
# :token authy token entered by the user
def self.verify(attributes)
token = attributes[:token] || attributes['token']
user_id = attributes[:id] || attributes['id']
response = Typhoeus::Request.get("#{Authy.api_uri}/protected/json/verify/#{token}/#{user_id}", :params => {:api_key => Authy.api_key})

Authy::Response.new(response)
end

# options:
# :id user id
def self.request_sms(attributes)
user_id = attributes[:id] || attributes['id']

response = Typhoeus::Request.get("#{Authy.api_uri}/protected/json/sms/#{user_id}", :params => {:api_key => Authy.api_key})

Authy::Response.new(response)
end
end
end
4 changes: 4 additions & 0 deletions lib/authy/response.rb
Expand Up @@ -26,6 +26,10 @@ def error_msg
(@raw_response.curl_error_message == "No error" && self.empty?) ? self.body : @raw_response.curl_error_message
end

def errors
self['errors'] || {}
end

protected
def method_missing(name, *args, &block)
if self.include?(name.to_s)
Expand Down
8 changes: 8 additions & 0 deletions spec/authy/api_spec.rb
Expand Up @@ -26,4 +26,12 @@
user.errors.should be_kind_of(Hash)
user.errors['cellphone'].should == ['must be a valid cellphone number.']
end

it "should request a SMS token" do
user = Authy::API.register_user(:email => generate_email, :cellphone => generate_cellphone, :country_code => 1)
user.should be_ok

response = Authy::API.request_sms(:id => user.id)
response.should be_ok
end
end

0 comments on commit a8579e7

Please sign in to comment.