Skip to content

Commit f9a0985

Browse files
authored
Merge pull request #93 from nov/feature/custom_header_support
Feature/custom header support
2 parents 6796b39 + f2c129b commit f9a0985

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

lib/rack/oauth2/access_token.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class AccessToken
55
attr_required :access_token, :token_type
66
attr_optional :refresh_token, :expires_in, :scope
77
attr_accessor :raw_attributes
8-
delegate :get, :patch, :post, :put, :delete, to: :httpclient
8+
delegate :get, :patch, :post, :put, :delete, to: :http_client
99

1010
alias_method :to_s, :access_token
1111

@@ -18,8 +18,8 @@ def initialize(attributes = {})
1818
attr_missing!
1919
end
2020

21-
def httpclient
22-
@httpclient ||= Rack::OAuth2.http_client("#{self.class} (#{VERSION})") do |faraday|
21+
def http_client
22+
@http_client ||= Rack::OAuth2.http_client("#{self.class} (#{VERSION})") do |faraday|
2323
Authenticator.new(self).authenticate(faraday)
2424
end
2525
end

lib/rack/oauth2/access_token/mtls.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class MTLS < Bearer
77
def initialize(attributes = {})
88
super
99
self.token_type = :bearer
10-
httpclient.ssl.client_key = private_key
11-
httpclient.ssl.client_cert = certificate
10+
http_client.ssl.client_key = private_key
11+
http_client.ssl.client_cert = certificate
1212
end
1313
end
1414
end

lib/rack/oauth2/client.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def force_token_type!(token_type)
6868
@forced_token_type = token_type.to_s
6969
end
7070

71-
def access_token!(*args)
72-
headers, params, http_client, options = authenticated_context_from(*args)
71+
def access_token!(*args, &local_http_config)
72+
headers, params, http_client, options = authenticated_context_from(*args, &local_http_config)
7373
params[:scope] = Array(options.delete(:scope)).join(' ') if options[:scope].present?
7474
params.merge! @grant.as_json
7575
params.merge! options
@@ -82,8 +82,8 @@ def access_token!(*args)
8282
end
8383
end
8484

85-
def revoke!(*args)
86-
headers, params, http_client, options = authenticated_context_from(*args)
85+
def revoke!(*args, &local_http_config)
86+
headers, params, http_client, options = authenticated_context_from(*args, &local_http_config)
8787

8888
params.merge! case
8989
when access_token = options.delete(:access_token)
@@ -126,9 +126,9 @@ def absolute_uri_for(endpoint)
126126
_endpoint_.to_s
127127
end
128128

129-
def authenticated_context_from(*args)
129+
def authenticated_context_from(*args, &local_http_config)
130130
headers, params = {}, {}
131-
http_client = Rack::OAuth2.http_client
131+
http_client = Rack::OAuth2.http_client(&local_http_config)
132132

133133
# NOTE:
134134
# Using Array#extract_options! for backward compatibility.

spec/rack/oauth2/client_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,23 @@
309309
end
310310
end
311311

312+
context 'local_http_config handling' do
313+
it do
314+
mock_response(
315+
:post,
316+
'https://server.example.com/oauth2/token',
317+
'tokens/bearer.json',
318+
request_header: {
319+
'Authorization' => 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=',
320+
'X-Foo' => 'bar'
321+
}
322+
)
323+
client.access_token! do |request|
324+
request.headers.merge! 'X-Foo' => 'bar'
325+
end
326+
end
327+
end
328+
312329
context 'when bearer token is given' do
313330
before do
314331
client.authorization_code = 'code'
@@ -433,6 +450,28 @@
433450
end
434451

435452
describe '#revoke!' do
453+
context 'local_http_config handling' do
454+
it do
455+
mock_response(
456+
:post,
457+
'https://server.example.com/oauth2/revoke',
458+
'blank',
459+
status: 200,
460+
body: {
461+
token: 'access_token',
462+
token_type_hint: 'access_token'
463+
},
464+
request_header: {
465+
'Authorization' => 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=',
466+
'X-Foo' => 'bar'
467+
}
468+
)
469+
client.revoke!(access_token: 'access_token') do |request|
470+
request.headers.merge! 'X-Foo' => 'bar'
471+
end
472+
end
473+
end
474+
436475
context 'when access_token given' do
437476
before do
438477
mock_response(

0 commit comments

Comments
 (0)