pelle / oauth-plugin

Rails plugin for OAuth
http://stakeventures.com/articles/2009/07/21/consuming-oauth-intelligently-in-rails
MIT License
715 stars 215 forks source link

Consumer Issue with versions >= 0.4.0 pre-3 (Mongoid) #32

Open 3en opened 13 years ago

3en commented 13 years ago

I noticed this issue appears in 0.4.0 pre-3 and in 0.4.0 pre-4 with Mongoid. For debugging purposes I am using 0.4.0 pre-4.

I destroyed and re-generated oauth_consumer

The following error occurs even with the newly generated oauth_consumer_controller:

Unknown action
The action 'callback' could not be found for OauthConsumersController

If I add (to oauth_consumer_controller):

def callback
    super
end

The error results in:

can't convert Hash into Integer

app/models/consumer_token.rb:26:in `find_or_create_from_access_token'
app/controllers/oauth_consumers_controller.rb:16:in `callback'

Also just to double check as I belive this is not documented anywhere but looking at the generated ConsumerToken model I have defined the Mongoid relationship in User as "embeds_many :consumer_tokens". I believe this is right.

Any idea what's causing this. I believe it's only a problem since 0.4.0 pre-3

3en commented 13 years ago

Okay, I managed to work it out.

There are two things that I believe is needed to make everything word:

1) Call back needs to be in /app/controllers/oauth_consumers_controller.rb def callback super end

2) Mongoid Queries in ConsumerToken in the class find_or_create_from_access_token need to be rewritten (in the file: oauth-plugin/lib/ generators/mongoid/oauth_consumer_templates/consumer_token.rb)

Old - Line 24

      user.consumer_tokens.first(:conditions=>{:_type=>self.to_s,:token=>access_token.token}) ||
# New - Line 24
user.consumer_tokens.where(:_type=>self.to_s,:token=>access_token.token).first

And also from the same file

# Old - Line 29
user = User.first(:conditions=>{:_type=>self.to_s,"consumer_tokens.token"=>access_token.token})

# New - Line 29
user =  User.where("consumer_tokens._type"=>self.to_s,"consumer_tokens.token"=>access_token.token).first

The "where" queries use what's called the Criteria API in Mongoid, which according to the documentation is "preferred method of querying the database" http://mongoid.org/docs/querying/

I will attempt to fork, patch, commit, push these changes. This is the first time I will be submitting a change to a gem :-)

Could you confirm that the callback method in oauth_consumer_controller is necessary? Can't think whats the best way to fix this, but it works for me.

3en commented 13 years ago

Also I've just come across another issue. Need to clarify if you want ConsumerToken model to be embedded into the User model or referenced instead. Referencing works more like the traditional associations in SQL databases (data kept in separate collection/table).

Looks like having ConsumerToken referenced instead of embedded will work better as it means less code differing from the ActiveRecord implementation. But if you want it to be an embedded association to User then stuff like load_consumer in OauthConsumersController class needs to be overwritten for Mongoid.

pelle commented 13 years ago

I think embedded is the best case. But it could be done in a non embedded version.

I changed the mongoid generator to hopefully fix the issue you're seeing. Could you please test it:

https://github.com/pelle/oauth-plugin/commit/f805e8c359b18bd0254eaa774171ff7938ee8165

As the fix is in the generator you can just paste this over your existing consumer token:

https://github.com/pelle/oauth-plugin/blob/f805e8c359b18bd0254eaa774171ff7938ee8165/lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb