Security

SAML vs OAuth: Differences, How Each Works, When to Use

SAML and OAuth solve different problems. SAML is an XML standard for single sign-on: an identity provider tells an application who you are, in a signed assertion. OAuth 2.0 is an authorization framework: it gives an application a token that lets it call an API with limited permissions, without handing over your password.

The confusion comes from the fact that both involve redirects to a login page and both let one company's account system serve another company's application. The difference is in what gets passed back. SAML passes back a statement about a person. OAuth passes back a credential for an API.

SAML vs OAuth at a glance

AttributeSAML 2.0OAuth 2.0
Main purposeSingle sign-on: tell an application who the user isDelegated authorization: let an application call an API on a user's behalf
What is issuedAn assertion, an XML documentAn access token (format not set by the core spec)
Who gets itThe service provider (the application)The client (the application), to present to a resource server
RolesIdentity provider (IdP), service provider (SP)Resource owner, client, authorization server, resource server
Message formatXML, signed with XML SignatureHTTP requests and JSON responses
Typical useEmployees signing in to web apps with a company account"Connect your account" buttons, mobile apps, API access
Login on top of itBuilt inAdded by OpenID Connect

What SAML does

The OASIS SAML 2.0 Technical Overview describes SAML as a framework for exchanging security information between online business partners. Two roles carry single sign-on across domains. The identity provider (IdP) authenticates the user and issues assertions. The service provider (SP) is the application that relies on those assertions to decide whether to let you in.

An assertion can hold three kinds of statement:

  • Authentication: created by the party that successfully authenticated the user. It records how and when that happened.
  • Attribute: identifying details about the subject, such as group or membership.
  • Authorization decision: something the subject is entitled to do.

The SAML web browser sign-in flow

The overview describes the common SP-initiated flow like this:

  1. You request a protected page at the service provider and have no session.
  2. The SP sends an HTTP redirect that carries an authentication request (AuthnRequest) to the identity provider.
  3. The IdP checks whether you are already signed in. If not, it asks for your credentials.
  4. The IdP builds a SAML assertion describing your logon context, wraps it in a response, and returns it to your browser in an HTML form.
  5. Your browser posts that form to the SP's Assertion Consumer Service. The SP validates the signature and creates its own local session.
  6. The SP checks what you may access and returns the page.

The messages travel through your browser using bindings. HTTP Redirect carries a message in a 302 redirect. HTTP POST carries it base64-encoded inside an HTML form control. HTTP Artifact sends only a small reference value through the browser, and the full message is fetched separately. Because a response with an assertion passes through the browser, the specification requires that it be signed with XML Signature, so the SP can check its integrity and origin.

The result is that your password is typed only at the IdP. The application gets a signed statement and starts its own session. It never calls the IdP again for each request.

What OAuth does

RFC 6749 opens by saying the OAuth 2.0 authorization framework "enables a third-party application to obtain limited access to an HTTP service". The application (the client) never sees your password. It receives an access token, which the RFC defines as a credential used to access protected resources. The client then sends that token to the API (the resource server) with each request.

Permissions are expressed as scopes, "a list of space-delimited, case-sensitive strings" whose meaning each service defines. The authorization server may grant fewer scopes than the client asked for. The token's format is not fixed: the RFC says access tokens "can have different formats, structures, and methods of utilization". Some services issue JSON Web Tokens, others issue opaque random strings that only the server can look up.

The full step-by-step flow, including PKCE and the grant types, is covered in OAuth explained. For a comparison with the simpler alternative, see API key vs OAuth.

Authentication vs authorization

This is the core difference, and the one that matters most when choosing.

  • Authentication answers "who is this person?". SAML was built to answer it.
  • Authorization answers "what is this application allowed to do?". OAuth 2.0 was built to answer it.

An OAuth access token says an application may, for example, read your photos. It does not reliably tell the application who you are. The OAuth RFC does not define an identity statement at all. That gap is filled by OpenID Connect, which its specification calls "a simple identity layer on top of the OAuth 2.0 protocol". It defines an ID token, a JSON Web Token (JWT) that "contains Claims about the Authentication event". When someone says "log in with OAuth", they almost always mean OpenID Connect.

So the fairer comparison for single sign-on is SAML vs OpenID Connect. Both give an application a signed statement about the user from a trusted provider. SAML wraps it in XML. OpenID Connect wraps it in a JWT and rides on OAuth's HTTP and JSON conventions. For how JWTs compare with server-side sessions, see JWT vs session cookies.

When to use SAML

  • The application you are integrating only supports SAML. Many enterprise and older business applications do, and this settles the choice.
  • Your organisation's identity provider is already configured for SAML and you are adding another web application to it.
  • The use case is browser sign-in to a web application, and no API access on the user's behalf is needed.

SAML is less natural for native mobile apps and single-page apps, because its flow assumes a browser posting an HTML form to a server-side endpoint, and because XML signature handling is heavier than reading a JWT. This is a design observation, not a limit stated in the specification.

When to use OAuth

  • An application needs to call an API as the user: reading their calendar, files or repositories.
  • You are building a mobile, desktop or single-page client.
  • You need limited, revocable access: a token with a few scopes instead of a shared password.
  • You want sign-in for a new application and can use OpenID Connect on top of OAuth.

Do not use a bare OAuth access token as proof of identity. If you need to know who the user is, use OpenID Connect and validate the ID token.

Using both together

The two are not rivals in most organisations. A common arrangement is that staff sign in to web applications through SAML single sign-on from the company identity provider, while those same applications use OAuth 2.0 to call third-party or internal APIs. One handles "who are you", the other handles "what may this app do for you". Neither replaces the other, and neither replaces transport security: both depend on HTTPS, which what HTTPS stands for and how it works explains.

Security points that apply to both

  • Verify signatures. A SAML service provider must validate the assertion's signature. An OAuth resource server must validate the token, by signature if it is a JWT or by asking the authorization server if it is opaque.
  • Use HTTPS everywhere. Both protocols pass sensitive values through the browser or over the network.
  • Keep lifetimes short. A stolen assertion or token is useful to whoever holds it until it expires.
  • Request the minimum. In OAuth, ask for the narrowest scopes. In SAML, release only the attributes the application needs.

Frequently asked questions

Is SAML or OAuth better for single sign-on?
For browser-based single sign-on, both work, but they are built for different jobs. SAML was designed for it: an identity provider sends a signed assertion about the user to the application. OAuth 2.0 by itself only issues access tokens, so sign-in with OAuth normally means OpenID Connect, an identity layer on top of OAuth 2.0. Choose SAML if the application only offers SAML, and OpenID Connect if you are building a new app or a mobile or single-page client.
Can SAML and OAuth be used together?
Yes. Many organisations use SAML for employee sign-in to web applications and OAuth 2.0 for the APIs those applications call. They solve different problems, so they often sit side by side in the same company.
Is OAuth authentication or authorization?
OAuth 2.0 is an authorization framework. RFC 6749 describes it as letting a third-party application obtain limited access to an HTTP service. Authentication, meaning proving who the user is, is added by OpenID Connect, which defines an ID token for that purpose.
Does SAML use tokens?
SAML uses assertions, which are XML documents, rather than OAuth-style access tokens. An assertion states facts about a user, such as who authenticated and when, and the identity provider signs it so the service provider can verify where it came from.

Sources