SAML
Security Assertion Markup Language
An XML-based authentication/authorization protocol. The enterprise SSO standard.
Security Assertion Markup Language
An XML-based authentication/authorization protocol. The enterprise SSO standard.
SAML (Security Assertion Markup Language) is an XML-based authentication/authorization protocol standardized by OASIS in 2005. It is mainly used to implement SSO (Single Sign-On) in enterprise environments. Once you log in, you are automatically authenticated to multiple services (Salesforce, Workday, Slack, etc.).
The core components of SAML are the Identity Provider (IdP), the Service Provider (SP), and the Assertion. The IdP is the central authentication server (Okta, Azure AD, OneLogin, etc.) responsible for user authentication. The SP is the service that requests authentication. The Assertion is an XML document issued by the IdP that contains user information and authentication status.
There are two SAML flows: SP-Initiated and IdP-Initiated. SP-Initiated is the flow in which the user accesses the SP โ the SP redirects a SAML Request to the IdP โ the user logs in at the IdP โ the IdP sends a SAML Response to the SP โ the SP verifies the Assertion and creates a session.
SAML is optimized for browser-based web applications, and its security is strong thanks to XML signatures and encryption. However, it has drawbacks such as XML parsing overhead, limited mobile support, and complex configuration, so many new projects choose OAuth 2.0/OIDC. Even so, SAML is still essential for integrating existing enterprise systems.
// Node.js SAML Service Provider (passport-saml ์ฌ์ฉ)
import passport from 'passport';
import { Strategy as SamlStrategy } from '@node-saml/passport-saml';
// SAML Strategy ์ค์
passport.use(new SamlStrategy(
{
// Service Provider ์ค์
path: '/auth/saml/callback', // ACS (Assertion Consumer Service) URL
entryPoint: 'https://idp.example.com/sso/saml', // IdP SSO URL
issuer: 'https://myapp.example.com', // SP Entity ID
// IdP ์ธ์ฆ์ (SAML Response ์๋ช
๊ฒ์ฆ์ฉ)
cert: fs.readFileSync('./idp-certificate.pem', 'utf-8'),
// ์ต์
wantAssertionsSigned: true, // Assertion ์๋ช
ํ์
signatureAlgorithm: 'sha256',
identifierFormat: 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress',
},
// ๊ฒ์ฆ ์ฝ๋ฐฑ - Assertion์์ ์ฌ์ฉ์ ์ ๋ณด ์ถ์ถ
async (profile, done) => {
try {
// profile์์ ์ฌ์ฉ์ ์ ๋ณด ์ถ์ถ
const user = await findOrCreateUser({
email: profile.nameID, // SAML NameID
firstName: profile['firstName'], // Custom Attribute
lastName: profile['lastName'],
groups: profile['groups'] || [], // ๊ทธ๋ฃน/์ญํ ์ ๋ณด
});
return done(null, user);
} catch (error) {
return done(error);
}
}
));
// ๋ผ์ฐํธ ์ค์
const app = express();
// SAML ๋ก๊ทธ์ธ ์์ (SP-Initiated)
app.get('/auth/saml/login',
passport.authenticate('saml', {
failureRedirect: '/login',
failureFlash: true
})
);
// SAML Callback (ACS - Assertion Consumer Service)
app.post('/auth/saml/callback',
passport.authenticate('saml', {
failureRedirect: '/login',
failureFlash: true
}),
(req, res) => {
// ์ธ์ฆ ์ฑ๊ณต ํ ๋ฆฌ๋ค์ด๋ ํธ
res.redirect(req.session.returnTo || '/dashboard');
}
);
// SAML ๋ฉํ๋ฐ์ดํฐ ์ ๊ณต (IdP ๋ฑ๋ก ์ ํ์)
app.get('/auth/saml/metadata', (req, res) => {
const metadata = samlStrategy.generateServiceProviderMetadata(
fs.readFileSync('./sp-certificate.pem', 'utf-8'), // SP ๊ณต๊ฐ ์ธ์ฆ์
fs.readFileSync('./sp-private-key.pem', 'utf-8') // SP ๋น๋ฐํค
);
res.type('application/xml');
res.send(metadata);
});
// SAML ๋ก๊ทธ์์ (Single Logout)
app.get('/auth/saml/logout', (req, res) => {
samlStrategy.logout(req, (err, requestUrl) => {
if (err) return res.status(500).send(err);
req.logout();
res.redirect(requestUrl); // IdP ๋ก๊ทธ์์ URL๋ก ๋ฆฌ๋ค์ด๋ ํธ
});
});
In enterprise integration:
"A large enterprise customer has asked for Okta SSO integration. Our app needs to act as a SAML SP. We just need to provide the metadata XML and configure the ACS URL. Also check the Attribute Mapping."
In a technical interview:
"What's the difference between SAML and OAuth?" - "SAML is centered on authentication, is XML-based, and is mainly used for enterprise SSO. OAuth is an authorization framework that is JSON-based and suited to API access control. OIDC adds an authentication layer on top of OAuth."
In troubleshooting:
"SAML Response validation keeps failing." - "It may be a time synchronization issue. Because of the NotBefore/NotOnOrAfter conditions, validation fails even if the server time is off by just a few minutes. Check your NTP configuration and try increasing the clock skew tolerance."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free