DEV Community

Cover image for ๐Ÿ” Understanding SAML, OAuth 2.0 and OpenID Connect
Fazal Mansuri
Fazal Mansuri

Posted on

๐Ÿ” Understanding SAML, OAuth 2.0 and OpenID Connect

๐Ÿ”Ž What Are These Protocols?

SAML 2.0 (Security Assertion Markup Language)

SAML is an authentication and authorization protocol primarily used for Single Sign-On (SSO) in enterprises. It enables users to log in once and access multiple applications without entering credentials again.

โœ” Used For: Enterprise authentication (e.g., logging into Google Workspace, Salesforce, etc.)

โœ” How It Works: Uses XML-based security assertions to exchange authentication and authorization data.

โœ” Downside: Verbose XML format, and not actively developed anymore.

OAuth 2.0 (Open Authorization)

OAuth 2.0 is an authorization protocol designed to grant limited access to resources without exposing user credentials. Itโ€™s commonly used for API access and delegated permissions.

โœ” Used For: Third-party API authorization (e.g., "Allow this app to access your Google Drive")

โœ” How It Works: Uses access tokens to grant specific permissions.

โœ” Downside: It doesnโ€™t authenticate usersโ€”it just authorizes actions.

OpenID Connect (OIDC)

OIDC is an authentication protocol built on top of OAuth 2.0. It adds ID Tokens to OAuth, allowing applications to verify a user's identity in a secure, standardized way.

โœ” Used For: Modern authentication (e.g., "Sign in with Google")

โœ” How It Works: Extends OAuth 2.0 by providing identity tokens (JWTs) to verify users.

โœ” Why Itโ€™s Popular: Lightweight, JSON-based, and designed for modern web and mobile apps.


At this point, you might be thinking:

"Okay, I just read these definitions, but things still seem a bit confusing."

"Donโ€™t worryโ€”I felt the same when I first tried to understand them! So, let me break it down in the simplest way possible."

Before we jump into the story-based explanation, letโ€™s first understand two key terms that will make everything clearer:

โœ” Identity Provider (IdP):

The system that authenticates users and confirms their identity.

Example: Google, Okta, Microsoft Entra ID (Azure AD).

โœ” Service Provider (SP):

The application or website that users want to access after authentication.

Example: Gmail, Slack, Salesforce.

๐Ÿ’ก Think of it this way:

The IdP is like a security guard checking your ID before letting you in.

The SP is the building or service youโ€™re trying to enter.


๐Ÿ” Letโ€™s Understand This with a Story

Imagine a big amusement park ๐ŸŽก where visitors need special passes to access different rides.

In this park:
๐ŸŽŸ A Ticket (Token) = Proof of Access
๐Ÿ› The Ticket Counter (Identity Provider) = Authenticates visitors
๐ŸŽข The Ride (Service Provider) = Grants access based on the ticket

This analogy will help you understand how authentication and authorization work in each protocol.

1๏ธโƒฃ SAML 2.0 โ€“ The VIP Pass for Employees

๐Ÿ“– Story: The Corporate Park & Employee Pass

SAML image with the amusement park analogy, where a visitor uses a single ticket to access multiple rides after verification.

Imagine one company that owns a huge amusement park. Employees donโ€™t buy separate tickets for every rideโ€”they get a VIP Employee Badge ๐ŸŽซ from the HR office.

How It Works (SAML Flow):

1๏ธโƒฃ ๐Ÿ› HR Office = Identity Provider (IdP)

Employees check in at the HR office (login page).

The HR office verifies who they are and issues a VIP badge (SAML token).

2๏ธโƒฃ ๐ŸŽข Amusement Park Rides = Service Providers (SP)

The employee shows the badge at any rideโ€™s entrance.

The ride operator trusts the badge (without asking for a password).

The employee gets access to the ride (authorization).

Key Takeaways:
โœ… SSO (Single Sign-On): Employees log in once and access multiple rides.
โœ… Authentication + Authorization: The badge confirms who you are and grants access to specific rides.
โœ… Enterprise Use: Best for corporate authentication, but not ideal for modern mobile/web apps.
โœ… Legacy & Declining Use: SAML is no longer actively developed, and many modern applications are shifting towards OpenID Connect (OIDC).

๐Ÿ“Œ Example Use Case:

A company uses SAML-based SSO so employees can log in once and access tools like Google Workspace, Salesforce, and Slack without re-entering credentials.


2๏ธโƒฃ OAuth 2.0 โ€“ Borrowing a Friendโ€™s Car (Authorization, Not Authentication)

๐Ÿ“– Story: The Car Rental & Borrowing Keys

OAuth 2.0, Rental car analogy

Imagine you need a rental car ๐Ÿš— (access to an app), but instead of getting a car yourself, you ask your friend to get it for you.

How It Works (OAuth Flow):

1๏ธโƒฃ ๐Ÿงโ€โ™‚๏ธ User (You) asks your Friend (Client App) to get a Car (Protected Resource).

You donโ€™t have direct access to the rental car system.

Instead, you give your friend (third-party app) permission to get a car for you.

2๏ธโƒฃ ๐Ÿ› Rental Agency (Authorization Server) issues a Car Key (Access Token).

The agency checks if your friend is authorized.

They issue a temporary key (OAuth token) that allows your friend to drive the car.

3๏ธโƒฃ ๐Ÿš— Using the Car (Accessing Resources).

Your friend shows the car key and gets the car without needing your ID.

They canโ€™t modify the rental agreementโ€”just drive the car based on the given permission.

Key Takeaways:
โœ… Authorization, Not Authentication: The car key doesnโ€™t prove who you are, it just lets someone use a resource.
โœ… Token-Based: If someone steals the key, they can use the car, even if they arenโ€™t you (Bearer Token Risk).
โœ… Used for API Access: Perfect for granting limited access to apps (e.g., "Allow this app to access your Google Drive").

๐Ÿšจ Key Misconception:

OAuth โ‰  Authentication. Itโ€™s about authorization (permissions), not logging in.


3๏ธโƒฃ OpenID Connect (OIDC) โ€“ VIP Pass with Your Photo (Authentication + Authorization)

๐Ÿ“– Story: The All-Access Digital ID

OIDC, where a visitor uses a special wristband to enter an amusement park and access rides, symbolizing both authentication and authorization.

Now, imagine a more modern amusement park where you can:
โœ” Use a digital ID ๐Ÿ“ฑ instead of a paper ticket.
โœ” Your ID has your photo to prove who you are (Authentication).
โœ” Your ID also includes ride permissions (Authorization).

How It Works (OIDC Flow):

1๏ธโƒฃ ๐Ÿ› Ticket Booth (Identity Provider) issues an ID with your photo.

The booth verifies your identity and gives you a digital ID (ID Token) with your photo and details.

2๏ธโƒฃ ๐ŸŽข Amusement Park Scanners (Apps & APIs) accept the ID.

When you go to a ride, you scan your digital ID.

The system verifies your photo (Authentication) and checks ride access (Authorization).

Key Takeaways:
โœ… Authentication + Authorization: OIDC proves who you are (ID Token) + controls what you can do (Access Token).
โœ… Modern & Lightweight: Uses JSON instead of XML (simpler than SAML).
โœ… Best for Web & Mobile Apps: Google, Facebook, and Microsoft use OIDC for "Sign in with Google/Facebook" logins.
โœ… Replacing SAML: Many modern applications are shifting from SAML to OIDC for better support in web & mobile apps.


๐Ÿค” When to Use Which Protocol?

Use Case Best Choice Why?
Enterprise SSO (Internal Apps) SAML Secure, well-established in enterprises
Third-party API Access OAuth 2.0 Grants limited access without credentials
Social Login & Modern Web Apps OIDC Lightweight, authentication + authorization

๐ŸŽฏ Final Thoughts

Understanding SAML, OAuth and OIDC is crucial for securing applications. These protocols ensure users and apps interact securely without sharing sensitive credentials.

You might have noticed that SAML and OIDC seem similarโ€”both handle authentication and enable Single Sign-On (SSO). However, the key difference is how they work:

โœ” SAML: Uses XML, is enterprise-focused, and is mainly for web-based applications.
โœ” OIDC: Uses JSON, is lightweight, and works well for modern web and mobile apps.

While SAML is older and widely used in enterprises, OIDC is the futureโ€”itโ€™s more flexible and designed for modern applications.

๐Ÿš€ If your system already uses SAML, you might stick with it. But if you're building something new, OIDC is likely the better choice!

๐Ÿ’ฌ Which protocol have you worked with the most? Letโ€™s discuss in the comments! ๐Ÿš€

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil โ€” patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)