Chris@0: OpenIdAuthentication Chris@0: ==================== Chris@0: Chris@0: Provides a thin wrapper around the excellent ruby-openid gem from JanRan. Be sure to install that first: Chris@0: Chris@0: gem install ruby-openid Chris@0: Chris@0: To understand what OpenID is about and how it works, it helps to read the documentation for lib/openid/consumer.rb Chris@0: from that gem. Chris@0: Chris@0: The specification used is http://openid.net/specs/openid-authentication-2_0.html. Chris@0: Chris@0: Chris@0: Prerequisites Chris@0: ============= Chris@0: Chris@0: OpenID authentication uses the session, so be sure that you haven't turned that off. It also relies on a number of Chris@0: database tables to store the authentication keys. So you'll have to run the migration to create these before you get started: Chris@0: Chris@0: rake open_id_authentication:db:create Chris@0: Chris@0: Or, use the included generators to install or upgrade: Chris@0: Chris@0: ./script/generate open_id_authentication_tables MigrationName Chris@0: ./script/generate upgrade_open_id_authentication_tables MigrationName Chris@0: Chris@0: 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: Chris@0: Chris@0: OpenIdAuthentication.store = :file Chris@0: Chris@0: This particular plugin also relies on the fact that the authentication action allows for both POST and GET operations. Chris@0: If you're using RESTful authentication, you'll need to explicitly allow for this in your routes.rb. Chris@0: Chris@0: 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: Chris@0: Chris@0: map.root :controller => 'articles' Chris@0: Chris@0: This plugin relies on Rails Edge revision 6317 or newer. Chris@0: Chris@0: Chris@0: Example Chris@0: ======= Chris@0: Chris@0: This example is just to meant to demonstrate how you could use OpenID authentication. You might well want to add Chris@0: salted hash logins instead of plain text passwords and other requirements on top of this. Treat it as a starting point, Chris@0: not a destination. Chris@0: Chris@0: 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 Chris@0: model you are using for authentication. Chris@0: Chris@0: Also of note is the following code block used in the example below: Chris@0: Chris@0: authenticate_with_open_id do |result, identity_url| Chris@0: ... Chris@0: end Chris@0: Chris@0: 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' - Chris@0: If you are storing just 'example.com' with your user, the lookup will fail. Chris@0: Chris@0: There is a handy method in this plugin called 'normalize_url' that will help with validating OpenID URLs. Chris@0: Chris@0: OpenIdAuthentication.normalize_url(user.identity_url) Chris@0: Chris@0: The above will return a standardized version of the OpenID URL - the above called with 'example.com' will return 'http://example.com/' Chris@0: It will also raise an InvalidOpenId exception if the URL is determined to not be valid. Chris@0: Use the above code in your User model and validate OpenID URLs before saving them. Chris@0: Chris@0: config/routes.rb Chris@0: Chris@0: map.root :controller => 'articles' Chris@0: map.resource :session Chris@0: Chris@0: Chris@0: app/views/sessions/new.erb Chris@0: Chris@0: <% form_tag(session_url) do %> Chris@0:

Chris@0: Chris@0: <%= text_field_tag "name" %> Chris@0:

Chris@0: Chris@0:

Chris@0: Chris@0: <%= password_field_tag %> Chris@0:

Chris@0: Chris@0:

Chris@0: ...or use: Chris@0:

Chris@0: Chris@0:

Chris@0: Chris@0: <%= text_field_tag "openid_identifier" %> Chris@0:

Chris@0: Chris@0:

Chris@0: <%= submit_tag 'Sign in', :disable_with => "Signing in…" %> Chris@0:

Chris@0: <% end %> Chris@0: Chris@0: app/controllers/sessions_controller.rb Chris@0: class SessionsController < ApplicationController Chris@0: def create Chris@0: if using_open_id? Chris@0: open_id_authentication Chris@0: else Chris@0: password_authentication(params[:name], params[:password]) Chris@0: end Chris@0: end Chris@0: Chris@0: Chris@0: protected Chris@0: def password_authentication(name, password) Chris@0: if @current_user = @account.users.authenticate(params[:name], params[:password]) Chris@0: successful_login Chris@0: else Chris@0: failed_login "Sorry, that username/password doesn't work" Chris@0: end Chris@0: end Chris@0: Chris@0: def open_id_authentication Chris@0: authenticate_with_open_id do |result, identity_url| Chris@0: if result.successful? Chris@0: if @current_user = @account.users.find_by_identity_url(identity_url) Chris@0: successful_login Chris@0: else Chris@0: failed_login "Sorry, no user by that identity URL exists (#{identity_url})" Chris@0: end Chris@0: else Chris@0: failed_login result.message Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: Chris@0: private Chris@0: def successful_login Chris@0: session[:user_id] = @current_user.id Chris@0: redirect_to(root_url) Chris@0: end Chris@0: Chris@0: def failed_login(message) Chris@0: flash[:error] = message Chris@0: redirect_to(new_session_url) Chris@0: end Chris@0: end Chris@0: Chris@0: Chris@0: Chris@0: If you're fine with the result messages above and don't need individual logic on a per-failure basis, Chris@0: you can collapse the case into a mere boolean: Chris@0: Chris@0: def open_id_authentication Chris@0: authenticate_with_open_id do |result, identity_url| Chris@0: if result.successful? && @current_user = @account.users.find_by_identity_url(identity_url) Chris@0: successful_login Chris@0: else Chris@0: failed_login(result.message || "Sorry, no user by that identity URL exists (#{identity_url})") Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: Chris@0: Simple Registration OpenID Extension Chris@0: ==================================== Chris@0: Chris@0: Some OpenID Providers support this lightweight profile exchange protocol. See more: http://www.openidenabled.com/openid/simple-registration-extension Chris@0: Chris@0: You can support it in your app by changing #open_id_authentication Chris@0: Chris@0: def open_id_authentication(identity_url) Chris@0: # Pass optional :required and :optional keys to specify what sreg fields you want. Chris@0: # Be sure to yield registration, a third argument in the #authenticate_with_open_id block. Chris@0: authenticate_with_open_id(identity_url, Chris@0: :required => [ :nickname, :email ], Chris@0: :optional => :fullname) do |result, identity_url, registration| Chris@0: case result.status Chris@0: when :missing Chris@0: failed_login "Sorry, the OpenID server couldn't be found" Chris@0: when :invalid Chris@0: failed_login "Sorry, but this does not appear to be a valid OpenID" Chris@0: when :canceled Chris@0: failed_login "OpenID verification was canceled" Chris@0: when :failed Chris@0: failed_login "Sorry, the OpenID verification failed" Chris@0: when :successful Chris@0: if @current_user = @account.users.find_by_identity_url(identity_url) Chris@0: assign_registration_attributes!(registration) Chris@0: Chris@0: if current_user.save Chris@0: successful_login Chris@0: else Chris@0: failed_login "Your OpenID profile registration failed: " + Chris@0: @current_user.errors.full_messages.to_sentence Chris@0: end Chris@0: else Chris@0: failed_login "Sorry, no user by that identity URL exists" Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: # registration is a hash containing the valid sreg keys given above Chris@0: # use this to map them to fields of your user model Chris@0: def assign_registration_attributes!(registration) Chris@0: model_to_registration_mapping.each do |model_attribute, registration_attribute| Chris@0: unless registration[registration_attribute].blank? Chris@0: @current_user.send("#{model_attribute}=", registration[registration_attribute]) Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: def model_to_registration_mapping Chris@0: { :login => 'nickname', :email => 'email', :display_name => 'fullname' } Chris@0: end Chris@0: Chris@0: Attribute Exchange OpenID Extension Chris@0: =================================== Chris@0: Chris@0: 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 Chris@0: Chris@0: 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: Chris@0: Chris@0: authenticate_with_open_id(identity_url, Chris@0: :required => [ :email, 'http://schema.openid.net/birthDate' ]) do |result, identity_url, registration| Chris@0: Chris@0: This would provide the sreg data for :email, and the AX data for 'http://schema.openid.net/birthDate' Chris@0: Chris@0: Chris@0: Chris@0: Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license