node-soap icon indicating copy to clipboard operation
node-soap copied to clipboard

Authentication while fetching wsdl file

Open Sgt-Pepper opened this issue 14 years ago • 3 comments

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

Sgt-Pepper avatar May 14 '12 16:05 Sgt-Pepper

yep, needed this too!

djMax avatar Jun 02 '12 14:06 djMax

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(...);
}

vincent99 avatar Aug 28 '12 22:08 vincent99

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) {

glucena avatar Jan 23 '15 11:01 glucena