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
- IdP (Identity Provider): where you log in, the receptionist. Checking the ID card stands for logging in with a password. The school foundation runs one IdP at
https://idp.foundation.test, for example Microsoft Entra ID, Google Workspace, or Keycloak. - SP (Service Provider): the app you want to enter, the office. Here there are two: School Portal and Digital Library.
- Assertion: the letter. An assertion is an XML document from the IdP that says who the user is, with their attributes (facts about the user, such as the role), and it is signed by the IdP.
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>
ID: the request's number. The Portal keeps it in the session, so it can match the answer later.Issuer: who is asking, the Portal's entity ID.Destination: where the request is sent, the IdP's login address.RelayState=%2Freportin the URL carries the target page, so after login Mr. Andi lands back on/report.
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:
- The SP server's clock runs 6 minutes fast.
- The entity ID is mistyped at the IdP:
https://portal.school.test/, with a trailing slash. - The IdP already signs with a new certificate, while the SP's metadata still holds the old one.
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:
buildAuthRequest(spEntityId, idpUrl, requestId)returns{ type: "AuthnRequest", id: requestId, issuer: spEntityId, destination: idpUrl }. It holds the same values as theID,Issuer, andDestinationin the AuthnRequest XML above.validateAssertion(assertion, spEntityId)comparesassertion.audiencewith the SP's entity ID, which is the Audience check. When they differ:{ valid: false, reason: "Wrong audience" }. When they are the same:{ valid: true, userId: assertion.subject }. The Recipient check, which compares with the ACS URL, isn't part of the exercise.extractRoles(assertion)returnsassertion.attributes.roles, or[]when there is none.
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
- Reading only the
NameIDand skipping the other checks. An assertion signed for another app, or one past its time, must still be rejected. All six checks are required. - A drifting server clock. The window is only a few minutes. Run NTP (a service that keeps the server's clock equal to standard time) on the SP's server, and allow a small tolerance (tens of seconds), not minutes.
- The IdP certificate changes without updating the SP's metadata. Every login fails the signature check. Import the new metadata before the IdP switches certificates.
- The same assertion accepted twice. Keep the
IDof every assertion already used until itsNotOnOrAfterpasses, and reject a second assertion with the sameID.
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.