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:
Azure Active Directory allows us to create a test app to validate our integration.
1. Create an enterprise app.
Enterprise app
2. Setup the enterprise app and redirect to our rails app.
Setup app
Remember set up the following fields on the basic saml setup.
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
Attributes
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
Fingerprint
Get the certificate, the Azure AD Identificator, and SSO URL and set up on our rails app the following environment variables.