The idea: log in at one place, trusted by many apps
A company has dozens of web apps. With SAML 2.0 (Security Assertion Markup Language) none of them keeps passwords. Each app is a service provider (SP). One server, the identity provider (IdP), logs people in and then writes a signed statement, an assertion: "this is alice@example.org, she logged in at 09:00, this is for app X, valid for 5 minutes". The app checks the signature and believes it.
The canvas follows one login. On the left is the SP sp.example.com, in the middle alice's browser
with its address bar and cookies, on the right the IdP idp.example.org. The panel below shows the
SAML message of the moment as XML. Blue boxes carry the AuthnRequest, orange boxes the signed Response, grey
boxes plain HTTP.
Metadata: what the two sides know in advance
Before any login, the SP and the IdP swap metadata, small XML files that an admin uploads once. Nothing else connects them.
| Side | entityID (its name) | Endpoint | Key |
|---|---|---|---|
| SP | https://sp.example.com/saml | ACS URL /saml/acs (HTTP-POST) | trusts the IdP certificate |
| IdP | https://idp.example.org | SSO URL /sso (HTTP-Redirect) | signs with its private key |
The ACS URL (Assertion Consumer Service) is the one address where the SP accepts assertions. The IdP sends an answer only to an ACS URL it has in that SP's metadata.
Step 1: the AuthnRequest rides a redirect
alice opens /reports without a session. The SP makes an <samlp:AuthnRequest>
with a fresh ID (_req1), its own entityID as Issuer and its ACS URL, and
remembers _req1 as pending for this browser. It does not call the IdP. It answers
302 Found: the request is deflated, base64-encoded and put in the Location URL as
SAMLRequest, next to RelayState=/reports (where to go afterwards). This is the
HTTP-Redirect binding. The browser follows it to the IdP
(Demo: SP-initiated login, end to end).
Step 2: the IdP answers with a signed Response
The IdP logs alice in on its own page, so only the IdP ever sees her password. Then it builds a
<samlp:Response> holding an <saml:Assertion> and signs the assertion with
its private key (an XML signature, RSA-SHA256). A Response is too big for a URL, so the IdP returns an HTML page
with a hidden form field SAMLResponse and a script that submits it at once to the ACS URL: the
HTTP-POST binding.
| Field | Says | Example |
|---|---|---|
Issuer | who wrote it | https://idp.example.org |
ds:Signature | proof that the IdP wrote it, unchanged | RSA-SHA256 over _as1 |
NameID | who the user is | alice@example.org |
Audience, Recipient, Destination | which SP it is for | https://sp.example.com/saml, its ACS URL |
NotOnOrAfter | until when it may be used | IssueInstant + 5 min |
InResponseTo | which request it answers | _req1 |
AuthnStatement | how and when she logged in | SessionIndex="_s1" |
Step 3: what the SP checks at the ACS URL
The Response came through the browser, so the browser (or whoever controls it) could have changed it, delayed it or swapped it. HTTPS does not help here: it only protects the hop, not the content. The SP therefore trusts nothing until four checks pass, and each failure case on this page fails exactly one of them.
| Check | Fails when | Demo |
|---|---|---|
| 1. Signature verifies with the IdP certificate from metadata | any byte of the assertion changed (NameID edited to admin@example.org) | assertion edited in the browser |
| 2. Destination, Recipient and Audience name this SP | the IdP made it for another SP (Mallory's wiki) | Mallory forwards a wiki assertion |
3. The SP clock is before NotOnOrAfter | it arrives after 5 minutes | form posted 10 min late |
4. InResponseTo is a request this browser started | nobody here asked for it (login CSRF) | Mallory posts her own Response |
Check 2 matters because every SP of the same IdP receives assertions about alice. Without it, a dishonest
SP could log in to all the others as alice with what it received. Check 4 stops the reverse: Mallory logs alice
in to Mallory's account, and alice then uploads her files to it. The SP also crosses _req1 off its
pending list, so the same Response cannot be posted twice. Real SPs add a few more checks: the
Issuer, NotBefore, a small clock-skew allowance, and a cache of assertion IDs already
used. See How a Digital Signature Works for how the signature check itself
works, and CSRF and SameSite Cookies for the general form of check 4's attack.
Two sessions: SSO and the app
After a login there are two cookies on two domains. idp_session on the IdP is what makes it
single sign-on: the next app that sends alice to the IdP gets an assertion without a password
prompt (Demo: SSO, already signed in at the IdP). sp_session on the SP is the app's own
login; every later request uses only that cookie, with no SAML at all. Ending one does not end the other: the
SP session ends button clears only the SP cookie, so the next login skips the password. See
JWT vs Session Cookies for how such an app session is kept.
SAML vs OpenID Connect
OpenID Connect solves the same problem on top of OAuth 2.0. Its IdP is called an OpenID Provider and its assertion is an ID token, a signed JSON Web Token.
| SAML 2.0 | OpenID Connect | |
|---|---|---|
| Format | XML, XML signature | JSON, JWT signed with JWS |
| Request | AuthnRequest in a redirect | /authorize?response_type=code&scope=openid |
| How the token reaches the app | through the browser, in a form POST to the ACS URL | usually a short code through the browser, then the ID token on a direct back-channel call |
| "For this app" field | Audience | aud = client_id |
| "Answers my request" field | InResponseTo | nonce (and state) |
| Expiry field | NotOnOrAfter | exp |
| Typical use | enterprise apps, the corporate login | consumer logins, mobile apps, APIs |
The checks are the same idea with different names. The big practical difference is the channel: in SAML the signed assertion always passes through the browser, so the signature carries all the trust; in the OpenID Connect code flow the app fetches the token from the provider directly. Kerberos is the older single sign-on of a company network: it hands out tickets instead of signed statements.
What the page leaves out
- IdP-initiated login (the user starts at an IdP portal; there is no AuthnRequest and no InResponseTo to check), and the Artifact binding, where the SP fetches the assertion over a back channel.
- Encrypted assertions (
EncryptedAssertion), signed AuthnRequests and signing the whole Response instead of the assertion. - Attributes (
AttributeStatement: groups, roles, email), NameID formats such as persistent and transient IDs. - Single Logout (
LogoutRequestto every SP of a session),ForceAuthnandIsPassive. - XML details: canonicalization, enveloped signatures and the XML signature wrapping attacks that careless parsers fall for.
- Real IDs are random 128-bit or longer values, and real clocks allow a little skew.