Guide to Implementing Authentication with NestJS and SSO Saml2
Introduction
SSO (Single Sign-On) is a centralized authentication mechanism that allows users to access multiple different systems with a single set of login credentials.
Key advantages include:
- Improving user experience by reducing the number of passwords to remember.
- Enhancing security through centralized management and minimizing the risk of brute-force attacks at various points.
- Purpose: To enable users to log in only once to one location (Identity Provider - IdP) but be able to access multiple different applications without re-entering their password.
- Example: You log into your Google account, then open Gmail, YouTube, Drive without logging in again.
SAML & SAML2 (Security Assertion Markup Language)
- SAML 1.0/1.1 were the first versions that laid the foundation for exchanging identity data using XML, but are now obsolete.
- SAML 2.0 (Saml2) is a strong combination and improvement, supporting modern web scenarios and becoming the most popular standard for SSO in Corporate/Enterprise environments due to its flexible integration and high security (requiring Certificate, Metadata XML).
- Nature: Uses XML data structures to exchange authentication and authorization information between an Identity Provider (such as Microsoft Entra) and a Service Provider (such as the web servers we develop).
Prerequisites
This article will continue developing from a previous project using JWT. After performing login via SSO, token information will be sent to the client for use. You can review previous articles to grasp the necessary information before continuing.
Detail
First, access the page to create an application to get SSO Saml2 information as follows: Enterprise app > New application
After successfully creating the application, you can see information such as:
- Reply URL (Assertion Consumer Service URL): is the callback url that will be redirected to after successful login. This value can accept http, so you can input an arbitrary value to test on localhost.
- Logout Url (Optional): is the callback url that will be redirected to after successful logout, usually set to your frontend url. This value only accepts HTTPS, so you can deploy your web application to some host to input the appropriate value.
Please download the Federation Metadata XML file, this file will contain the necessary information for SSO authentication and will be used in the NestJS project in the next steps.
Update the environment.service.ts file with information from the .env file as follows:
Create the saml2.strategy.ts file:
This code segment initializes the SAML2 authentication strategy by reading the Federation Metadata XML file (which you downloaded earlier) to automatically configure parameters such as Identity Provider URL and certificate (Cert), and then returns the user profile after successful login.
Create the saml2.guard.ts file to use Saml2Strategy:
The auth.service.ts file with the following content:
Create the sso.controller.ts file:
- This Controller directs the SSO login flow: the /login endpoint redirects to the IdP (Identity Provider), /login-callback receives information after login to create a JWT token and send it back to the frontend, and /logout handles synchronization logout with the SSO system.
- Note here that there must be a /logout api to delete the logged-in session from the IdP, not just clear the token on the frontend side. Otherwise, after the user logs out and then logs in again, the session from the old account will still be kept, and it's impossible to switch to using another account.
Update the app.module.ts file:
This is the .env file, please change other information according to the application you created on Microsoft Entra:
Next, in the NextJS project to perform SSO login, create a page with the following content:
This React page manages client-side login state: it checks and stores the JWT token from the URL after SSO redirect back, handles user navigation based on the redirect parameter, and provides an interface to trigger the login or logout process.
Here the operating flow will be as follows:
- Use on the web:
- When accessing the page https://your-domain.com
- Check if a token already exists, then allow usage.
- If there is no token, automatically redirect to the /sso/login page to log in with SSO.
- After successful login, token information will be returned for the frontend to save and use.
- In case it needs to be used on other devices like mobile with a change in the redirect url:
- Access the page https://your-domain.com?redirect=https://your-domain.com/deep-link-mobile
- The Redirect url (https://your-domain.com/deep-link-mobile) will be saved to perform redirect after successful login.
- Also check the token to allow usage or redirect to the SSO login page.
- After successful login, it will redirect back to the previously saved page (https://your-domain.com/deep-link-mobile)
- When performing logout, calling the /sso/logout api will return a redirect url. When the frontend redirects to that page, it will clear the session of that account and then redirect to the home page.
Token information will be attached to the url after successful SSO login in a form like this:
Check the login and logout results as follows:
Happy coding!
Comments
Post a Comment