wsfed
Version:
WSFed server middleware
66 lines (55 loc) • 2.14 kB
JavaScript
var utils = require('./utils');
var templates = require('./templates');
var PassportProfileMapper = require('./claims/PassportProfileMapper');
var URL_PATH = '/FederationMetadata/2007-06/FederationMetadata.xml';
var encoders = require('./encoders');
function getEndpointAddress (req, endpointPath) {
endpointPath = endpointPath ||
(req.originalUrl.substr(0, req.originalUrl.length - URL_PATH.length));
return utils.getBaseUrl(req) + endpointPath;
}
/**
* WSFederation metadata endpoint
*
* This endpoint returns a wsfederation metadata document.
*
* You should expose this endpoint in an address like:
*
* 'https://your-wsfederation-server.com/FederationMetadata/2007-06/FederationMetadata.xml
*
* options:
* - issuer string
* - cert the public certificate
* - profileMapper a function that given a user returns a claim based identity, also contains the metadata. By default maps from Passport.js user schema (PassportProfile).
* - endpointPath optional, defaults to the root of the fed metadata document.
* - mexEndpoint optional, url of the wsfederation MEX endpoint metadata document.
*
* @param {[type]} options [description]
* @return {[type]} [description]
*/
function metadataMiddleware (options) {
//claimTypes, issuer, pem, endpointPath
options = options || {};
if(!options.issuer) {
throw new Error('options.issuer is required');
}
if(!options.cert) {
throw new Error('options.cert is required');
}
var claimTypes = (options.profileMapper || PassportProfileMapper).prototype.metadata;
var issuer = options.issuer;
var pem = encoders.removeHeaders(options.cert);
return function (req, res) {
var endpoint = getEndpointAddress(req, options.endpointPath);
var mexEndpoint = options.mexEndpoint ? getEndpointAddress(req, options.mexEndpoint) : '';
res.set('Content-Type', 'application/xml');
res.send(templates.metadata({
claimTypes: claimTypes,
pem: pem,
issuer: issuer,
endpoint: endpoint,
mexEndpoint: mexEndpoint
}).replace(/\n/g, ''));
};
}
module.exports = metadataMiddleware;