recipes

Technical recipes for frequently and infrequently recurring problems

View the Project on GitHub curationexperts/recipes

Authenticating with Shibboleth in a Hyrax Application

Goal: Authenticate against shibboleth in production, but against a database in (local) development environment.

1. Add needed fields to your user model

2. Use uid instead of email for the user_key

Edit config/initializers/devise.rb and change the value of config.authentication_keys to uid (or whatever is appropriate for this particular shibboleth integration.)

config.authentication_keys = [:uid]

Run your test suite and fix any tests that broke. You might need to use find_by_user_key instead of find_by(:email), for example.

3. Add AuthConfig model

Add an model called AuthConfig we can use to configure which authentication method we want to use. This lets us continue to use database authentication in development. Add this to app/models/auth_config.rb:

  class AuthConfig
    # In production, we use Shibboleth for user authentication,
    # but in development mode, you may want to use local database
    # authentication instead.
    def self.use_database_auth?
      !Rails.env.production? && ENV['DATABASE_AUTH'] == 'true'
    end
  end

4. Allow shibboleth login to create a new user

We can’t assume that all user accounts will exist on the system before they log in, so authenticating against shibboleth has to allow for the creation of a new User account at login time.

5. Devise integration

5. Add shibboleth routes, controllers, and devise configuration

6. Allow user login with uid

7. Allow systems users to be created without passwords

Now in production we’re expecting that all users will be managed with shibboleth, and so the User model no longer has a password method. This is going to cause anything that creates a systems user to fail. Let’s fix that.

You should now be able to deploy this application to a systems with Shibboleth SP configured and have it work as expected. Note that this document assumes the systems to which you’ll be deploying is set up in the DCE Shibboleth SP pattern.