Integrate Azure Active Directory with Rails app using devise

Using devise_saml and devise_token_auth gems

Posted on August 01, 2023

One of the most common authentication methods is Single Sign-On (SSO) and Active Directory is one of the most popular credentials database managers used by the largest companies. When you have a SaaS product, enabling the option to connect it with an Active Directory is definitely a necessity.


In the given scenario, we have a Rails app that requests credentials from Azure AD. We have already implemented a Devise authentication scheme and now need to upgrade our Rails app to allow both authentication methods: User-Password and Single Sign-On (SSO).

Adding SAML to our Rails App


Once you've set up Devise in your application, the next step is to integrate the devise_saml_authenticatable.This gem will enable the creation of a SAML Request to communicate with your Active Directory.


bundle install

Add the devise saml strategy to the model user created by devise.

 class User < ActiveRecord::Base
    ...
    devise :saml_authenticatable, :trackable
    ...
  end

Setup devise initializer


Using theĀ  initializer generated by devise gem setup the following:

Devise.setup do |config|
  config.saml_create_user = true
  config.saml_update_user = true
  config.saml_default_user_key = :email
  config.saml_session_index_key = :session_index
  config.saml_configure do |settings|
    settings.assertion_consumer_service_url     = "http://localhost:3000/users/saml/auth"
    settings.assertion_consumer_service_binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
    settings.name_identifier_format             = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
    settings.sp_entity_id                       = "http://localhost:3000/users/saml/metadata"
    settings.idp_slo_service_url                = ENV['SAML_IDP_SLO'] || ''
    settings.idp_sso_service_url                = ENV['SAML_IDP_SSO']
    settings.idp_cert_fingerprint               = ENV['SAML_FINGERPRINT']
    settings.idp_cert_fingerprint_algorithm     = "http://www.w3.org/2000/09/xmldsig#sha256"
    settings.issuer = ENV['SAML_IDP_ISSUER']
  end
end

Setup Active Directory


Azure Active Directory allows us to create a test app to validate our integration.

1. Create an enterprise app.


2. Setup the enterprise app and redirect to our rails app.



Remember set up the following fields on the basic saml setup.

Magnified Image
Entity Id => http://localhost:3000/users/saml/metadata
Assertion Consumer Service =>http://localhost:3000/users/saml/auth
SSO Service URL => http://localhost:3000/users/saml/auth

3. Setting attributes for response
The required field is email but you can customize any other additional field like first_name, last_name or company name.

4. Get the certificate and SSL URL


Get the certificate, the Azure AD Identificator, and SSO URL and set up on our rails app the following environment variables.

SAML_IDP_SSO => SSO URL
SAML_IDP_SL0 => SLO URL
SAML_FINGERPRINT => CERTIFICATE FINGERPRINT

To get the Certificate fingerprint, use the following command.

openssl x509 -text -noout -in YourCerticate.cer -fingerprint -sha256
The SHA256 Fingerprint is a the end of the generated text.


Test your Rails app.


Once you set your Active Directory and set the environment variables try login using SAML accessing a the following route on your localhost

http://localhost:3000/users/saml/sign_in

You must be redirected to your Active Directory and asked for your credentials, later you'll be redirected to your app.

Can validate accessing your IDP running this repo https://github.com/hllanes08/rails7_baseline/tree/feature/sso