From 2037b382e79eae974a67ebe96188e755e88fd3ad Mon Sep 17 00:00:00 2001 From: Mark James Date: Mon, 8 Sep 2025 15:56:52 +1000 Subject: [PATCH 1/2] Add verb-dependent token mode --- lib/oauth2/access_token.rb | 14 ++++++++------ spec/oauth2/access_token_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) 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..3d81d263 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:} } + + 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} } From 4c6907b7d4f395b99079d5d5bacdfc7223c48ed7 Mon Sep 17 00:00:00 2001 From: Mark James Date: Mon, 8 Sep 2025 16:26:42 +1000 Subject: [PATCH 2/2] Add hash value to spec to support ruby < 3.1 --- spec/oauth2/access_token_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/oauth2/access_token_spec.rb b/spec/oauth2/access_token_spec.rb index 3d81d263..c7649904 100644 --- a/spec/oauth2/access_token_spec.rb +++ b/spec/oauth2/access_token_spec.rb @@ -422,8 +422,8 @@ def assert_initialized_token(target) end end - let(:options) { {mode:} } - + 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)