Setting up rails3 as an OAuth2 provider

125 views
Skip to first unread message

Henrik Berggren

unread,
Oct 5, 2010, 6:24:15 AM10/5/10
to OAuth Ruby
Hey everyone, and especially you Pelle.

I'm just getting started with setting up a new rails3 app with oauth
and since you wanted help testing stuff out let's do it.

First of all, i'm using your oauth-plugin (rails3 branch) as well as
your oauth gem (pelle-oauth, master). I'm also using Devise for
regular authentication, might be good to know.

The first hurdle is the controller specs. It throws this error:
provider_controller.rb:8:in `included': undefined method
`oauthenticate' for OauthController:Class (NoMethodError)

Is that the same error as you're getting?

Next step is to set up a test case for authentication. I'll let you
know how it goes.

Henrik Berggren

unread,
Oct 6, 2010, 8:29:46 AM10/6/10
to OAuth Ruby
Ok, i've currently got all the specs running. This is basically what i
had to do:

- First of all i had to include the ApplicationControllerMethods into
application_controller.rb

require 'oauth/controllers/application_controller_methods'
include OAuth::Controllers::ApplicationControllerMethods

- Then i added fixtures for two users, aaron & quentin. I called it
users.yml and put it in spec/fixtures. Make sure they have "password"
as their password, or you have to change some of the specs.

- Then i ran rake:db migrate for development & test

- Since i use Devise, "before_filter login_required" in
OauthorizedController which resides in oauth_controller_spec.rb was
wrong. I changed it to "before_filter authenticate_user!". I'm a bit
unsure if this is right but it makes the specs pass. Also, if you use
something other than Devise, change the before_filer accordingly.

- Lot's of specs fail due to missing routes for the Oauthorized
controller (which is only for testing purposes and should not be
included in routes.rb). I'm sure there is a cleaner way but my
solution is to draw a new set of routes before the specs for that
controller is run. Something like:

before do
MyApp::Application.routes.draw do
get '/interactive', :to => "oauthorized#interactive"
# add all routes needed here
end
end

You also have to add an after hook to make sure the rest of your tests
pass.

after do
MyApp::Application.reload_routes!
end

- Then i had to add an authenticate mehod for the User class. It takes
two arguments, i'm using email & password. It hould return a user if
it passes and nil if not.

- This method is then also used in the authenticate_user method that i
uncommented in oauth_controller.rb

- In Devise the logged_in? method is called user_signed_in? so I
created an alias for logged_in? in application_controller.rb

- I then added some relationships in user.rb to match the new classes
created.

has_many :client_applications
has_many :tokens, :class_name => 'OauthToken'

- One route was missing in the generator so i added it to routes.rb

match '/invalidate', :to => 'oauth#invalidate', :as
=> :invalidate


Onwards to creating a test app!

Henrik
Reply all
Reply to author
Forward
0 new messages