Skip to content

Commit 373b1e7

Browse files
authored
feat: Add multi-auth support to Messages and Verify2 (#345)
* Adding Basic auth support to Messages and Verify2 * Initial update to README * Updating Basic and JWT sections of the README * Updating Sig Auth section of README * Adding multi-auth section to README * Updating menu links
1 parent 38da604 commit 373b1e7

12 files changed

Lines changed: 422 additions & 28 deletions

README.md

Lines changed: 237 additions & 21 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
module Vonage
5+
class BasicAndBearerToken < AbstractAuthentication
6+
def update(object, data)
7+
return unless object.is_a?(Net::HTTPRequest)
8+
9+
if @config.authentication_preference == :basic
10+
object.basic_auth(@config.api_key, @config.api_secret)
11+
else
12+
object['Authorization'] = 'Bearer ' + @config.token
13+
end
14+
end
15+
end
16+
17+
private_constant :BasicAndBearerToken
18+
end

lib/vonage/basic_and_signature.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ def update(object, data)
1818
end
1919
end
2020

21-
private_constant :Basic
21+
private_constant :BasicAndSignature
2222
end

lib/vonage/messaging.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Vonage
66
class Messaging < Namespace
77
extend Forwardable
88

9-
self.authentication = BearerToken
9+
self.authentication = BasicAndBearerToken
1010

1111
self.request_body = JSON
1212

lib/vonage/verify2.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Vonage
55
class Verify2 < Namespace
6-
self.authentication = BearerToken
6+
self.authentication = BasicAndBearerToken
77

88
self.request_body = JSON
99

lib/vonage/verify2/template_fragments.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Vonage
55
class Verify2::TemplateFragments < Namespace
66
CHANNELS = ['sms', 'voice'].freeze
77

8-
self.authentication = BearerToken
8+
self.authentication = BasicAndBearerToken
99

1010
self.request_body = JSON
1111

lib/vonage/verify2/templates.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Vonage
55
class Verify2::Templates < Namespace
6-
self.authentication = BearerToken
6+
self.authentication = BasicAndBearerToken
77

88
self.request_body = JSON
99

test/vonage/messaging_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ def messaging
66
Vonage::Messaging.new(config)
77
end
88

9+
def messaging_with_basic_auth
10+
config = Vonage::Config.new.merge(
11+
{
12+
api_key: api_key,
13+
api_secret: api_secret,
14+
authentication_preference: :basic
15+
}
16+
)
17+
Vonage::Messaging.new(config)
18+
end
19+
920
def geo_specific_messaging_host
1021
'api-eu.vonage.com'
1122
end
@@ -80,6 +91,22 @@ def test_send_method_with_optional_params
8091
assert_kind_of Vonage::Response, messaging.send(to: "447700900000", from: "447700900001", **message)
8192
end
8293

94+
def test_send_method_with_basic_authentication
95+
params = {
96+
to: "447700900000",
97+
from: "447700900001",
98+
channel: "sms",
99+
message_type: "text",
100+
text: "Hello world!"
101+
}
102+
103+
stub_request(:post, messaging_uri).with(request(body: params, auth_method: basic_authorization)).to_return(response)
104+
105+
message = messaging_with_basic_auth.sms(message: "Hello world!")
106+
107+
assert_kind_of Vonage::Response, messaging_with_basic_auth.send(to: "447700900000", from: "447700900001", **message)
108+
end
109+
83110
def test_send_method_without_to
84111
assert_raises(ArgumentError) { messaging.send(from: "447700900001", channel: 'sms', message_type: 'text', text: "Hello world!") }
85112
end

test/vonage/test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ def vonage_host
112112
"api-eu.vonage.com"
113113
end
114114

115-
def request(body: nil, query: nil, headers: {})
116-
headers['Authorization'] = authorization
115+
def request(body: nil, query: nil, headers: {}, auth_method: nil)
116+
headers['Authorization'] = auth_method || authorization
117117
if body
118118
headers['Content-Type'] = 'application/json' unless headers.has_key?('Content-Type')
119119
end

test/vonage/verify2/template_fragments_test.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ def template_fragments
55
Vonage::Verify2::TemplateFragments.new(config)
66
end
77

8+
def template_fragments_with_basic_auth
9+
config = Vonage::Config.new.merge(
10+
{
11+
api_key: api_key,
12+
api_secret: api_secret,
13+
authentication_preference: :basic
14+
}
15+
)
16+
Vonage::Verify2::TemplateFragments.new(config)
17+
end
18+
819
def template_id
920
'8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9'
1021
end
@@ -43,6 +54,15 @@ def test_list_method_with_optional_params
4354
template_fragments_list.each { |template_fragment| assert_kind_of Vonage::Entity, template_fragment }
4455
end
4556

57+
def test_list_method_with_basic_authentication
58+
stub_request(:get, template_fragments_uri).with(request(auth_method: basic_authorization)).to_return(template_fragments_list_response)
59+
60+
template_fragments_list = template_fragments_with_basic_auth.list(template_id: template_id)
61+
62+
assert_kind_of Vonage::Verify2::TemplateFragments::ListResponse, template_fragments_list
63+
template_fragments_list.each { |template_fragment| assert_kind_of Vonage::Entity, template_fragment }
64+
end
65+
4666
def test_list_method_without_template_id
4767
assert_raises(ArgumentError) { template_fragments.list }
4868
end
@@ -53,6 +73,12 @@ def test_info_method
5373
assert_kind_of Vonage::Response, template_fragments.info(template_id: template_id, template_fragment_id: template_fragment_id)
5474
end
5575

76+
def test_info_method_with_basic_authentication
77+
stub_request(:get, template_fragment_uri).with(request(auth_method: basic_authorization)).to_return(response)
78+
79+
assert_kind_of Vonage::Response, template_fragments_with_basic_auth.info(template_id: template_id, template_fragment_id: template_fragment_id)
80+
end
81+
5682
def test_info_method_without_template_id
5783
assert_raises(ArgumentError) { template_fragments.info(template_fragment_id: template_fragment_id) }
5884
end
@@ -67,6 +93,12 @@ def test_create_method
6793
assert_kind_of Vonage::Response, template_fragments.create(template_id: template_id, channel: 'sms', locale: 'en-gb', text: 'Code: ${code}')
6894
end
6995

96+
def test_create_method_with_basic_authentication
97+
stub_request(:post, template_fragments_uri).with(request(body: { channel: 'sms', locale: 'en-gb', text: 'Code: ${code}' }, auth_method: basic_authorization)).to_return(response)
98+
99+
assert_kind_of Vonage::Response, template_fragments_with_basic_auth.create(template_id: template_id, channel: 'sms', locale: 'en-gb', text: 'Code: ${code}')
100+
end
101+
70102
def test_create_method_without_template_id
71103
assert_raises(ArgumentError) { template_fragments.create(channel: 'sms', locale: 'en-gb', text: 'Code: ${code}') }
72104
end
@@ -93,6 +125,12 @@ def test_update_method
93125
assert_kind_of Vonage::Response, template_fragments.update(template_id: template_id, template_fragment_id: template_fragment_id, text: 'Your code is: ${code}')
94126
end
95127

128+
def test_update_method_with_basic_authentication
129+
stub_request(:patch, template_fragment_uri).with(request(body: { text: 'Your code is: ${code}' }, auth_method: basic_authorization)).to_return(response)
130+
131+
assert_kind_of Vonage::Response, template_fragments_with_basic_auth.update(template_id: template_id, template_fragment_id: template_fragment_id, text: 'Your code is: ${code}')
132+
end
133+
96134
def test_update_method_without_template_id
97135
assert_raises(ArgumentError) { template_fragments.update(template_fragment_id: template_fragment_id, text: 'Your code is: ${code}') }
98136
end
@@ -111,6 +149,12 @@ def test_delete_method
111149
assert_kind_of Vonage::Response, template_fragments.delete(template_id: template_id, template_fragment_id: template_fragment_id)
112150
end
113151

152+
def test_delete_method_with_basic_authentication
153+
stub_request(:delete, template_fragment_uri).with(request(auth_method: basic_authorization)).to_return(response)
154+
155+
assert_kind_of Vonage::Response, template_fragments_with_basic_auth.delete(template_id: template_id, template_fragment_id: template_fragment_id)
156+
end
157+
114158
def test_delete_method_without_template_id
115159
assert_raises(ArgumentError) { template_fragments.delete(template_fragment_id: template_fragment_id) }
116160
end

0 commit comments

Comments
 (0)