Setting OAUTH2 Headers #111

Closed
tomrossi7 opened this Issue Nov 1, 2013 · 4 comments

Comments

Projects
None yet
4 participants

With previous versions of ActiveResource, the OAUTH2 headers could be set like this:

  class Resource < ActiveResource::Base
  end

  class MyClass1 < Resource
  end

  class MyClass2 < Resource
  end

The headers could then be set:

Resource.headers['authorization'] = 'Bearer ' + my_oauth2_token

My problem with the latest ActiveResource is I can change the Resource.headers and each class will inherit correctly, until I try to set the headers to something else. If I update Resource.headers['authorization'], the other classes (MyClass1, MyClass2)) will still have the old token. Does that make sense?

lhm commented Oct 16, 2014

I'm having the same problem. I'm trying to pass on Rails' request-id as header to a backend server, but this only works once. What is the correct way to pass dynamic headers to ActiveResource?

@lhm at some point I figured this out, but now I don't totally remember. The problem is related to where ActiveResource is drawing the headers hash from. It took a lot of debugging, but the solution somehow involved this method:

  class MyClass1
      # If headers are not defined in a given subclass, then obtain
      # headers from the superclass.
      def self.headers
        if self == MyClass1
          @headers ||= {}
        else
          superclass.headers.merge({})
        end
      end
  end

lhm commented Oct 20, 2014

The problem appears to be that in AR::Base the merged headers hash is being stored in a thread local variable of the subclass. The stored value will then take precedence over a changed value in the parent class. See https://github.com/rails/activeresource/blob/master/lib/active_resource/base.rb#L607

Here is a failing test case that shows the behavior.

I'm currently not sure how this could be fixed in a backwards-compatible way. Not storing the value in the child class will prevent changing headers in the child in general. A solution that I could think of would involve splitting the headers method into a setter and getter, but that will obviously break existing code.

There is a PR for adding thread-safe class attributes to ActiveResource that might be helpful, but there hasn't been any activity on that lately: #120

rails-bot bot added the stale label May 5, 2017

rails-bot bot commented May 5, 2017

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.

If it is an issue and you can still reproduce this error on the master branch,
please reply with all of the information you have about it in order to keep the issue open.

If it is a pull request and you are still interested on having it merged please make sure it can be merged clearly.

Thank you for all your contributions.

Owner

rafaelfranca commented May 5, 2017

This issue has been automatically marked as stale because it has not been commented on for at least three months.

The resources of the Rails team are limited, and so we are asking for your help.

If it is an issue and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.

If it is a pull request and you are still interested on having it merged please make sure it can be merged clearly.

Thank you for all your contributions.

rails-bot bot closed this May 13, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment