4 Provides a thin wrapper around the excellent ruby-openid gem from JanRan. Be sure to install that first:
6 gem install ruby-openid
8 To understand what OpenID is about and how it works, it helps to read the documentation for lib/openid/consumer.rb
11 The specification used is http://openid.net/specs/openid-authentication-2_0.html.
17 OpenID authentication uses the session, so be sure that you haven't turned that off. It also relies on a number of
18 database tables to store the authentication keys. So you'll have to run the migration to create these before you get started:
20 rake open_id_authentication:db:create
22 Or, use the included generators to install or upgrade:
24 ./script/generate open_id_authentication_tables MigrationName
25 ./script/generate upgrade_open_id_authentication_tables MigrationName
27 Alternatively, you can use the file-based store, which just relies on on tmp/openids being present in RAILS_ROOT. But be aware that this store only works if you have a single application server. And it's not safe to use across NFS. It's recommended that you use the database store if at all possible. To use the file-based store, you'll also have to add this line to your config/environment.rb:
29 OpenIdAuthentication.store = :file
31 This particular plugin also relies on the fact that the authentication action allows for both POST and GET operations.
32 If you're using RESTful authentication, you'll need to explicitly allow for this in your routes.rb.
34 The plugin also expects to find a root_url method that points to the home page of your site. You can accomplish this by using a root route in config/routes.rb:
36 map.root :controller => 'articles'
38 This plugin relies on Rails Edge revision 6317 or newer.
44 This example is just to meant to demonstrate how you could use OpenID authentication. You might well want to add
45 salted hash logins instead of plain text passwords and other requirements on top of this. Treat it as a starting point,
48 Note that the User model referenced in the simple example below has an 'identity_url' attribute. You will want to add the same or similar field to whatever
49 model you are using for authentication.
51 Also of note is the following code block used in the example below:
53 authenticate_with_open_id do |result, identity_url|
57 In the above code block, 'identity_url' will need to match user.identity_url exactly. 'identity_url' will be a string in the form of 'http://example.com' -
58 If you are storing just 'example.com' with your user, the lookup will fail.
60 There is a handy method in this plugin called 'normalize_url' that will help with validating OpenID URLs.
62 OpenIdAuthentication.normalize_url(user.identity_url)
64 The above will return a standardized version of the OpenID URL - the above called with 'example.com' will return 'http://example.com/'
65 It will also raise an InvalidOpenId exception if the URL is determined to not be valid.
66 Use the above code in your User model and validate OpenID URLs before saving them.
70 map.root :controller => 'articles'
74 app/views/sessions/new.erb
76 <% form_tag(session_url) do %>
78 <label for="name">Username:</label>
79 <%= text_field_tag "name" %>
83 <label for="password">Password:</label>
84 <%= password_field_tag %>
92 <label for="openid_identifier">OpenID:</label>
93 <%= text_field_tag "openid_identifier" %>
97 <%= submit_tag 'Sign in', :disable_with => "Signing in…" %>
101 app/controllers/sessions_controller.rb
102 class SessionsController < ApplicationController
105 open_id_authentication
107 password_authentication(params[:name], params[:password])
113 def password_authentication(name, password)
114 if @current_user = @account.users.authenticate(params[:name], params[:password])
117 failed_login "Sorry, that username/password doesn't work"
121 def open_id_authentication
122 authenticate_with_open_id do |result, identity_url|
123 if result.successful?
124 if @current_user = @account.users.find_by_identity_url(identity_url)
127 failed_login "Sorry, no user by that identity URL exists (#{identity_url})"
130 failed_login result.message
138 session[:user_id] = @current_user.id
139 redirect_to(root_url)
142 def failed_login(message)
143 flash[:error] = message
144 redirect_to(new_session_url)
150 If you're fine with the result messages above and don't need individual logic on a per-failure basis,
151 you can collapse the case into a mere boolean:
153 def open_id_authentication
154 authenticate_with_open_id do |result, identity_url|
155 if result.successful? && @current_user = @account.users.find_by_identity_url(identity_url)
158 failed_login(result.message || "Sorry, no user by that identity URL exists (#{identity_url})")
164 Simple Registration OpenID Extension
165 ====================================
167 Some OpenID Providers support this lightweight profile exchange protocol. See more: http://www.openidenabled.com/openid/simple-registration-extension
169 You can support it in your app by changing #open_id_authentication
171 def open_id_authentication(identity_url)
172 # Pass optional :required and :optional keys to specify what sreg fields you want.
173 # Be sure to yield registration, a third argument in the #authenticate_with_open_id block.
174 authenticate_with_open_id(identity_url,
175 :required => [ :nickname, :email ],
176 :optional => :fullname) do |result, identity_url, registration|
179 failed_login "Sorry, the OpenID server couldn't be found"
181 failed_login "Sorry, but this does not appear to be a valid OpenID"
183 failed_login "OpenID verification was canceled"
185 failed_login "Sorry, the OpenID verification failed"
187 if @current_user = @account.users.find_by_identity_url(identity_url)
188 assign_registration_attributes!(registration)
193 failed_login "Your OpenID profile registration failed: " +
194 @current_user.errors.full_messages.to_sentence
197 failed_login "Sorry, no user by that identity URL exists"
203 # registration is a hash containing the valid sreg keys given above
204 # use this to map them to fields of your user model
205 def assign_registration_attributes!(registration)
206 model_to_registration_mapping.each do |model_attribute, registration_attribute|
207 unless registration[registration_attribute].blank?
208 @current_user.send("#{model_attribute}=", registration[registration_attribute])
213 def model_to_registration_mapping
214 { :login => 'nickname', :email => 'email', :display_name => 'fullname' }
217 Attribute Exchange OpenID Extension
218 ===================================
220 Some OpenID providers also support the OpenID AX (attribute exchange) protocol for exchanging identity information between endpoints. See more: http://openid.net/specs/openid-attribute-exchange-1_0.html
222 Accessing AX data is very similar to the Simple Registration process, described above -- just add the URI identifier for the AX field to your :optional or :required parameters. For example:
224 authenticate_with_open_id(identity_url,
225 :required => [ :email, 'http://schema.openid.net/birthDate' ]) do |result, identity_url, registration|
227 This would provide the sreg data for :email, and the AX data for 'http://schema.openid.net/birthDate'
231 Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license