infobip-twofactor 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +13 -0
- data/README.md +10 -6
- data/lib/infobip/twofactor/api.rb +23 -7
- data/lib/infobip/twofactor/version.rb +1 -1
- data/spec/support/fakeweb_api_responses.rb +55 -4
- data/spec/twofactor_api_spec.rb +30 -22
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56411b5a553e4bcc955e3abec3ed2df44288b9fc
|
4
|
+
data.tar.gz: a061f0e44d3817e056da19acd18afe88f4924171
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0044a5643747128a98f1c7168dabe980c78af53da75eca534f5c7800735e1dc25589657a8cd1c49a8045f03eba833b3520f05f288081c94c06e3dc15993b0625
|
7
|
+
data.tar.gz: d3ba9d5131c5206bcbe1aa1251ca05a9d5a02f4fd8f492c5779309738560c7ef482720f57b6e7ba19f019d5754f28bc62e26c0f35b887f17c166c2ea1532874c
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/visualitypl/infobip-twofactor.svg?branch=master)](https://travis-ci.org/visualitypl/infobip-twofactor.svg?branch=master)
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/infobip-twofactor.svg)](http://badge.fury.io/rb/infobip-twofactor)
|
4
|
+
|
1
5
|
# Infobip::Twofactor
|
2
6
|
|
3
7
|
Simple wrapper gem for Infobip two factor authentication service
|
@@ -22,17 +26,17 @@ Or install it yourself as:
|
|
22
26
|
|
23
27
|
```ruby
|
24
28
|
require 'infobip/twofactor'
|
25
|
-
@twofactor = Infobip::Twofactor::API.new("username", "password", "http://oneapi-test.infobip.com/2fa/1")
|
29
|
+
@twofactor = Infobip::Twofactor::API.new("username", "password", "http://oneapi-test.infobip.com/2fa/1", "message_id", "application_id")
|
26
30
|
|
27
|
-
=> #<Infobip::Twofactor::API:0x007fcce1e7efe0 @authorization_string="
|
31
|
+
=> #<Infobip::Twofactor::API:0x007fcce1e7efe0 @authorization_string="aXBwZ4r3rOjRA34tLMipf", @auth=#<Crib::API:0x007fcce1e7ee78 @_agent=<Sawyer::Agent http://oneapi-test.infobip.com/2fa/1>, @_last_response=#<Sawyer::Response: 200 @rels={} @data="\"d02f4d9a2d9fb5a70b827819823254b8-9d48e592-db4a-4a70-95f0-59b3449f48d4\"">>, @api_key="d02f4d9a2d9fb5a723827819823254b8-9d48e592-db4a-4a70-95f0-59b5a49f4edd4", @api=#<Crib::API:0x007fcce1f8c0b8 @_agent=<Sawyer::Agent http://oneapi-test.infobip.com/2fa/1>>>
|
28
32
|
|
29
|
-
@twofactor.send_pin("
|
33
|
+
@twofactor.send_pin("phone")
|
30
34
|
|
31
|
-
=> {:pinId=>"C2390DD39E0E1E39252D34BE796885FD", :to=>"
|
35
|
+
=> {:pinId=>"C2390DD39E0E1E39252D34BE796885FD", :to=>"48738288288", :ncStatus=>"NC_DESTINATION_UNKNOWN", :smsStatus=>"MESSAGE_SENT"}
|
32
36
|
|
33
|
-
@twofactor.verify_pin("
|
37
|
+
@twofactor.verify_pin("pin")
|
34
38
|
|
35
|
-
=> {:pinId=>"C2390DD39E0E1EA9252D34BE796885FD", :msisdn=>"
|
39
|
+
=> {:pinId=>"C2390DD39E0E1EA9252D34BE796885FD", :msisdn=>"48738288288", :verified=>true, :attemptsRemaining=>0}
|
36
40
|
|
37
41
|
```
|
38
42
|
|
@@ -6,12 +6,17 @@ module Infobip
|
|
6
6
|
class API
|
7
7
|
|
8
8
|
attr_reader :api_key
|
9
|
-
|
10
|
-
|
9
|
+
attr_reader :pin_id
|
10
|
+
attr_reader :application_id
|
11
|
+
attr_reader :message_id
|
11
12
|
|
12
|
-
def initialize(username, password, url)
|
13
|
+
def initialize(username, password, url, message_id, application_id)
|
14
|
+
raise "Missing message_id" unless message_id
|
15
|
+
raise "Missing application_id" unless application_id
|
13
16
|
#send auth reqest
|
14
17
|
@authorization_string = Base64.strict_encode64("#{username}:#{password}")
|
18
|
+
@message_id = message_id
|
19
|
+
@application_id = application_id
|
15
20
|
|
16
21
|
@auth = Crib.api(url) do |http|
|
17
22
|
http.headers[:authorization] = "Basic #{@authorization_string}"
|
@@ -27,12 +32,23 @@ module Infobip
|
|
27
32
|
end
|
28
33
|
|
29
34
|
|
30
|
-
def send_pin(
|
31
|
-
|
35
|
+
def send_pin(phone)
|
36
|
+
raise "Missing phone number" unless phone
|
37
|
+
raise "Missing message_id" unless @message_id
|
38
|
+
raise "Missing application_id" unless @application_id
|
39
|
+
response = @api.pin._post(applicationId: @application_id, messageId: @message_id, to: phone)
|
40
|
+
raise "Malformed two factor API response - no sms status field" unless (response.respond_to?(:smsStatus))
|
41
|
+
raise "Malformed two factor API response - no pin Id field" unless (response.respond_to?(:pinId))
|
42
|
+
raise "SMS not sent" unless (response.smsStatus == "MESSAGE_SENT")
|
43
|
+
@pin_id = response[:pinId]
|
44
|
+
response
|
32
45
|
end
|
33
46
|
|
34
|
-
def verify_pin(
|
35
|
-
@
|
47
|
+
def verify_pin(pin)
|
48
|
+
raise "Missing pin id" unless @pin_id
|
49
|
+
response = @api.pin(@pin_id).verify._post(pin: pin)
|
50
|
+
raise "Malformed two factor API response - no verified field" unless (response.respond_to?(:verified))
|
51
|
+
response
|
36
52
|
end
|
37
53
|
|
38
54
|
end
|
@@ -2,10 +2,61 @@ def api_key_response_body
|
|
2
2
|
"\"fc6112a4559b5b44dafe5a943771b53b-020887ca-c56d-42c4-80b0-33a16ea23610\""
|
3
3
|
end
|
4
4
|
|
5
|
-
def
|
6
|
-
|
5
|
+
def api_send_pin_response
|
6
|
+
%q{HTTP/1.1 200 OK
|
7
|
+
Server: Apache-Coyote/1.1
|
8
|
+
Access-Control-Allow-Origin: *
|
9
|
+
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE
|
10
|
+
Access-Control-Allow-Headers: Authorization, Content-Type
|
11
|
+
Content-Type: application/json;charset=UTF-8
|
12
|
+
Content-Length: 126
|
13
|
+
Date: Wed, 21 Jan 2015 13:58:15 GMT
|
14
|
+
Connection: close
|
15
|
+
|
16
|
+
{"pinId":"2B29B71922B37D3C93F8CEBB85B9E3CF","to":"48790809242","ncStatus":"NC_DESTINATION_UNKNOWN","smsStatus":"MESSAGE_SENT"}
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def api_verify_pin_response
|
21
|
+
%q{HTTP/1.1 200 OK
|
22
|
+
Server: Apache-Coyote/1.1
|
23
|
+
Access-Control-Allow-Origin: *
|
24
|
+
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE
|
25
|
+
Access-Control-Allow-Headers: Authorization, Content-Type
|
26
|
+
Content-Type: application/json;charset=UTF-8
|
27
|
+
Content-Length: 105
|
28
|
+
Date: Wed, 21 Jan 2015 14:06:38 GMT
|
29
|
+
Connection: close
|
30
|
+
|
31
|
+
{"pinId":"2B29B71922B37D3C93F8CEBB85B9E3CF","msisdn":"48790809242","verified":true,"attemptsRemaining":0}
|
32
|
+
}
|
7
33
|
end
|
8
34
|
|
9
|
-
def
|
10
|
-
|
35
|
+
def api_verify_pin_failed_response
|
36
|
+
%q{HTTP/1.1 200 OK
|
37
|
+
Server: Apache-Coyote/1.1
|
38
|
+
Access-Control-Allow-Origin: *
|
39
|
+
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE
|
40
|
+
Access-Control-Allow-Headers: Authorization, Content-Type
|
41
|
+
Content-Type: application/json;charset=UTF-8
|
42
|
+
Content-Length: 129
|
43
|
+
Date: Wed, 21 Jan 2015 14:06:38 GMT
|
44
|
+
Connection: close
|
45
|
+
|
46
|
+
{"pinId":"1129B71922B37D3C93F8CEBB85B9E3CF","msisdn":"48790809242","verified":false,"attemptsRemaining":2}
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def error_response
|
51
|
+
%q{HTTP/1.1 400 Bad Request
|
52
|
+
Server: Apache-Coyote/1.1
|
53
|
+
Access-Control-Allow-Origin: *
|
54
|
+
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE
|
55
|
+
Access-Control-Allow-Headers: Authorization, Content-Type
|
56
|
+
Content-Length: 96
|
57
|
+
Date: Thu, 22 Jan 2015 16:14:27 GMT
|
58
|
+
Connection: close
|
59
|
+
|
60
|
+
{"requestError":{"serviceException":{"messageId":"INVALID_ARGUMENT","text":"Invalid argument"}}}
|
61
|
+
}
|
11
62
|
end
|
data/spec/twofactor_api_spec.rb
CHANGED
@@ -8,41 +8,49 @@ describe Infobip::Twofactor::API do
|
|
8
8
|
|
9
9
|
before do
|
10
10
|
@configuration = YAML.load_file("configuration.yml")
|
11
|
+
|
11
12
|
FakeWeb.register_uri(:post, "http://ippdok:4%40SkK2*_@oneapi-test.infobip.com/2fa/1/api-key", status: ["200", "OK"], body: api_key_response_body)
|
12
|
-
FakeWeb.register_uri(:post, "http://oneapi-test.infobip.com/2fa/1/pin",
|
13
|
-
FakeWeb.register_uri(:post, "http://oneapi-test.infobip.com/2fa/1/pin/
|
14
|
-
|
13
|
+
FakeWeb.register_uri(:post, "http://oneapi-test.infobip.com/2fa/1/pin", response: api_send_pin_response)
|
14
|
+
FakeWeb.register_uri(:post, "http://oneapi-test.infobip.com/2fa/1/pin/2B29B71922B37D3C93F8CEBB85B9E3CF/verify", response: api_verify_pin_response)
|
15
|
+
|
16
|
+
@twofactor = Infobip::Twofactor::API.new(@configuration["username"], @configuration["password"], @configuration["url"], @configuration["message_id"], @configuration["application_id"])
|
15
17
|
end
|
16
18
|
|
17
19
|
subject { @twofactor }
|
18
20
|
|
19
21
|
it "should return valid api key" do
|
20
|
-
expect(
|
21
|
-
expect(
|
22
|
+
expect(subject.api_key.class).to eq String
|
23
|
+
expect(subject.api_key.length).to eq 69
|
22
24
|
end
|
23
25
|
|
24
26
|
it "should create a valid Send PIN request, given valid params" do
|
25
|
-
response =
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
expect(
|
27
|
+
response = subject.send_pin("48790809242")
|
28
|
+
expect(response[:smsStatus]).to eq "MESSAGE_SENT"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should store pin_id value" do
|
32
|
+
response = subject.send_pin("48790809242")
|
33
|
+
expect(subject.pin_id).to eq "2B29B71922B37D3C93F8CEBB85B9E3CF"
|
32
34
|
end
|
33
35
|
|
34
36
|
it "should create a valid Verify PIN request, given valid params" do
|
35
|
-
response =
|
36
|
-
|
37
|
-
|
38
|
-
else
|
39
|
-
response_hash = response
|
40
|
-
end
|
41
|
-
expect(response_hash[:verified]).to eq "true"
|
37
|
+
response = subject.send_pin("48790809242")
|
38
|
+
response = subject.verify_pin("1234")
|
39
|
+
expect(response[:verified]).to eq true
|
42
40
|
end
|
43
41
|
|
44
|
-
|
42
|
+
context "error handling" do
|
43
|
+
|
44
|
+
it "new should raise an error when missing params"
|
45
45
|
|
46
|
+
it "send pin should raise an error when missing params"
|
47
|
+
|
48
|
+
it "verify pin should raise an error when missing params"
|
49
|
+
|
50
|
+
it "should raise an error when received invalid response"
|
51
|
+
|
52
|
+
it "should raise an error when received error object"
|
53
|
+
|
54
|
+
end
|
46
55
|
|
47
|
-
|
48
|
-
# request = Hash.from_xml(FakeWeb.last_request.body.gsub("\n", ""))["createTransactionRequest"]["transactionRequest"]
|
56
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: infobip-twofactor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- knx
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: crib
|
@@ -159,6 +159,7 @@ extra_rdoc_files: []
|
|
159
159
|
files:
|
160
160
|
- ".gitignore"
|
161
161
|
- ".rspec"
|
162
|
+
- ".travis.yml"
|
162
163
|
- Gemfile
|
163
164
|
- Guardfile
|
164
165
|
- LICENSE
|