See saml Menu

Code Your Ruby App to Provide SSO via OneLogin

Get the toolkit: 

Using the Toolkit

The lib folder contains the files you’ll copy into your Rails application. But first, let’s take a look at the sample application located here. It’s a ready-to-run SAML relying party (or service provider). The saml_controller.rb file contains all of the logic needed to authenticate using SAML:

require 'onelogin/saml'

class SamlController < ApplicationController
  skip_before_filter :verify_authenticity_token, :only => [:consume]  

  def index
    # insert identity provider discovery logic here
    settings = Account.get_saml_settings
    request =

  def consume
    response =[:SAMLResponse])

    # insert identity provider discovery logic here
    response.settings = Account.get_saml_settings "NAMEID: #{response.name_id}"

    if response.is_valid?
      session[:userid] = response.name_id
      redirect_to :action => :complete
      redirect_to :action => :fail

The index action initiates the SAML exchange.

To know where to redirect the user with the authentication request, we need to establish the user’s identity provider. This depends on your application. If accounts have a dedicated subdomain name (e.g. or if SAML authentication for accounts is limited to certain IP ranges, you need to look up account information based on whatever information you already have about the user.

The consume action receives the SAML assertion. Again, you need to know the identity provider the user belongs to, but now you have a clue: use response.nameid to retrieve the username or email address in the SAML assertion. Then use the information to retrieve the identity provider information. You can then verify that the SAML assertion is actually from the identity provider configured on the account.

What Needs to be Configured

In the example above, SAML settings are retrieved using the get_saml_settings method on the account object. This example is generic and get_saml_settings doesn’t take any parameters. You’ll need to add your own information here (subdomain and ip_address, for example) when fetching the account, and hence SAML settings.

The following information needs to be available on the account:

  • assertion_consumer_service_url

    The URL at which the SAML assertion should be received. In this example, http://localhost:3000/saml/consume would be correct.

  • issuer

    The name of your application. Some identity providers might need this to establish the identity of the service provider requesting the login.

  • idp_sso_target_url

    The URL to which the authentication request should be sent. This would be on the identity provider.

  • idp_cert_fingerprint

    The certificate fingerprint, e.g. “90:cc:16:f0:8D:a6:D1:c6:BB:27:2D:ba:93:80:1A:1f:16:8e:4E:08”. This is provided from the identity provider when setting up the relationship.

  • name_identifier_format

    Describes the format of the username required by this application. If you need the email address, use “urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress”. See section 8.3 of Assertions and Protocols for the OASIS Security Assertion Markup Language (SAML) V2.0 for other options. Note that the identity provider might not support all options.

Have a Question?

Have a how-to question? Seeing a weird error? Ask us about it on StackOverflow.

Found a bug? Submit a support ticket.

Have a product idea or request? Share it with us in our Ideas Portal.

StackOverflow discussions about "onelogin saml ruby"

  • 20

    Q: CAS vs. SAML vs. OAuth2

    Asked Mar 14 2015 CASino and And I am sure there are hundreds of OAuth related gems. I just want a separate Rails … simple enough. At my company, we have a bunch of Ruby on Rails applications. I want to build an SSO authentication service which all those applications should use. Trying to do some research on how to go …

  • 15

    A: SAML 2.0 SSO for Ruby on Rails?

    Answered Nov 20 2010

    I played with this one once: It might be what you're looking for. …

  • 4

    Q: Rails - Onelogin ruby-saml integration issue +

    Asked Apr 08 2014

    I am facing issue in using ruby-saml in my Rails application. I am new to Ruby world. From here I got to know I could use ruby-saml tool kit for SAML SP. Now, when I tried to refer to OneLogin … different errors. require 'onelogin/saml' or require 'onelogin/ruby-saml' Getting the errors like, cannot load such file -- onelogin/saml or cannot load such file -- onelogin/ruby-saml I installed …

  • 2

    Q: Getting error while installing ruby-saml(1.3.0) in rails 2.3.2 application

    Asked Aug 12 2016

    to install ruby-saml(1.3.0) in ruby 1.8.7 because there are multiple functions(OneLogin::RubySaml::SloLogoutresponse,OneLogin::RubySaml::SloLogoutrequest) for idp initiated SLO, which are not in ruby … I am getting below error while installing ruby-saml(1.3.0) in my application(ruby 1.8.7,rails 2.3.2). Fetching: ruby-saml-1.3.0.gem (100%) ERROR: Error installing ruby-saml: mini_portile2 …

  • 2

    A: How to set up onelogin SAML in Ruby on Rails?

    Answered Apr 11 2017

    According to the declarations should be: OneLogin::RubySaml::Authrequest and OneLogin::RubySaml::Response It seems that the docs for the toolkit … on their website are outdated. Use docs provided in the gem. From the gem docs: Version 0.8.x changes the namespace of the gem from OneLogin::Saml to OneLogin::RubySaml. Please update your implementations of the gem accordingly. …