Skip to content

Commit 14be558

Browse files
committed
generateServiceProviderMetadata: remove callbackUrl dependency
1 parent d578141 commit 14be558

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Config parameter details:
4545
* `callbackUrl`: full callbackUrl (overrides path/protocol if supplied)
4646
* `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`)
4747
* `protocol`: protocol for callback; will be combined with path and server host information to construct callback url if `callbackUrl` is not specified (default: `http://`)
48+
* `host`: host for callback; will be combined with path and protocol to construct callback url if `callbackUrl` is not specified (default: `localhost`)
4849
* `entryPoint`: identity provider entrypoint
4950
* `issuer`: issuer string to supply to identity provider
5051
* `cert`: see 'security and signatures'

lib/passport-saml/saml.js

+25-11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ SAML.prototype.initialize = function (options) {
2727
options.path = '/saml/consume';
2828
}
2929

30+
if (!options.host) {
31+
options.host = 'localhost';
32+
}
33+
3034
if (!options.issuer) {
3135
options.issuer = 'onelogin_saml';
3236
}
@@ -65,6 +69,25 @@ SAML.prototype.initialize = function (options) {
6569
return options;
6670
};
6771

72+
SAML.prototype.getProtocol = function (req) {
73+
return this.options.protocol || (req.protocol || 'http').concat('://');
74+
};
75+
76+
SAML.prototype.getCallbackUrl = function (req) {
77+
// Post-auth destination
78+
if (this.options.callbackUrl) {
79+
return this.options.callbackUrl;
80+
} else {
81+
var host;
82+
if (req.headers) {
83+
host = req.headers.host;
84+
} else {
85+
host = this.options.host;
86+
}
87+
return this.getProtocol(req) + host + this.options.path;
88+
}
89+
};
90+
6891
SAML.prototype.generateUniqueID = function () {
6992
var chars = "abcdef0123456789";
7093
var uniqueID = "";
@@ -88,8 +111,6 @@ SAML.prototype.generateAuthorizeRequest = function (req, isPassive, callback) {
88111
var self = this;
89112
var id = "_" + self.generateUniqueID();
90113
var instant = self.generateInstant();
91-
var protocol = self.options.protocol || (req.protocol || 'http').concat('://');
92-
var callbackUrl;
93114
var forceAuthn = self.options.forceAuthn || false;
94115

95116
Q.fcall(function() {
@@ -100,21 +121,14 @@ SAML.prototype.generateAuthorizeRequest = function (req, isPassive, callback) {
100121
}
101122
})
102123
.then(function(){
103-
// Post-auth destination
104-
if (self.options.callbackUrl) {
105-
callbackUrl = self.options.callbackUrl;
106-
} else {
107-
callbackUrl = protocol + req.headers.host + self.options.path;
108-
}
109-
110124
var request = {
111125
'samlp:AuthnRequest': {
112126
'@xmlns:samlp': 'urn:oasis:names:tc:SAML:2.0:protocol',
113127
'@ID': id,
114128
'@Version': '2.0',
115129
'@IssueInstant': instant,
116130
'@ProtocolBinding': 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
117-
'@AssertionConsumerServiceURL': callbackUrl,
131+
'@AssertionConsumerServiceURL': self.getCallbackUrl(req),
118132
'@Destination': self.options.entryPoint,
119133
'saml:Issuer' : {
120134
'@xmlns:saml' : 'urn:oasis:names:tc:SAML:2.0:assertion',
@@ -758,7 +772,7 @@ SAML.prototype.generateServiceProviderMetadata = function( decryptionCert ) {
758772
'@index': '1',
759773
'@isDefault': 'true',
760774
'@Binding': 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
761-
'@Location': this.options.callbackUrl
775+
'@Location': this.getCallbackUrl({})
762776
}
763777
},
764778
}

0 commit comments

Comments
 (0)