Clever/saml2

Name: saml2

Owner: Clever

Description: Node module to abstract away the complexities of the SAML protocol behind an easy to use interface.

Created: 2014-03-12 00:23:44.0

Updated: 2018-01-18 10:36:50.0

Pushed: 2017-11-30 01:21:32.0

Homepage:

Size: 329

Language: CoffeeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

SAML2-js

CircleCI

saml2-js is a node module that abstracts away the complexities of the SAML protocol behind an easy to use interface.

Usage

Install with npm.

m install saml2-js --save

Include the SAML library.

r saml2 = require('saml2-js');
Documentation

This library exports two constructors.

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.

ServiceProvider(options)

Represents a service provider that relies on a trusted IdentityProvider for authentication and authorization in the SAML flow.

Options

An object that can contain the below options. All options are strings, unless specified otherwise. See note for more information on options.

Returns the following functions Example
r sp_options = {
entity_id: "https://sp.example.com/metadata.xml",
private_key: fs.readFileSync("key-file.pem").toString(),
certificate: fs.readFileSync("cert-file.crt").toString(),
assert_endpoint: "https://sp.example.com/assert",
force_authn: true,
auth_context: { comparison: "exact", class_refs: ["urn:oasis:names:tc:SAML:1.0:am:password"] },
nameid_format: "urn:oasis:names:tc:SAML:2.0:nameid-format:transient",
sign_get_request: false,
allow_unencrypted_assertion: true


 Call service provider constructor with options
r sp = new saml2.ServiceProvider(sp_options);

 Example use of service provider.
 Call metadata to get XML metatadata used in configuration.
r metadata = sp.create_metadata();
Service provider function definitions

create_login_request_url(IdP, options, cb)

Get a URL to initiate a login.

Takes the following arguments:

redirect_assert(IdP, options, cb)

Gets a SAML response object if the login attempt is valid, used for redirect binding.

Takes the following arguments:

Example of the SAML assert response returned:

response_header:
 { id: '_abc-1',
   destination: 'https://sp.example.com/assert',
   in_response_to: '_abc-2' },
type: 'authn_response',
user:
 { name_id: 'nameid',
   session_index: '_abc-3',
   attributes:
    { 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname': [ 'Test' ] } } }

post_assert(IdP, options, cb)

Gets a SAML response object if the login attempt is valid, used for post binding.

Takes the following arguments:

create_logout_request_url(IdP, options, cb)

Creates a SAML Request URL to initiate a user logout.

Takes the following arguments:

create_logout_response_url(IdP, options, cb)

Creates a SAML Response URL to confirm a successful IdP initiated logout.

Takes the following arguments:

create_metadata()

Returns the XML metadata used during the initial SAML configuration.

IdentityProvider(options)

Represents an online service that authenticates users in the SAML flow.

Returns no functions, exists solely to be passed to an SP function.

Options

An object that can contain the below options. All options are strings, unless specified otherwise. See note for more information on options.

Example
 Initialize options object
r idp_options = {
sso_login_url: "https://idp.example.com/login",
sso_logout_url: "https://idp.example.com/logout",
certificates: [fs.readFileSync("cert-file1.crt").toString(), fs.readFileSync("cert-file2.crt").toString()],
force_authn: true,
sign_get_request: false,
allow_unencrypted_assertion: false


 Call identity provider constructor with options
r idp = new saml2.IdentityProvider(idp_options);

 Example usage of identity provider.
 Pass identity provider into a service provider function with options and a callback.
.post_assert(idp, {}, callback);
Example: Express implementation

Library users will need to implement a set of URL endpoints, here is an example of express endpoints.

saml2 = require('saml2-js');
fs = require('fs');
express = require('express');
app = express();
bodyParser = require('body-parser');
use(bodyParser.urlencoded({
tended: true


reate service provider
sp_options = {
tity_id: "https://sp.example.com/metadata.xml",
ivate_key: fs.readFileSync("key-file.pem").toString(),
rtificate: fs.readFileSync("cert-file.crt").toString(),
sert_endpoint: "https://sp.example.com/assert"

sp = new saml2.ServiceProvider(sp_options);

reate identity provider
idp_options = {
o_login_url: "https://idp.example.com/login",
o_logout_url: "https://idp.example.com/logout",
rtificates: [fs.readFileSync("cert-file1.crt").toString(), fs.readFileSync("cert-file2.crt").toString()]

idp = new saml2.IdentityProvider(idp_options);

----- Define express endpoints ------

ndpoint to retrieve metadata
get("/metadata.xml", function(req, res) {
s.type('application/xml');
s.send(sp.create_metadata());


tarting point for login
get("/login", function(req, res) {
.create_login_request_url(idp, {}, function(err, login_url, request_id) {
if (err != null)
  return res.send(500);
res.redirect(login_url);
;


ssert endpoint for when login completes
post("/assert", function(req, res) {
r options = {request_body: req.body};
.post_assert(idp, options, function(err, saml_response) {
if (err != null)
  return res.send(500);

// Save name_id and session_index for logout
// Note:  In practice these should be saved in the user session, not globally.
name_id = saml_response.user.name_id;
session_index = saml_response.user.session_index;

res.send("Hello #{saml_response.user.name_id}!");
;


tarting point for logout
get("/logout", function(req, res) {
r options = {
name_id: name_id,
session_index: session_index


.create_logout_request_url(idp, options, function(err, logout_url) {
if (err != null)
  return res.send(500);
res.redirect(logout_url);
;


listen(3000);

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.