Single Sign-On & SAML — Auth

SSO (Single Sign-On): log in once at the IdP (Identity Provider, where you log in), then enter many SPs (Service Providers, the apps you visit). In the…

In many offices, you log in once in the morning, and then email, the calendar and the leave app all open without asking for a password again.

That's SSO (Single Sign-On): logging in once in one place, then entering many apps without typing a password again. SAML 2.0 is one protocol (a set of rules for how programs talk to each other) for SSO. SAML uses XML, a text format with tags like HTML. Picture the receptionist of an office building. She checks your ID card once, then gives you a signed letter of introduction for the office you want to visit. That office doesn't check your ID card again. The office only checks three things on the letter: the receptionist's signature is real, the letter is addressed to this office, and its time limit hasn't passed.

Three parties

Before any of this works, the SP and the IdP exchange metadata (setup data) once. The SP gives its entity ID, the SP's unique name (https://portal.school.test), and its ACS URL (Assertion Consumer Service), the address that receives assertions (https://portal.school.test/saml/acs). The IdP gives its login address (https://idp.foundation.test/sso) and its certificate, a file that holds the IdP's public key for checking its signature.

The flow, step by step

This is the SP-initiated flow, the flow that starts at the app. Times in SAML are always UTC, so 08:00 school time (UTC+7) is written 01:00. Exactly as in the Lab:

# From, to What happens
1 Browser to Portal GET /report with no session. The Portal builds an AuthnRequest and answers 302 to https://idp.foundation.test/sso?SAMLRequest=...&RelayState=%2Freport
2 Browser to IdP The IdP doesn't know Mr. Andi yet, so it shows its login page. Mr. Andi enters his password (and MFA). The IdP keeps a session of its own
3 IdP to browser The IdP builds a signed assertion, then sends a page with a form that is POSTed to the Portal's ACS URL automatically
4 Browser to Portal POST /saml/acs carrying SAMLResponse. The Portal checks the assertion. If it passes: the Portal creates a local session and answers 302 to /report

The AuthnRequest (the SP's login request) is small. Before it goes into the URL, this XML is compressed (DEFLATE) and then base64-encoded:

<samlp:AuthnRequest ID="_req7f3a91c2" Version="2.0" IssueInstant="2026-10-09T01:00:00Z"
    Destination="https://idp.foundation.test/sso"
    AssertionConsumerServiceURL="https://portal.school.test/saml/acs">
  <saml:Issuer>https://portal.school.test</saml:Issuer>
</samlp:AuthnRequest>

The assertion and what the SP checks

This is the assertion that arrives at the Portal's ACS (the signature part is shortened):

<saml:Assertion ID="_a91c55e0" IssueInstant="2026-10-09T01:00:12Z">
  <saml:Issuer>https://idp.foundation.test</saml:Issuer>
  <ds:Signature><!-- RSA-SHA256, certificate 4B:1E:9C:07:A2:F3:5D:88 --></ds:Signature>
  <saml:Subject>
    <saml:NameID>[email protected]</saml:NameID>
    <saml:SubjectConfirmationData InResponseTo="_req7f3a91c2"
        Recipient="https://portal.school.test/saml/acs" NotOnOrAfter="2026-10-09T01:05:12Z"/>
  </saml:Subject>
  <saml:Conditions NotBefore="2026-10-09T00:59:42Z" NotOnOrAfter="2026-10-09T01:05:12Z">
    <saml:AudienceRestriction><saml:Audience>https://portal.school.test</saml:Audience></saml:AudienceRestriction>
  </saml:Conditions>
  <saml:AttributeStatement>
    <saml:Attribute Name="role"><saml:AttributeValue>teacher</saml:AttributeValue></saml:Attribute>
    <saml:Attribute Name="department"><saml:AttributeValue>Maths</saml:AttributeValue></saml:Attribute>
  </saml:AttributeStatement>
</saml:Assertion>

Before trusting a single letter of it, the Portal runs six checks. All of them must pass:

Check Compared with In this assertion
Signature the IdP certificate from the metadata signed with certificate 4B:1E:..., a match
Issuer https://idp.foundation.test a match
Audience the Portal's entity ID, https://portal.school.test a match
Recipient the Portal's ACS URL a match
InResponseTo the AuthnRequest ID kept in the session _req7f3a91c2, a match
NotBefore, NotOnOrAfter the Portal server's clock when the POST arrives arrived at 01:00:13, inside the 5-minute window (the time range in which the assertion is valid)

When all of them pass, the NameID ([email protected]) is matched to a user in the Portal's database, and the role and department attributes are read. Then the Portal logs Mr. Andi in with a session of its own. If one fails, the assertion is rejected and no session is created. The Lab shows three causes of failure you often meet when setting up SSO:

Don't write the XML signature check yourself. Use a library: onelogin/php-saml in PHP (which several SAML packages for Laravel use as well), or @node-saml/node-saml in Node.

The second app

Five minutes later, Mr. Andi opens Digital Library. The library has no session yet either, so the library also sends an AuthnRequest to the IdP. The difference is in step 2: the IdP already has Mr. Andi's session, so there is no login page. The IdP immediately builds a new assertion with Audience https://library.school.test, then POSTs it to the library's ACS. So Mr. Andi isn't asked for a password again, but the letter is different. The Portal's assertion would be rejected at the library, because its audience isn't the library.

The exercise: three functions

The exercise uses JavaScript objects instead of XML, with the app named https://myapp.com and the IdP https://idp.corp.com/sso:

SAML or OIDC

OIDC (OpenID Connect) is a login layer on top of OAuth 2.0, the protocol behind "Log in with Google" in the Social Login lesson. Its goal is the same as SAML's: the IdP proves to the app who the user is.

SAML 2.0 OIDC
Published 2005 2014
Proof of identity an assertion, signed XML an ID token, a JWT (signed JSON)
How it reaches the app through the browser: a redirect, then a form POST to the ACS a redirect carrying a code, then the app's server exchanges that code directly with the IdP
Fits enterprise (large company) and B2B (company to company) integration, especially older systems new web and mobile apps

The practical rule: when a company customer asks "it must log in with SAML from our IdP", offer SAML. When you build from scratch, pick OIDC: JSON is lighter than XML, and there are more libraries for web and mobile.

⚠️ Why the SSO login gets refused, or lets the wrong one in

TL;DR: SSO (Single Sign-On): log in once at the IdP (Identity Provider, where you log in), then enter many SPs (Service Providers, the apps you visit). In the SP-initiated flow (it starts at the app), the SP sends an AuthnRequest (a login request) and keeps its ID. The IdP logs the user in, then POSTs an assertion to the ACS URL, the SP's address that receives assertions. An assertion is a signed XML document that says who the user is. The SP checks the signature, issuer (who issued it), audience (must be its own entity ID, the SP's unique name), recipient (must be its ACS URL), InResponseTo (must be that ID), and the NotBefore/NotOnOrAfter time window, before creating its own session. A second app asks for no password because the IdP still has a session, but it gets an assertion of its own. SAML (XML, 2005) for large companies and older systems, OIDC (JWT, on top of OAuth 2.0) for new apps.

⚡ IdP, SP and the assertion

Meaning: With SSO (Single Sign-On), you log in once, then enter many apps. SAML is one protocol (a set of rules for how programs talk to each other) for SSO. The IdP (Identity Provider) is where you log in, like the building's receptionist who checks your ID card once. The SP (Service Provider) is the app you want to enter, like the office you're visiting. The assertion is like the receptionist's signed letter of introduction for that office: an XML document from the IdP that says who the user is, signed by the IdP.

⚡ The flow starts at the app

Meaning: The SP sends an AuthnRequest (a login request) and keeps its ID in the session. The IdP asks the user to log in, then POSTs a signed assertion to the ACS URL, the SP's address that receives assertions. The SP checks that assertion, and only then creates a session of its own.

⚡ Six checks before trusting it

Write:

<saml:AudienceRestriction><saml:Audience>https://portal.school.test</saml:Audience></saml:AudienceRestriction>

Meaning: This line of the assertion names the app the assertion was made for (Audience). The SP runs six checks, and all must pass: the signature matches the IdP certificate (which holds its public key), the Issuer equals the IdP's address, the Audience equals the Portal's entity ID (its unique name), the Recipient equals the ACS URL, InResponseTo equals the stored AuthnRequest ID, and the server's clock is inside the NotBefore/NotOnOrAfter time window.

⚡ The second app

Guess first: Mr. Andi has logged in through the IdP to open School Portal. Five minutes later he opens Digital Library, a second SP that uses the same IdP. Is he asked for a password again?

No. The IdP still has Mr. Andi's session, so the IdP immediately builds a new assertion with the library's Audience. The Portal's assertion would be rejected at the library, because its audience isn't the library.

Meaning: SSO like this can also be built with OIDC (OpenID Connect), the login layer on top of OAuth 2.0 that "Log in with Google" uses. When a company customer asks for SAML from their IdP, use SAML. When you build from scratch, pick OIDC: JSON is lighter than XML, and there are more libraries.

What you will learn