Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generateServiceProviderMetadata: remove callbackUrl dependency #103

Merged
merged 1 commit into from
Aug 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Config parameter details:
* `callbackUrl`: full callbackUrl (overrides path/protocol if supplied)
* `path`: path to callback; will be combined with protocol and server host information to construct callback url if `callbackUrl` is not specified (default: `/saml/consume`)
* `protocol`: protocol for callback; will be combined with path and server host information to construct callback url if `callbackUrl` is not specified (default: `http://`)
* `host`: host for callback; will be combined with path and protocol to construct callback url if `callbackUrl` is not specified (default: `localhost`)
* `entryPoint`: identity provider entrypoint
* `issuer`: issuer string to supply to identity provider
* `cert`: see 'security and signatures'
Expand Down
41 changes: 25 additions & 16 deletions lib/passport-saml/saml.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ SAML.prototype.initialize = function (options) {
options.path = '/saml/consume';
}

if (!options.host) {
options.host = 'localhost';
}

if (!options.issuer) {
options.issuer = 'onelogin_saml';
}
Expand Down Expand Up @@ -65,6 +69,25 @@ SAML.prototype.initialize = function (options) {
return options;
};

SAML.prototype.getProtocol = function (req) {
return this.options.protocol || (req.protocol || 'http').concat('://');
};

SAML.prototype.getCallbackUrl = function (req) {
// Post-auth destination
if (this.options.callbackUrl) {
return this.options.callbackUrl;
} else {
var host;
if (req.headers) {
host = req.headers.host;
} else {
host = this.options.host;
}
return this.getProtocol(req) + host + this.options.path;
}
};

SAML.prototype.generateUniqueID = function () {
var chars = "abcdef0123456789";
var uniqueID = "";
Expand All @@ -88,8 +111,6 @@ SAML.prototype.generateAuthorizeRequest = function (req, isPassive, callback) {
var self = this;
var id = "_" + self.generateUniqueID();
var instant = self.generateInstant();
var protocol = self.options.protocol || (req.protocol || 'http').concat('://');
var callbackUrl;
var forceAuthn = self.options.forceAuthn || false;

Q.fcall(function() {
Expand All @@ -100,21 +121,14 @@ SAML.prototype.generateAuthorizeRequest = function (req, isPassive, callback) {
}
})
.then(function(){
// Post-auth destination
if (self.options.callbackUrl) {
callbackUrl = self.options.callbackUrl;
} else {
callbackUrl = protocol + req.headers.host + self.options.path;
}

var request = {
'samlp:AuthnRequest': {
'@xmlns:samlp': 'urn:oasis:names:tc:SAML:2.0:protocol',
'@ID': id,
'@Version': '2.0',
'@IssueInstant': instant,
'@ProtocolBinding': 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
'@AssertionConsumerServiceURL': callbackUrl,
'@AssertionConsumerServiceURL': self.getCallbackUrl(req),
'@Destination': self.options.entryPoint,
'saml:Issuer' : {
'@xmlns:saml' : 'urn:oasis:names:tc:SAML:2.0:assertion',
Expand Down Expand Up @@ -746,11 +760,6 @@ function processValidlySignedPostRequest(self, doc, callback) {
}

SAML.prototype.generateServiceProviderMetadata = function( decryptionCert ) {
if (!this.options.callbackUrl) {
throw new Error(
"Unable to generate service provider metadata when callbackUrl option is not set");
}

var metadata = {
'EntityDescriptor' : {
'@xmlns': 'urn:oasis:names:tc:SAML:2.0:metadata',
Expand All @@ -763,7 +772,7 @@ SAML.prototype.generateServiceProviderMetadata = function( decryptionCert ) {
'@index': '1',
'@isDefault': 'true',
'@Binding': 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
'@Location': this.options.callbackUrl
'@Location': this.getCallbackUrl({})
}
},
}
Expand Down
33 changes: 31 additions & 2 deletions test/tests.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.