node-soap
node-soap copied to clipboard
Authentication while fetching wsdl file
Hi,
I stumbled over an issue, where a SOAP server would require username and password before serving the wsdl file.
Obviously the following did not work, as the client only exists after parsing of the wsdl file
soap.createClient(url,function(err, client) {
client.setSecurity(new soap.BasicAuthSecurity('HANS', 'WURST'))
}
I solved my issue quite dirty by adding an extra authentication header to the first wsdl-request. myClient:
var auth = "Basic " + new Buffer("HANS" + ":" + "WURST").toString("base64");
var myHeaders = {
"Authorization" : auth
}
soap.createClient(url, {exHeaders:myHeaders},function(err, client) {
... and patching the wsdl.js:
function open_wsdl(uri, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
var wsdl;
if (!/^http/.test(uri)) {
fs.readFile(uri, 'utf8', function(err, definition) {
if (err) {
callback(err)
} else {
wsdl = new WSDL(definition, uri, options);
wsdl.onReady(callback);
}
})
} else {
http.request(uri, null /* options */, function(err, response, definition) {
if (err) {
callback(err);
} else if (response && response.statusCode == 200) {
wsdl = new WSDL(definition, uri, options);
wsdl.onReady(callback);
} else {
callback(new Error('Invalid WSDL URL: ' + uri))
}
}, options.exHeaders); //<--- HERE!!!
}
return wsdl;
}
However I wonder if there is a better solution for this?
Best Regards
yep, needed this too!
You can also do this in a slightly less hacky way without patching:
var user = 'myuser';
var pass = 'mypass';
var url = 'http://'+user+':'+pass+'@host/path/to/wsdl';
soap.createClient(url,function(err, client) {
client.setSecurity(new soap.BasicAuthSecurity(user,pass));
client.someFunction(...);
}
You can solve the problem doing this:
var auth = "Basic " + new Buffer("HANS" + ":" + "WURST").toString("base64");
soap.createClient(url, { wsdl_headers: {Authorization: auth} }, function(err, client) {