omniauth / omniauth-saml

A generic SAML strategy for OmniAuth
https://github.com/omniauth/omniauth-saml
Other
331 stars 205 forks source link
omniauth saml

OmniAuth SAML

Gem Version Ruby Maintainability Coverage Status

A generic SAML strategy for OmniAuth available under the MIT License

https://github.com/omniauth/omniauth-saml

Requirements

Versioning

We tag and release gems according to the Semantic Versioning principle. In addition to the guidelines of Semantic Versioning, we follow a further guideline that otherwise backwards-compatible dependency upgrades for security reasons should generally be cause for a MINOR version upgrade as opposed to a PATCH version upgrade. Backwards-incompatible dependency upgrades for security reasons should still result in a MAJOR version upgrade for this library.

Usage

Use the SAML strategy as a middleware in your application:

require 'omniauth'
use OmniAuth::Strategies::SAML,
  :assertion_consumer_service_url     => "consumer_service_url",
  :sp_entity_id                       => "sp_entity_id",
  :idp_sso_service_url                => "idp_sso_service_url",
  :idp_sso_service_url_runtime_params => {:original_request_param => :mapped_idp_param},
  :idp_cert                           => "-----BEGIN CERTIFICATE-----\n...-----END CERTIFICATE-----",
  :idp_cert_multi                     => {
                                           :signing => ["-----BEGIN CERTIFICATE-----\n...-----END CERTIFICATE-----", "-----BEGIN CERTIFICATE-----\n...-----END CERTIFICATE-----", ...],
                                           :encryption => []
                                         },
  :idp_cert_fingerprint               => "E7:91:B2:E1:...",
  :idp_cert_fingerprint_validator     => lambda { |fingerprint| fingerprint },
  :name_identifier_format             => "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"

or in your Rails application:

in Gemfile:

gem 'omniauth-saml'

and in config/initializers/omniauth.rb:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :saml,
    :assertion_consumer_service_url     => "consumer_service_url",
    :sp_entity_id                       => "rails-application",
    :idp_sso_service_url                => "idp_sso_service_url",
    :idp_sso_service_url_runtime_params => {:original_request_param => :mapped_idp_param},
    :idp_cert                           => "-----BEGIN CERTIFICATE-----\n...-----END CERTIFICATE-----",
    :idp_cert_multi                     => {
                                             :signing => ["-----BEGIN CERTIFICATE-----\n...-----END CERTIFICATE-----", "-----BEGIN CERTIFICATE-----\n...-----END CERTIFICATE-----", ...],
                                             :encryption => []
                                           },
    :idp_cert_fingerprint               => "E7:91:B2:E1:...",
    :idp_cert_fingerprint_validator     => lambda { |fingerprint| fingerprint },
    :name_identifier_format             => "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
end

For IdP-initiated SSO, users should directly access the IdP SSO service URL. Set the href of your application's login link to the value of idp_sso_service_url. For SP-initiated SSO, link to /auth/saml.

A OneLogin::RubySaml::Response object is added to the env['omniauth.auth'] extra attribute, so we can use it in the controller via env['omniauth.auth'].extra.response_object

SP Metadata

The service provider metadata used to ease configuration of the SAML SP in the IdP can be retrieved from http://example.com/auth/saml/metadata. Send this URL to the administrator of the IdP.

Note that when integrating with Devise, the URL path will be scoped according to the name of the Devise resource. For example, if the app's user model calls devise_for :users, the metadata URL will be http://example.com/users/auth/saml/metadata.

Options

IdP Metadata

You can use the OneLogin::RubySaml::IdpMetadataParser to configure some options:

require 'omniauth'
idp_metadata_parser = OneLogin::RubySaml::IdpMetadataParser.new
idp_metadata = idp_metadata_parser.parse_remote_to_hash("http://idp.example.com/saml/metadata")

# or, if you have the metadata in a String:
# idp_metadata = idp_metadata_parser.parse_to_hash(idp_metadata_xml)

use OmniAuth::Strategies::SAML,
  idp_metadata.merge(
    :assertion_consumer_service_url => "consumer_service_url",
    :sp_entity_id                   => "sp_entity_id"
  )

See the Ruby SAML gem's README for more details.

Devise Integration

Straightforward integration with Devise, the widely-used authentication solution for Rails.

In config/initializers/devise.rb:

Devise.setup do |config|
  config.omniauth :saml,
    idp_cert_fingerprint: 'fingerprint',
    idp_sso_service_url: 'idp_sso_service_url'
end

Then follow Devise's general OmniAuth tutorial, replacing references to facebook with saml.

Single Logout

Single Logout can be Service Provider initiated or Identity Provider initiated.

For SP initiated logout, the idp_slo_service_url option must be set to the logout url on the IdP, and users directed to user_saml_omniauth_authorize_path + '/spslo' after logging out locally. For IdP initiated logout, logout requests from the IdP should go to /auth/saml/slo (this can be advertised in metadata by setting the single_logout_service_url config option).

When using Devise as an authentication solution, the SP initiated flow can be integrated in the SessionsController#destroy action.

For this to work it is important to preserve the saml_uid and saml_session_index value before Devise clears the session and redirect to the /spslo sub-path to initiate the single logout.

Example destroy action in sessions_controller.rb:

class SessionsController < Devise::SessionsController
  # ...

  def destroy
    # Preserve the saml_uid and saml_session_index in the session
    saml_uid = session['saml_uid']
    saml_session_index = session['saml_session_index']
    super do
      session['saml_uid'] = saml_uid
      session['saml_session_index'] = saml_session_index
    end
  end

  # ...

  def after_sign_out_path_for(_)
    if session['saml_uid'] && session['saml_session_index'] && SAML_SETTINGS.idp_slo_service_url
      user_saml_omniauth_authorize_path + "/spslo"
    else
      super
    end
  end
end

By default, omniauth-saml attempts to log the current user out of your application by clearing the session. This may not be enough for some authentication solutions (e.g. Clearance). Instead, you may set the :idp_slo_session_destroy option to a proc that performs the necessary logout tasks.

Example :idp_slo_session_destroy setting for Clearance compatibility:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :saml, idp_slo_session_destroy: proc { |env, _session| env[:clearance].sign_out }, ...
end

Authors

Authored by Rajiv Aaron Manglani, Raecoo Cao, Todd W Saxton, Ryan Wilcox, Steven Anderson, Nikos Dimitrakopoulos, Rudolf Vriend and Bruno Pedro.