Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
151 lines (108 sloc) 5.39 KB

devise_openid_authenticatable Build Status

Written by Nat Budin

devise_openid_authenticatable is OpenID authentication support for Devise applications. It is very thin and uses Rack::OpenID for most of the actual work.


  • Devise 1.3 or higher
  • rack-openid


Add the following to your project's Gemfile:

gem "devise_openid_authenticatable"

Then run bundle install.


Once devise_openid_authenticatable is installed, add the following to your user model:

devise :openid_authenticatable

You can also add other modules such as token_authenticatable, trackable, etc. Database_authenticatable should work fine alongside openid_authenticatable.

You'll also need to set up the database schema for this:

create_table :users do |t|

and, optionally, indexes:

add_index :users, :identity_url, :unique => true

In addition, you'll need to modify sessions/new.html.erb (or the appropriate scoped view if you're using those). You need to add a field for identity_url, and remove username and password if you aren't using database_authenticatable:

<% form_for resource_name, resource, :url => session_path(resource_name) do |f| -%>
  <p><%= f.label :identity_url %></p>
  <p><%= f.text_field :identity_url %></p>

  <% if devise_mapping.rememberable? -%>
    <p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
  <% end -%>

  <p><%= f.submit "Sign in" %></p>
<% end -%>

Finally, you'll need to wire up Rack::OpenID in your Rails configuration:

config.middleware.insert_before(Warden::Manager, Rack::OpenID)

(Rack::OpenID needs to come before Warden in the middleware stack so that by the time devise_openid_authenticatable tries to authenticate the user, the OpenID response will have already been decoded by Rack::OpenID.)

Automatically creating users

If you want to have users automatically created when a new OpenID identity URL is successfully used to sign in, you can implement a method called "build_from_identity_url" to your user model class:

class User < ActiveRecord::Base
  devise :openid_authenticatable
  def self.build_from_identity_url(identity_url) => identity_url)

SReg and AX Extensions

As of version 1.0.0.alpha4, devise_openid_authenticatable now supports the SReg (simple registration) and AX (attribute exchange) extensions. This allows OpenID providers to pass you additional user details, such as name, email address, gender, nickname, etc.

To add SReg and AX support to your User model, you'll need to do two things: first, you need to specify what fields you'd like to request from OpenID providers. Second, you need to provide a method for processing these fields during authentication.

To specify which fields to request, you can implement one (or both) of two class methods: openid_required_fields and openid_optional_fields. For example:

def self.openid_required_fields
  ["fullname", "email", "", ""]

def self.openid_optional_fields
  ["gender", ""]

Required fields should be used for fields without which your app can't operate properly. Optional fields should be used for fields which are nice to have, but not necessary for your app. Note that just because you specify a field as "required" doesn't necessarily mean that the OpenID provider has to give it to you (for example, a provider might not have that field for its users).

In the above example, we're specifying both SReg fields (fullname, email, and gender) and the equivalent AX fields (the ones that look like URLs). A list of defined AX fields and their equivalent SReg fields can be found at It is highly recommended to specify both AX and SReg fields, as both are implemented by different common OpenID providers.

Once a successful OpenID response comes back, you still need to process the fields that the provider returned to your app. To do that, implement an instance method called openid_fields=. This method takes a hash that maps each returned field to a string value. For example:

def openid_fields=(fields)
  fields.each do |key, value|
    # Some AX providers can return multiple values per key
    if value.is_a? Array
      value = value.first
    case key.to_s
    when "fullname", "" = value
    when "email", "" = value
    when "gender", ""
      self.gender = value
      logger.error "Unknown OpenID field: #{key}"

See also


  • Test on non-ActiveRecord ORMs