Skip to content

Commit

Permalink
Add Client option for access_token_class (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonspalmer authored Jul 22, 2020
1 parent 5a750df commit b801b1e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/oauth2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,24 @@ class Client # rubocop:disable Metrics/ClassLength
# @option options [Boolean] :raise_errors (true) whether or not to raise an OAuth2::Error
# @option options [Logger] :logger (::Logger.new($stdout)) which logger to use when OAUTH_DEBUG is enabled
# on responses with 400+ status codes
# @option options [Class] :access_token_class (AccessToken) class used to create access tokens
# @yield [builder] The Faraday connection builder
def initialize(client_id, client_secret, options = {}, &block)
opts = options.dup
@id = client_id
@secret = client_secret
@site = opts.delete(:site)
ssl = opts.delete(:ssl)
@options = {:authorize_url => 'oauth/authorize',
:token_url => 'oauth/token',
:token_method => :post,
:auth_scheme => :basic_auth,
:connection_opts => {},
:connection_build => block,
:max_redirects => 5,
:raise_errors => true,
:logger => ::Logger.new($stdout)}.merge!(opts)
@options = {:authorize_url => 'oauth/authorize',
:token_url => 'oauth/token',
:token_method => :post,
:auth_scheme => :basic_auth,
:connection_opts => {},
:connection_build => block,
:max_redirects => 5,
:raise_errors => true,
:logger => ::Logger.new($stdout),
:access_token_class => AccessToken}.merge!(opts)
@options[:connection_opts][:ssl] = ssl if ssl
end

Expand Down Expand Up @@ -133,7 +135,7 @@ def request(verb, url, opts = {}) # rubocop:disable CyclomaticComplexity, Method
# @param access_token_opts [Hash] access token options, to pass to the AccessToken object
# @param access_token_class [Class] class of access token for easier subclassing OAuth2::AccessToken
# @return [AccessToken] the initialized AccessToken
def get_token(params, access_token_opts = {}, access_token_class = AccessToken) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
def get_token(params, access_token_opts = {}, access_token_class = options[:access_token_class]) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
# if ruby version >= 2.4
# params.transform_keys! do |key|
# RESERVED_PARAM_KEYS.include?(key) ? key.to_sym : key
Expand Down
14 changes: 14 additions & 0 deletions spec/oauth2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,20 @@
expect(token.token).to eq('the-token')
end

it 'returns a configured access token given by client.options[:access_token_class]' do
NewAccessToken = Class.new(AccessToken)
client = stubbed_client(access_token_class: NewAccessToken) do |stub|
stub.post('/oauth/token') do
[200, {'Content-Type' => 'application/json'}, MultiJson.encode('access_token' => 'the-token')]
end
end

token = client.get_token({})
expect(token).to be_a NewAccessToken
expect(token.token).to eq('the-token')
end


it 'authenticates with request parameters' do
client = stubbed_client(:auth_scheme => :request_body) do |stub|
stub.post('/oauth/token', 'client_id' => 'abc', 'client_secret' => 'def') do |env|
Expand Down

0 comments on commit b801b1e

Please sign in to comment.