diff --git a/lib/oauth2/access_token.rb b/lib/oauth2/access_token.rb index 57b95b10..e598d110 100644 --- a/lib/oauth2/access_token.rb +++ b/lib/oauth2/access_token.rb @@ -132,8 +132,9 @@ def no_tokens_warning(hash, key) # @option opts [FixNum, String] :expires_in (nil) the number of seconds in which the AccessToken will expire # @option opts [FixNum, String] :expires_at (nil) the epoch time in seconds in which AccessToken will expire # @option opts [FixNum, String] :expires_latency (nil) the number of seconds by which AccessToken validity will be reduced to offset latency, @version 2.0+ - # @option opts [Symbol] :mode (:header) the transmission mode of the Access Token parameter value - # one of :header, :body or :query + # @option opts [Symbol or callable] :mode (:header) the transmission mode of the Access Token parameter value: + # either one of :header, :body or :query, or a callable that accepts a request-verb parameter + # and returns one of these three symbols. # @option opts [String] :header_format ('Bearer %s') the string format to use for the Authorization header # @option opts [String] :param_name ('access_token') the parameter name to use for transmission of the # Access Token value in :body or :query transmission mode @@ -324,7 +325,7 @@ def to_hash # # @see OAuth2::Client#request def request(verb, path, opts = {}, &block) - configure_authentication!(opts) + configure_authentication!(opts, verb) @client.request(verb, path, opts, &block) end @@ -370,8 +371,9 @@ def headers private - def configure_authentication!(opts) - case options[:mode] + def configure_authentication!(opts, verb) + mode = options[:mode].respond_to?(:call) ? options[:mode].call(verb) : options[:mode] + case mode when :header opts[:headers] ||= {} opts[:headers].merge!(headers) @@ -389,7 +391,7 @@ def configure_authentication!(opts) end # @todo support for multi-part (file uploads) else - raise("invalid :mode option of #{options[:mode]}") + raise("invalid :mode option of #{mode}") end end diff --git a/spec/oauth2/access_token_spec.rb b/spec/oauth2/access_token_spec.rb index 2c032e4d..c7649904 100644 --- a/spec/oauth2/access_token_spec.rb +++ b/spec/oauth2/access_token_spec.rb @@ -411,6 +411,26 @@ def assert_initialized_token(target) end end + context "with verb-dependent mode" do + let(:mode) do + lambda do |verb| + case verb + when :get then :query + when :post, :delete then :header + when :put, :patch then :body + end + end + end + + let(:options) { {mode: mode} } + + VERBS.each do |verb| + it "correctly handles a #{verb.to_s.upcase}" do + expect(subject.__send__(verb, "/token/#{mode.call(verb)}").body).to include(token) + end + end + end + context "with client.options[:raise_errors] = false" do let(:options) { {raise_errors: false} }