Skip to content

Commit 9bb815b

Browse files
authored
SWI-12283 Allow Setting Custom Token URL (#223)
* SWI-12283 Allow Setting Custom Token URL * update unit tests to not hit live oauth url
1 parent 4cf4a79 commit 9bb815b

14 files changed

Lines changed: 24 additions & 28 deletions

‎custom_templates/configuration.mustache‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ module {{moduleName}}
6565
# Defines the access token (Bearer) used with OAuth2.
6666
attr_accessor :access_token
6767

68+
# Defines the URL used to obtain access tokens (Bearer) used with OAuth2.
69+
# @return [String]
70+
attr_accessor :access_token_url
71+
6872
# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
6973
# Overrides the access_token if set
7074
# @return [Proc]
@@ -175,16 +179,16 @@ module {{moduleName}}
175179
{{#authMethods}}
176180
{{#isOAuth}}
177181
@access_token_expiration = nil
182+
@access_token_url = '{{tokenUrl}}'
178183
@access_token_getter = Proc.new {
179184
access_token_valid = @access_token && (@access_token_expiration.nil? || @access_token_expiration > Time.now + 60)
180185
next @access_token if access_token_valid
181186
next unless @client_id && @client_secret
182187
183188
puts "Refreshing access token..." if @debugging
184189
# obtain new access token using client credentials
185-
token_url = '{{tokenUrl}}'
186190
auth_header = 'Basic ' + ["#{@client_id}:#{@client_secret}"].pack('m').delete("\r\n")
187-
conn = Faraday.new(url: token_url) do |faraday|
191+
conn = Faraday.new(url: @access_token_url) do |faraday|
188192
faraday.request :url_encoded
189193
faraday.adapter Faraday.default_adapter
190194
end

‎lib/bandwidth-sdk/configuration.rb‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class Configuration
7272
# Defines the access token (Bearer) used with OAuth2.
7373
attr_accessor :access_token
7474

75+
# Defines the URL used to obtain access tokens (Bearer) used with OAuth2.
76+
# @return [String]
77+
attr_accessor :access_token_url
78+
7579
# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
7680
# Overrides the access_token if set
7781
# @return [Proc]
@@ -193,16 +197,16 @@ def initialize
193197
@force_ending_format = false
194198
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
195199
@access_token_expiration = nil
200+
@access_token_url = 'https://api.bandwidth.com/api/v1/oauth2/token'
196201
@access_token_getter = Proc.new {
197202
access_token_valid = @access_token && (@access_token_expiration.nil? || @access_token_expiration > Time.now + 60)
198203
next @access_token if access_token_valid
199204
next unless @client_id && @client_secret
200205

201206
puts "Refreshing access token..." if @debugging
202207
# obtain new access token using client credentials
203-
token_url = 'https://api.bandwidth.com/api/v1/oauth2/token'
204208
auth_header = 'Basic ' + ["#{@client_id}:#{@client_secret}"].pack('m').delete("\r\n")
205-
conn = Faraday.new(url: token_url) do |faraday|
209+
conn = Faraday.new(url: @access_token_url) do |faraday|
206210
faraday.request :url_encoded
207211
faraday.adapter Faraday.default_adapter
208212
end

‎spec/unit/api/calls_api_spec.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
before(:all) do
66
Bandwidth.configure do |config|
77
config.debugging = true
8-
config.username = BW_USERNAME
9-
config.password = BW_PASSWORD
8+
config.access_token = 'abcd1234'
109
config.ignore_operation_servers = true
1110
config.host = '127.0.0.1:4010'
1211
end

‎spec/unit/api/conferences_api_spec.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
before(:all) do
88
Bandwidth.configure do |config|
99
config.debugging = true
10-
config.username = BW_USERNAME
11-
config.password = BW_PASSWORD
10+
config.access_token = 'abcd1234'
1211
config.ignore_operation_servers = true
1312
config.return_binary_data = true
1413
config.host = '127.0.0.1:4010'

‎spec/unit/api/endpoints_api_spec.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
before(:all) do
66
Bandwidth.configure do |config|
77
config.debugging = true
8-
config.username = BW_USERNAME
9-
config.password = BW_PASSWORD
8+
config.access_token = 'abcd1234'
109
config.ignore_operation_servers = true
1110
config.host = '127.0.0.1:4010'
1211
end

‎spec/unit/api/media_api_spec.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
before(:all) do
88
Bandwidth.configure do |config|
99
config.debugging = true
10-
config.username = BW_USERNAME
11-
config.password = BW_PASSWORD
10+
config.access_token = 'abcd1234'
1211
config.ignore_operation_servers = true
1312
config.return_binary_data = true
1413
config.host = '127.0.0.1:4010'

‎spec/unit/api/messages_api_spec.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
before(:all) do
44
Bandwidth.configure do |config|
55
config.debugging = true
6-
config.username = BW_USERNAME
7-
config.password = BW_PASSWORD
6+
config.access_token = 'abcd1234'
87
config.ignore_operation_servers = true
98
config.host = '127.0.0.1:4010'
109
end

‎spec/unit/api/mfa_api_spec.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
before(:all) do
77
Bandwidth.configure do |config|
88
config.debugging = true
9-
config.username = BW_USERNAME
10-
config.password = BW_PASSWORD
9+
config.access_token = 'abcd1234'
1110
config.ignore_operation_servers = true
1211
config.host = '127.0.0.1:4010'
1312
end

‎spec/unit/api/multi_channel_api_spec.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
before(:all) do
44
Bandwidth.configure do |config|
55
config.debugging = true
6-
config.username = BW_USERNAME
7-
config.password = BW_PASSWORD
6+
config.access_token = 'abcd1234'
87
config.ignore_operation_servers = true
98
config.host = '127.0.0.1:4010'
109
end

‎spec/unit/api/phone_number_lookup_api_spec.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
before(:all) do
88
Bandwidth.configure do |config|
99
config.debugging = true
10-
config.username = BW_USERNAME
11-
config.password = BW_PASSWORD
10+
config.access_token = 'abcd1234'
1211
config.ignore_operation_servers = true
1312
config.host = '127.0.0.1:4010'
1413
end

0 commit comments

Comments
 (0)