#170
Jul 13, 2009

OpenID with Authlogic

Learn how to apply OpenID to an existing Authlogic setup as I show in this episode. This builds upon the app from episode 160.
Download (25.7 MB, 11:39)
alternative download for iPod & Apple TV (15.6 MB, 11:39)

Resources

sudo gem install ruby-openid authlogic-oid
script/plugin install git://github.com/rails/open_id_authentication.git
rake open_id_authentication:db:create
script/generate migration add_openid_identifier_to_users openid_identifier:string
rake db:migrate
# config/environment.rb
config.gem "authlogic-oid", :lib => "authlogic_openid"
config.gem "ruby-openid", :lib => "openid"

# models/user.rb
acts_as_authentic do |c|
  c.openid_required_fields = [:nickname, :email]
end

private

def map_openid_registration(registration)
  self.email = registration["email"] if email.blank?
  self.username = registration["nickname"] if username.blank?
end

# user_sessions_controller.rb
def create
  @user_session = UserSession.new(params[:user_session])
  @user_session.save do |result|
    if result
      flash[:notice] = "Successfully logged in."
      redirect_to root_url
    else
      render :action => 'new'
    end
  end
end

# users_controller.rb
def create
  @user = User.new(params[:user])
  @user.save do |result|
    if result
      flash[:notice] = "Registration successful."
      redirect_to root_url
    else
      render :action => 'new'
    end
  end
end

def update
  @user = current_user
  @user.attributes = params[:user]
  @user.save do |result|
    if result
      flash[:notice] = "Successfully updated profile."
      redirect_to root_url
    else
      render :action => 'edit'
    end
  end
end
<!-- users/_form.html.erb -->
<% if @user.openid_identifier.blank? %>
  <p>
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </p>
  <p>
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </p>
  <p><%= f.submit "Submit" %></p>

  <h2>Or use OpenID</h2>
<% end %>
<p>
  <%= f.label :openid_identifier, "OpenID URL" %><br />
  <%= f.text_field :openid_identifier %>
</p>
<p><%= f.submit "Submit" %></p>

<!-- user_sessions/new.html.erb -->
<h2>Or use OpenID</h2>
<p>
  <%= f.label :openid_identifier, "OpenID URL" %><br />
  <%= f.text_field :openid_identifier %>
</p>
<p><%= f.submit "Submit" %></p>
/* embeds the openid image in the text field */
input#user_openid_identifier, input#user_session_openid_identifier {
  background: url(http://openid.net/images/login-bg.gif) no-repeat;
  background-color: #fff;
  background-position: 0 50%;
  color: #000;
  padding-left: 18px;
}

RSS Feed for Episode Comments 49 comments

1. Madan Kumar Rajan Jul 13, 2009 at 00:01

Hi Ryan,

Amazing video... Keep it up.

Thanks,
Madan Kumar Rajan


2. millisami Jul 13, 2009 at 00:07

Awesome as always.
Keep rocking.


3. David Jul 13, 2009 at 00:22

email fetch works with yahoo openid?


4. Igor Jul 13, 2009 at 00:29

Hi, Ryan

Thank you for another one god screencast!

Had you any experience with single sign on with Shibboleth? Is it makes sense to do screencast about it?

Thanks,
Igor


5. Tobias Svensson Jul 13, 2009 at 02:03

Your timing couldn't have been better as I was just about to play around with OpenID for Authlogic today. Thanks a lot ;)


6. josh Jul 13, 2009 at 13:22

We've seen quite a few episodes about authentication, I think that authorization is an important followup (it's a little daunting to me).


7. Blake Chambers Jul 13, 2009 at 14:31

Great information. I had to migrate in something like this:
change_column :users, :crypted_password, :string, :default => nil, :null => true
    change_column :users, :password_salt, :string, :default => nil, :null => true

If I didn't, mysql would complain about a blank password.


8. jfmiller28 Jul 13, 2009 at 20:06

Wonderful wonderful screencast!

I would also like to see a screen cast on being an openID provider


9. Ben Johnson Jul 13, 2009 at 21:44

Hey Ryan, great video once again. Thanks a lot for doing this, this has helped me out tremendously with answering questions and helping people get started.


10. Gavin Jul 14, 2009 at 04:23

Perfect timing!!

Thanks Ryan


11. Jeff Jul 14, 2009 at 07:10

Yep, very good and very useful.

I second the request for some authorization casts!

Preferably using one of the frameworks like acl9 (which works quite nicely with authlogic)


12. Jeff Jul 14, 2009 at 07:23

Another great thing would be to include how to test an openid login ( using cucumber for example )


13. Brett Jul 14, 2009 at 15:51

Is the openid_identifier field also required on the UserSession table? I'm getting an error when I add the field to the login view.

http://gist.github.com/147267


14. Joe Jul 14, 2009 at 20:57

@brett same here, I get:

undefined method `openid_identifier' for #<UserSession: no credentials provided>


15. Michael Hasenstein Jul 15, 2009 at 00:41

I got it working 50% - but after returning from the OpenID provider I'm shown the registration form with two validation errors for login and email (too short) - they are empty. There is an OpenID URL for the user (https://me.yahoo.com/a/RXqidPob1_KpF0cfyeR.cycJcHax9.s). I cannot see anything wrong, views, models, controllers, are all simple enough after all... any ideas?


16. Michael Hasenstein Jul 15, 2009 at 00:55

Follow-up: in map_openid_registration(registration) "registration" is an empty hash - so THAT's the problem. But why???


17. Jeremy Weiskotten Jul 15, 2009 at 04:56

Very nice, Ryan. I recently started working on Skeletor, a skeletal Rails 2.3 app that uses Authlogic with OpenID and other common plugins/gems.

It's not polished yet, but I think it's a good start and I'm looking for feedback from the community.

http://github.com/jeremyw/skeletor/tree/master


18. Brett Jul 15, 2009 at 08:22

OK, fixed - need to set config.action_controller.session in the environment.rb


19. Cathal Jul 15, 2009 at 08:55

Hi Ryan,

Do you plan to cover ldap authentication through Authlogic too?


20. Joe Jul 15, 2009 at 09:51

Ahh, i figured something important out, and this may help a few of you, many of the openid providers require you to explicitly choose to have openid make a username and email address available. By default most of them only return the a valid credentials response and the token.

For myopenid, got into your profile


21. arie Jul 16, 2009 at 09:26

Nice. Requesting railscasts on Facebook Connect and Sign in with Twitter.


22. captproton Jul 17, 2009 at 15:29

@arie until Ryan gets time, here's a slideshow: http://www.slideshare.net/mbleigh/twitter-on-rails-1396030


23. captproton Jul 17, 2009 at 15:31

or for those who don't want to copy and paste: <a href="http://www.slideshare.net/mbleigh/twitter-on-rails-1396030">http://www.slideshare.net/mbleigh/twitter-on-rails-1396030</a>


24. cbmeeks Jul 18, 2009 at 17:46

Awesome tutorial as usual.

However, I can't get it to work with Yahoo, Google (and blogspot), etc.

It works with AOL (once I told it to export username/email). It works with myid.net. Etc.

But even though Yahoo and Google ask if I want to allow it (and I do), they won't return the nickname and email back so I am still getting a "Username/Email Required" error.

Any clues?

Thanks


25. Dan Jul 18, 2009 at 18:47

Ryan,

How about an episode that goes over a simple single sign-on system built in rails for multiple rails apps.

Thanks for all the great screencasts!


26. jd Jul 19, 2009 at 13:19

Thank you for the scast.

Setting "background: url(http://openid.net/images/login-bg.gif) no-repeat;" as a direct link on openid server may lead to overload. Think about using a local picture.


27. Francis Fish Jul 21, 2009 at 14:25

Oauth and authlogic are covered really well in the documentation at http://github.com/jrallison/authlogic_oauth/tree/master. Ryan tends to plug gaps of stuff that you can't find anywhere else, I can't see the point in him reading the documentation there out into a screen cast myself.


28. Trevor Jul 26, 2009 at 15:02

@Brett, set config.action_controller.session to what? I'm getting the same error as Bret and Joe. And I'm a rails noob :(


29. Trevor Jul 26, 2009 at 16:34

Quitting script/server and relaunching seemed to fix this problem. I also did sudo gem install rails just in case, but I'm not sure if that's what did it. Also, you can find the proper config.action_controller.session codes in the source code linked above.


30. Kenton Sep 06, 2009 at 06:52

I've tried setting this up on a test site using Authlogic but keep getting the following error once I return from myopenid.com:

------
Openid identifier did not match any users in our database, have you set up your account to use OpenID?
-----

I've looked at the README for the plugin, on Google and here but I'm stumped.

Any ideas?

Thanks.


31. Kenton Sep 06, 2009 at 08:19

Scratch that last comment...figured it out.

(1) I was trying to "login" using my OpenID credentials before "registering". Gotta register first, then you can use OpenID to login during subsequent visits to the site.

(2) For registration, I had the crypted_password and password_salt columns in the user model set to :null => false. But using OpenID, those don't get created which throws a database error. A simple migration using the following fixed that:

http://pastie.org/607719

Once I fixed that and registered for the site FIRST, it worked just fine.


32. StevenW Sep 15, 2009 at 16:36

Just so you know the plugin command that you have supplied doesn't seem to work any more. Rails says that the plugin installs but when you try to run the rake command rails can't find it. I located another plugin from Google Code that seems to work

script/plugin install http://open-id-authentication.googlecode.com/svn/trunk/open_id_authentication/


33. Nikesha Sep 21, 2009 at 15:31

Вот бы еще по русски.


34. evindha Oct 15, 2009 at 00:03

Hi Ryan, I'm getting error when i run rake open_id_authentication:db:create. The error is :
undefined method `add_acts_as_authentic_module' for ActiveRecord::Base:Class
Anyone help me?


35. tiago Oct 25, 2009 at 07:27

I followed you guide and its amazing, thank you very much.
But i found some problems when trying to loggin in.
If I choose an openid that doesnt exists, it fails for "Template is missing" in the user_sessions/create.erb.
And if I try to login with a wrong username/password, it no longer shows the error message about it, just reloads the view.
Any idea how could I fix this?


36. ililu Oct 29, 2009 at 03:16

to Nikesha, +1!!! ^)


37. wholesale nike shoes Jan 13, 2010 at 23:09

A very good article, I will always come in.


38. wholesale scarves Jan 13, 2010 at 23:09

Such a good article, caught my sympathy!
-


39. kit Jan 17, 2010 at 01:17

hrm, a bit stuck here, the rake task open_id_authentication:db:create doesn't appear to exist.

I have the authlogic, authlogic-oid and ruby-openid gems installed.

If anyone comes across the error "llformed requirement ["=>0.2.1"]", there's a typo in the latest version of open_id_authentication.
Change

    :version => '=>0.2.1'
to
    :version => '>=0.2.1'


40. kit Jan 17, 2010 at 01:35

It looks like rails/open_id_authentication no longer provides a rakefile or the model generators. The repo in your video had 38 files, but the current version only has 11.

Any thoughts on where these have gone?


41. pd Jan 18, 2010 at 01:52

i'm confused by your routes. you say that builds upon screencast 160. but in 160, you haven't used "session" but "user_session". is this the same?


42. pd Jan 18, 2010 at 01:53

@kit that's the problem when using gem in environment.rb. i also wondered about this.

install the plugin and gem by hand like you see in the video and they appear.

a little bit ugly since you enter it in environment.rb and install it with rake gems:install, but you still have to set up them manually.


43. Marc Luzietti Jan 18, 2010 at 12:09

The rakefile and model generators are no longer part of the plugin. They were removed several versions ago.


44. Marc Luzietti Jan 18, 2010 at 12:17

You can find the "missing" files here: http://github.com/rails/open_id_authentication/commit/16e40d2ee73f8f10c2cb77234b6f5f4816c94547#diff-0

It appears they are just a bunch of tests, so you can skip "rake open_id_authentication:db:create"


45. Marc Luzietti Jan 18, 2010 at 13:10

Maybe not. Looking at the migrations and the schema, there's a lot more going on than just the tests I see in the missing rakefile.


46. Marc Luzietti Jan 18, 2010 at 13:30

You can find the old files Ryan uses here: http://github.com/ryanb/railscasts-episodes/tree/master/episode-170/blog/vendor/plugins/open_id_authentication/


47. Abel Feb 17, 2010 at 00:50

@Marc Luzietti: I'm going crazy trying to set this up. Why is that rake file missing from the main repository. Even in the homepage for the project it says that you need that particular task "rake open_id_authentication:db:create".


48. Doug Feb 26, 2010 at 15:38

The following commit seems to indicate the database migrations are no longer needed.

http://github.com/rails/open_id_authentication/commit/ee5be2f26c37049f6c37e113627a34c1ef6d35cd


49. Louis Vuitton handbags Mar 09, 2010 at 19:19

railscasts.com/episodes/170-openid-with-authlogic

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source