⚷ Authentication & Identity

SAML vs OAuth vs OpenID Connect

SAML, OAuth 2.0, and OpenID Connect are three of the most widely used standards for federated identity, and they are frequently confused because their scopes overlap and their names appear side by side in login systems. The critical distinction is that SAML and OpenID Connect handle authentication, proving who a user is, while OAuth 2.0 handles authorization, granting an application limited access to resources. Choosing correctly, and combining them safely, depends on understanding what each protocol was actually designed to do.

OAuth 2.0: Delegated Authorization

OAuth 2.0 is an authorization framework. Its purpose is to let a user grant one application limited access to their data held by another service, without sharing a password. When you allow a photo-printing app to read your cloud photos, OAuth issues the app a scoped access token instead of your credentials.

The framework defines four roles:

  • Resource owner: the user who owns the data.
  • Client: the application requesting access.
  • Authorization server: issues tokens after the user consents.
  • Resource server: the API that holds the data and accepts tokens.

The most common flow is the authorization code grant:

  1. The client redirects the user to the authorization server with requested scopes.
  2. The user authenticates and consents to the requested access.
  3. The authorization server returns a short-lived authorization code to the client's redirect URI.
  4. The client exchanges that code, plus its own credentials, for an access token at the token endpoint.
  5. The client presents the access token to the resource server to call the API.

The key point for security reviewers is that OAuth by itself says nothing about the user's identity. An access token is a bearer credential granting API access; it is not proof of who logged in. Treating a raw OAuth access token as authentication is a classic mistake that can lead to account confusion and token substitution vulnerabilities.

OpenID Connect: An Identity Layer on OAuth

OpenID Connect (OIDC) fills exactly that gap. It is a thin identity layer built on top of OAuth 2.0, adding authentication in a standardized way. Alongside the access token, the authorization server issues an ID token.

The ID token is a JSON Web Token (JWT) containing signed claims about the authentication event:

  • iss (issuer) identifies the provider that authenticated the user.
  • sub (subject) is a stable unique identifier for the user.
  • aud (audience) names the client the token was issued for.
  • exp and iat bound the token's validity in time.
  • nonce ties the token to the original request to prevent replay.

Decoded, an ID token payload is simply signed JSON:

{
  "iss": "https://idp.example.com",
  "sub": "248289761001",
  "aud": "client-app-id",
  "exp": 1710000900,
  "iat": 1710000600,
  "nonce": "n-0S6_WzA2Mj"
}

Because OIDC reuses OAuth's flows and adds a well-defined identity token plus a standard userinfo endpoint, it has become the common choice for modern web and mobile single sign-on. It gives applications both authentication (the ID token) and authorization (the access token) in one round trip.

SAML: XML-Based Enterprise Federation

Security Assertion Markup Language (SAML) 2.0 predates OIDC and solves the same authentication problem using XML. A SAML assertion is an XML document, signed by the identity provider, stating that a user has authenticated and carrying attributes such as name, email, and group membership.

SAML's typical web browser SSO uses HTTP redirects and form POSTs to move the assertion from the identity provider to the service provider. Its signing and validation rely on XML Digital Signatures over the assertion.

SAML remains entrenched in enterprise environments and SaaS integrations because of its maturity and rich attribute handling. Its main drawbacks are the verbosity of XML, the operational complexity of certificate management, and a history of parser-related vulnerabilities, notably XML signature wrapping, where an attacker restructures the document so a validator checks a legitimately signed element while the application reads an attacker-controlled one.

Comparing the Three

The protocols answer different questions, and the following comparison clarifies when each applies.

  • Primary purpose: OAuth grants authorization; SAML and OIDC assert authentication.
  • Token format: SAML uses XML assertions; OAuth and OIDC use JSON, with OIDC's ID token being a JWT.
  • Typical use: SAML dominates legacy enterprise web SSO; OIDC suits modern apps, single-page apps, and mobile; OAuth underlies API access delegation for both.
  • Transport: SAML leans on browser redirects and POST; OAuth and OIDC use RESTful HTTP with JSON.

A practical rule: if you need to prove identity for login, reach for OIDC or SAML. If you need to call an API on a user's behalf, reach for OAuth. All three are common mechanisms behind single sign-on.

Security Considerations Across Protocols

Each standard has recurring pitfalls worth auditing.

Validate Tokens Rigorously

For OIDC and OAuth, always verify the JWT signature against the provider's published keys, and check iss, aud, exp, and nonce. Never accept a token whose signature algorithm is set to none, and do not trust unsigned claims.

Protect the Flows

  • Use the authorization code flow with PKCE for public clients such as mobile and single-page apps, which defends against code interception.
  • Register exact redirect URIs to prevent open-redirect token leakage.
  • Keep access tokens short-lived and scope them narrowly.

Guard Against SAML-Specific Attacks

  • Validate signatures over the correct elements and reject documents with wrapped or duplicated assertions.
  • Enforce audience restriction and assertion expiry to stop replay.
  • Pin trusted signing certificates and rotate them deliberately.

Because these protocols issue tokens that grant broad access, they must be paired with strong session management once the user is logged in, ensuring a stolen token cannot be replayed indefinitely.

Key Takeaways

  • OAuth 2.0 is for authorization; SAML and OpenID Connect are for authentication.
  • OpenID Connect adds an ID token, a signed JWT, on top of OAuth to standardize proof of identity.
  • SAML uses signed XML assertions and remains common in enterprise SSO despite XML complexity.
  • Never treat a raw OAuth access token as evidence of who the user is.
  • Rigorous token validation, PKCE, exact redirect URIs, and defense against XML signature wrapping are essential across these protocols.
samloauthoidcfederationtokens

Frequently asked questions

What is the difference between SAML, OAuth, and OpenID Connect?

SAML and OpenID Connect handle authentication, proving who a user is, while OAuth 2.0 handles authorization, granting an application limited access to resources without sharing a password. SAML carries signed XML assertions, OpenID Connect adds a JSON ID token on top of OAuth, and OAuth on its own issues access tokens for APIs. A useful rule is to use OIDC or SAML for login and OAuth for API access.

What is OAuth 2.0 used for?

OAuth 2.0 is an authorization framework that lets a user grant one application limited access to their data held by another service, without revealing their password. It issues a scoped access token that the application presents to an API, rather than proving who the user is. Treating a raw OAuth access token as proof of identity is a classic mistake that can lead to token substitution vulnerabilities.

What is the difference between an access token and an ID token?

An access token is a bearer credential that grants access to an API and says nothing reliable about who logged in, whereas an ID token, introduced by OpenID Connect, is a signed JSON Web Token asserting the user's identity and the authentication event. The ID token contains claims such as issuer, subject, audience, and expiry. Applications should use the ID token for login and the access token for calling resources.

Is SAML better than OpenID Connect?

Neither is strictly better; they suit different environments. SAML is mature and rich in attribute handling and remains entrenched in enterprise web SSO and SaaS integrations, but its XML is verbose and has a history of parser attacks like signature wrapping. OpenID Connect is lighter, uses JSON and JSON Web Tokens, and is favored for modern web, single-page, and mobile applications.

What is XML signature wrapping in SAML?

XML signature wrapping is an attack in which an adversary restructures a SAML document so a validator checks a legitimately signed element while the application actually reads a different, attacker-controlled element. It exploits the gap between what is verified and what is consumed. Defenses include validating signatures over the correct elements and rejecting documents with duplicated or wrapped assertions.

How should OAuth and OpenID Connect tokens be validated?

Always verify the token's signature against the provider's published keys and check the issuer, audience, and expiry, plus the nonce for an ID token to prevent replay. Never accept a token whose signature algorithm is set to none, and do not trust unsigned claims. Using the authorization code flow with PKCE and registering exact redirect URIs further protects the flow.

Try it hands-on

K0G is an open toolkit of browser-based security utilities — hashing, encoding, JWT, certificates, crypto and more, all running locally in your browser.

Explore the tools →