Skip to content

Commit

Permalink
Add X-Forwarded-Host support
Browse files Browse the repository at this point in the history
  • Loading branch information
ziluvatar committed Feb 26, 2018
1 parent 6b91af4 commit ab49e06
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
14 changes: 7 additions & 7 deletions lib/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ function getEndpointAddress (req, endpointPath) {
var protocol = req.headers['x-iisnode-https'] && req.headers['x-iisnode-https'] == 'on' ?
'https' :
(req.headers['x-forwarded-proto'] || req.protocol);

return protocol + '://' + req.headers['host'] + endpointPath;
var host = req.headers['x-forwarded-host'] || req.headers['host'];
return protocol + '://' + host + endpointPath;
}

/**
* SAML metadata endpoint
*
* This endpoint returns a SAML metadata document.
*
*
* You should expose this endpoint in an address like:
*
* 'https://your-saml-server.com/FederationMetadata/2007-06/FederationMetadata.xml
*
*
* options:
* - issuer string
* - cert the public certificate
Expand All @@ -32,7 +32,7 @@ function getEndpointAddress (req, endpointPath) {
* - postEndpointPath optional, location value for HTTP-POST binding (SingleSignOnService)
* - logoutEndpointPaths.redirect optional, location value for HTTP-Redirect binding (SingleLogoutService)
* - logoutEndpointPaths.post optional, location value for HTTP-POST binding (SingleLogoutService)
*
*
* @param {[type]} options [description]
* @return {[type]} [description]
*/
Expand All @@ -54,7 +54,7 @@ function metadataMiddleware (options) {
return function (req, res) {
var redirectEndpoint = getEndpointAddress(req, options.redirectEndpointPath);
var postEndpoint = getEndpointAddress(req, options.postEndpointPath);

options.logoutEndpointPaths = options.logoutEndpointPaths || { redirect: '/logout' };

var logoutEndpoints = {};
Expand All @@ -63,7 +63,7 @@ function metadataMiddleware (options) {
logoutEndpoints[binding] = getEndpointAddress(req, options.logoutEndpointPaths[binding]);
}
});

res.set('Content-Type', 'application/xml');

res.send(templates.metadata({
Expand Down
39 changes: 36 additions & 3 deletions test/metadata.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('samlp metadata', function () {
before(function (done) {
server.start(done);
});

after(function (done) {
server.close(done);
});
Expand All @@ -24,7 +24,7 @@ describe('samlp metadata', function () {
var doc, content;
before(function (done) {
request.get({
jar: request.jar(),
jar: request.jar(),
uri: 'http://localhost:5050/samlp/FederationMetadata/2007-06/FederationMetadata.xml'
}, function (err, response, b){
if(err) return done(err);
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('samlp metadata', function () {
it('sholud have the logout endpoint url', function(){
expect(doc.getElementsByTagName('SingleSignOnService')[0].getAttribute('Binding'))
.to.equal('urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect');

expect(doc.getElementsByTagName('SingleLogoutService')[0].getAttribute('Location'))
.to.equal('http://localhost:5050/logout');
});
Expand All @@ -79,4 +79,37 @@ describe('samlp metadata', function () {
});

});

describe('request to metadata with proxy', function () {
var doc;
before(function (done) {
request.get({
jar: request.jar(),
uri: 'http://localhost:5050/samlp/FederationMetadata/2007-06/FederationMetadata.xml',
headers: {
'X-Forwarded-Host': 'myserver.com'
}
}, function (err, response, b) {
if (err) return done(err);
doc = new xmldom.DOMParser().parseFromString(b).documentElement;
done();
});
});

it('sholud have the redirect endpoint url with the forwarded host', function () {
expect(doc.getElementsByTagName('SingleSignOnService')[0].getAttribute('Location'))
.to.equal('http://myserver.com/samlp/123');
});

it('sholud have the POST endpoint url with the forwarded host', function () {
expect(doc.getElementsByTagName('SingleSignOnService')[1].getAttribute('Location'))
.to.equal('http://myserver.com/login/callback');
});

it('sholud have the logout endpoint url with the forwarded host', function () {
expect(doc.getElementsByTagName('SingleLogoutService')[0].getAttribute('Location'))
.to.equal('http://myserver.com/logout');
});

});
});

0 comments on commit ab49e06

Please sign in to comment.