Gathering detailed insights and metrics for saml2-js
Gathering detailed insights and metrics for saml2-js
Gathering detailed insights and metrics for saml2-js
Gathering detailed insights and metrics for saml2-js
Node module to abstract away the complexities of the SAML protocol behind an easy to use interface.
npm install saml2-js
Typescript
Module System
Min. Node Version
Node Version
NPM Version
95.8
Supply Chain
97.8
Quality
87.1
Maintenance
100
Vulnerability
100
License
CoffeeScript (99.95%)
JavaScript (0.05%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
360 Stars
381 Commits
168 Forks
70 Watchers
3 Branches
42 Contributors
Updated on Jul 11, 2025
Latest Version
4.0.4
Package Id
saml2-js@4.0.4
Unpacked Size
238.65 kB
Size
70.10 kB
File Count
48
NPM Version
6.14.16
Node Version
12.22.12
Published on
Jul 07, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
This library is currently in maintenance mode. Until further notice, the primary directive is to handle bug reports and security issues with this library.
Any library alternatives and suggestions can be filed under an issue.
saml2-js
is a node module that abstracts away the complexities of the SAML protocol behind an easy to use interface. It achieves this this by helping you implement a service provider for the SAML protocol. It currently does not implement the features to act as an identity provider.
Install with npm.
1 npm install saml2-js --save
Include the SAML library.
1 var saml2 = require('saml2-js');
This library exports two constructors.
ServiceProvider
- Represents a service provider that relies on a trusted IdentityProvider
for authentication and authorization in the SAML flow.IdentityProvider
- Represents an online service that authenticates users in the SAML flow.Note: Some options can be set on the SP, IdP, and/or on a per-method basis. For the options that are set in multiple places, they are overridden in the following order: per-method basis overrides IdP which overrides SP.
Represents a service provider that relies on a trusted IdentityProvider
for authentication and authorization in the SAML flow.
An object that can contain the below options. All options are strings, unless specified otherwise. See note for more information on options.
entity_id
- Required - Unique identifier for the service provider, often the URL of the metadata file.private_key
- Required - (PEM format string) - Private key for the service provider.certificate
- Required - (PEM format string) - Certificate for the service provider.assert_endpoint
- Required - URL of service provider assert endpoint.alt_private_keys
- (Array of PEM format strings) - Additional private keys to use when attempting to decrypt responses. Useful for adding backward-compatibility for old certificates after a rollover.alt_certs
- (Array of PEM format strings) - Additional certificates to expose in the SAML metadata. Useful for staging new certificates for rollovers.audience
- (String or RegExp) — If set, at least one of the <Audience>
values within the <AudienceRestriction>
condition of a SAML authentication response must match. Defaults to entity_id
.notbefore_skew
- (Number) – To account for clock skew between IdP and SP, accept responses with a NotBefore condition ahead of the current time (according to our clock) by this number of seconds. Defaults to 1. Set it to 0 for optimum security but no tolerance for clock skew.force_authn
- (Boolean) - If true, forces re-authentication of users even if the user has a SSO session with the IdP. This can also be configured on the IdP or on a per-method basis.auth_context
- Specifies AuthnContextClassRef
. This can also be configured on a per-method basis.nameid_format
- Format for Name ID. This can also be configured on a per-method basis.sign_get_request
- (Boolean) - If true, signs the request. This can also be configured on the IdP or on a per-method basis.allow_unencrypted_assertion
- (Boolean) - If true, allows unencrypted assertions. This can also be configured on the IdP or on a per-method basis.create_login_request_url(IdP, options, cb)
- Get a URL to initiate a login.redirect_assert(IdP, options, cb)
- Gets a SAML response object if the login attempt is valid, used for redirect binding.post_assert(IdP, options, cb)
- Gets a SAML response object if the login attempt is valid, used for post binding.create_logout_request_url(IdP, options, cb)
- Creates a SAML Request URL to initiate a user logout.create_logout_response_url(IdP, options, cb)
- Creates a SAML Response URL to confirm a successful IdP initiated logout.create_metadata()
- Returns the XML metadata used during the initial SAML configuration.1 2 var sp_options = { 3 entity_id: "https://sp.example.com/metadata.xml", 4 private_key: fs.readFileSync("key-file.pem").toString(), 5 certificate: fs.readFileSync("cert-file.crt").toString(), 6 assert_endpoint: "https://sp.example.com/assert", 7 force_authn: true, 8 auth_context: { comparison: "exact", class_refs: ["urn:oasis:names:tc:SAML:1.0:am:password"] }, 9 nameid_format: "urn:oasis:names:tc:SAML:2.0:nameid-format:transient", 10 sign_get_request: false, 11 allow_unencrypted_assertion: true 12 } 13 14 // Call service provider constructor with options 15 var sp = new saml2.ServiceProvider(sp_options); 16 17 // Example use of service provider. 18 // Call metadata to get XML metatadata used in configuration. 19 var metadata = sp.create_metadata(); 20
Get a URL to initiate a login.
Takes the following arguments:
IdP
- IdPoptions
- An object that can contain the below options. All options are strings, unless specified otherwise. See note for more information on options.
relay_state
- SAML relay state.auth_context
- Specifies AuthnContextClassRef
. This can also be configured on the SP.nameid_format
- Format for Name ID. This can also be configured on the SP.force_authn
- (Boolean) - If true, forces re-authentication of users even if the user has a SSO session with the IdP. This can also be configured on the IdP or SP.sign_get_request
- (Boolean) - If true, signs the request. This can also be configured on the IdP or SP.cb(error, login_url, request_id)
- Callback called with the login URL and ID of the request.Gets a SAML response object if the login attempt is valid, used for redirect binding.
Takes the following arguments:
IdP
- IdPoptions
- An object that can contain the below options. All options are strings, unless specified otherwise. See note for more information on options.
request_body
- (Object) - An object containing the parsed query string parameters. This object should contain the value for either a SAMLResponse
or SAMLRequest
.allow_unencrypted_assertion
- (Boolean) - If true, allows unencrypted assertions. This can also be configured on the IdP or SP.require_session_index
- (Boolean) - If false, allow the assertion to be valid without a SessionIndex
attribute on the AuthnStatement
node.cb(error, response)
- Callback called with the request response.1{ response_header: 2 { id: '_abc-1', 3 destination: 'https://sp.example.com/assert', 4 in_response_to: '_abc-2' }, 5 type: 'authn_response', 6 user: 7 { name_id: 'nameid', 8 session_index: '_abc-3', 9 attributes: 10 { 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname': [ 'Test' ] } } }
Gets a SAML response object if the login attempt is valid, used for post binding.
Takes the following arguments:
IdP
- IdPoptions
- An object that can contain the below options. All options are strings, unless specified otherwise. See note for more information on options.
request_body
- (Object) - An object containing the parsed query string parameters. This object should contain the value for either a SAMLResponse
or SAMLRequest
.allow_unencrypted_assertion
- (Boolean) - If true, allows unencrypted assertions. This can also be configured on the IdP or SP.require_session_index
- (Boolean) - If false, allow the assertion to be valid without a SessionIndex
attribute on the AuthnStatement
node.audience
- (String or RegExp) — If set, at least one of the <Audience>
values within the <AudienceRestriction>
condition of a SAML authentication response must match. Defaults to entity_id
.notbefore_skew
- (Number) – To account for clock skew between IdP and SP, accept responses with a NotBefore condition ahead of the current time (according to our clock) by this number of seconds. Defaults to 1. Set it to 0 for optimum security but no tolerance for clock skew.cb(error, response)
- Callback called with the request response.Creates a SAML Request URL to initiate a user logout.
Takes the following arguments:
IdP
- IdP. Note: Can pass sso_logout_url
instead of IdP.options
- An object that can contain the below options. All options are strings, unless specified otherwise. See note for more information on options.
name_id
- Format for Name ID. This can also be configured on a per-method basis.session_index
- Session index to use for creating logout request.allow_unencrypted_assertion
- (Boolean) - If true, allows unencrypted assertions. This can also be configured on the IdP or SP.sign_get_request
- (Boolean) - If true, signs the request. This can also be configured on the IdP or SP.relay_state
- SAML relay state.cb(error, request_url)
- Callback called with the logout request url.Creates a SAML Response URL to confirm a successful IdP initiated logout.
Takes the following arguments:
IdP
- IdP. Note: Can pass sso_logout_url
instead of IdP.options
- An object that can contain the below options. All options are strings, unless specified otherwise. See note for more information on options.
cb(error, response_url)
- Callback called with the logout response url.Returns the XML metadata used during the initial SAML configuration.
Represents an online service that authenticates users in the SAML flow.
Returns no functions, exists solely to be passed to an SP function.
An object that can contain the below options. All options are strings, unless specified otherwise. See note for more information on options.
sso_login_url
- Required - Login url to use during a login request.sso_logout_url
- Required - Logout url to use during a logout request.certificates
- Required - (PEM format string or array of PEM format strings) - Certificate or certificates (array of certificate) for the identity provider.force_authn
- (Boolean) - If true, forces re-authentication of users even if the user has a SSO session with the IdP. This can also be configured on the SP or on a per-method basis.sign_get_request
- (Boolean) - If true, signs the request. This can also be configured on the [SP or on a per-method basis.allow_unencrypted_assertion
- (Boolean) - If true, allows unencrypted assertions. This can also be configured on the SP or on a per-method basis.1 2 // Initialize options object 3 var idp_options = { 4 sso_login_url: "https://idp.example.com/login", 5 sso_logout_url: "https://idp.example.com/logout", 6 certificates: [fs.readFileSync("cert-file1.crt").toString(), fs.readFileSync("cert-file2.crt").toString()], 7 force_authn: true, 8 sign_get_request: false, 9 allow_unencrypted_assertion: false 10 }; 11 12 // Call identity provider constructor with options 13 var idp = new saml2.IdentityProvider(idp_options); 14 15 // Example usage of identity provider. 16 // Pass identity provider into a service provider function with options and a callback. 17 sp.post_assert(idp, {}, callback); 18
Library users will need to implement a set of URL endpoints, here is an example of express endpoints.
1var saml2 = require('saml2-js'); 2var fs = require('fs'); 3var express = require('express'); 4var app = express(); 5// If you're using express <4.0: 6// var bodyParser = require('body-parser'); 7// app.use(bodyParser.urlencoded({ 8// extended: true 9// })); 10app.use(express.urlencoded()); 11 12// Create service provider 13var sp_options = { 14 entity_id: "https://sp.example.com/metadata.xml", 15 private_key: fs.readFileSync("key-file.pem").toString(), 16 certificate: fs.readFileSync("cert-file.crt").toString(), 17 assert_endpoint: "https://sp.example.com/assert" 18}; 19var sp = new saml2.ServiceProvider(sp_options); 20 21// Create identity provider 22var idp_options = { 23 sso_login_url: "https://idp.example.com/login", 24 sso_logout_url: "https://idp.example.com/logout", 25 certificates: [fs.readFileSync("cert-file1.crt").toString(), fs.readFileSync("cert-file2.crt").toString()] 26}; 27var idp = new saml2.IdentityProvider(idp_options); 28 29// ------ Define express endpoints ------ 30 31// Endpoint to retrieve metadata 32app.get("/metadata.xml", function(req, res) { 33 res.type('application/xml'); 34 res.send(sp.create_metadata()); 35}); 36 37// Starting point for login 38app.get("/login", function(req, res) { 39 sp.create_login_request_url(idp, {}, function(err, login_url, request_id) { 40 if (err != null) 41 return res.send(500); 42 res.redirect(login_url); 43 }); 44}); 45 46// Variables used in login/logout process 47var name_id, session_index; 48 49// Assert endpoint for when login completes 50app.post("/assert", function(req, res) { 51 var options = {request_body: req.body}; 52 sp.post_assert(idp, options, function(err, saml_response) { 53 if (err != null) 54 return res.send(500); 55 56 // Save name_id and session_index for logout 57 // Note: In practice these should be saved in the user session, not globally. 58 name_id = saml_response.user.name_id; 59 session_index = saml_response.user.session_index; 60 61 res.send("Hello #{name_id}! session_index: #{session_index}."); 62 }); 63}); 64 65// Starting point for logout 66app.get("/logout", function(req, res) { 67 var options = { 68 name_id: name_id, 69 session_index: session_index 70 }; 71 72 sp.create_logout_request_url(idp, options, function(err, logout_url) { 73 if (err != null) 74 return res.send(500); 75 res.redirect(logout_url); 76 }); 77}); 78 79app.listen(3000); 80
7.7/10
Summary
Authentication bypass via incorrect XML canonicalization and DOM traversal in saml2-js
Affected Versions
>= 2.0.0, < 2.0.2
Patched Versions
2.0.2
6.8/10
Summary
Authentication Bypass in saml2-js
Affected Versions
< 2.0.5
Patched Versions
2.0.5
7.7/10
Summary
Authentication bypass via incorrect XML canonicalization and DOM traversal in saml2-js
Affected Versions
< 1.12.4
Patched Versions
1.12.4
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
6 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 5
Reason
Found 4/9 approved changesets -- score normalized to 4
Reason
branch protection is not maximal on development and all release branches
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-14
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More