Setting Authorization in Node.js SOAP Client

21,562

Solution 1

You can provide username and password like this:

var soap = require('soap');
var url = 'your WSDL url';
var auth = "Basic " + new Buffer("your username" + ":" + "your password").toString("base64");

soap.createClient(url, { wsdl_headers: {Authorization: auth} }, function(err, client) {
});

(derived from https://github.com/vpulim/node-soap/issues/56, thank you Gabriel Lucena https://github.com/glucena)

Solution 2

Another option to add basic authentication is using client.addHttpHeader. I tried both setSecurity and setting wsdl_headers but neither worked for me when authenticating to Cisco CUCM AXL.

Here is what worked for me:

var soap = require('soap');
var url = 'AXLAPI.wsdl';  // Download this file and xsd files from cucm admin page
var auth = "Basic " + new Buffer("your username" + ":" + "your password").toString("base64");
soap.createClient(url,function(err,client){
  client.addHttpHeader('Authorization',auth);
});

Solution 3

Just to share what I've read from https://github.com/vpulim/node-soap:

var soap = require('soap');
var url = 'your WSDL url';

soap.createClient(url, function(err, client) {
    client.setSecurity(new soap.BasicAuthSecurity('your username','your password'));
});

Solution 4

You need to set the username and password by passing the authorisation to the wsdl_headers object e.g

var auth = "Basic " + new Buffer('username' + ':' + 'password').toString("base64");

var client = Soap.createClient('wsdlUrl', { wsdl_headers: { Authorization: auth } }, (err, client) => {
    if (err) {
        throw err;
    } else {
        client.yourMethod();
    }
});

Solution 5

A small tweak to the existing answers: you can use your security object to create the header for the WSDL request too, e.g.

const security = new soap.BasicAuthSecurity(username, password);
const wsdl_headers = {};
security.addHeaders(wsdl_headers);
soap.createClientAsync(url, { wsdl_headers }).then((err, client) => {
    client.setSecurity(security);
    // etc.
});

Or if you're using something more complicated than BasicAuthSecurity you may also need to set wsdl_options from the security object, e.g.

const security = new soap.NTLMSecurity(username, password, domain, workstation);
const wsdl_headers = {}, wsdl_options = {};
security.addHeaders(wsdl_headers);
security.addOptions(wsdl_options);
soap.createClientAsync(url, { wsdl_headers, wsdl_options }).then((err, client) => {
    client.setSecurity(security);
    // etc.
});
Share:
21,562
Kamalakannan J
Author by

Kamalakannan J

Certified AWS Developer Associate and Solutions Architect Associate. TLDR; 8+ years experience with Node.js and 2+ years experience with React. :) Being in love with JavaScript since 2013. Worked both on MEAN and MERN stack. Love to work with AWS too. Have good experience in Android app development(worked on 2 android apps). Tried lots of tools from design to deployment. I evolve myself by not restricting to any specific language and platform. I love learning.

Updated on July 09, 2022

Comments

  • Kamalakannan J
    Kamalakannan J almost 2 years

    I want to access a WSDL service through SOAP Client in Node.js. I used soap node module. But I can't able to find any documentation to set username and password. I'm not going to create SOAP server, I just want SOAPClient which is similar to PHP's SoapClient, using which I should able to access the WSDL service.

    Update:

    I had forked and customised the source to support this feature https://github.com/sincerekamal/node-soap

  • Ed Bishop
    Ed Bishop over 8 years
    Wouldn't this error if the wsdl was also behind authentication?
  • Admin
    Admin over 8 years
    From what I have experienced, no. Seems that authentication is done before getting the WSDL.
  • Ed Bishop
    Ed Bishop over 8 years
    No, the wsdl is used to build the client that is passed to callback where you are then setting the security on it. As far as I can tell this method is there for when the wsdl does not require auth, requires a different auth or is loaded from the file system. I think you need to pass the auth to the create client method: var auth = "Basic " + new Buffer('username' + ':' + 'password').toString("base64"); var client = Soap.createClient('wsdlUrl', { wsdl_headers: { Authorization: auth } }, (err, client) => { if (err) { throw err; } else { client.yourMethod(); } });
  • bgausden
    bgausden almost 6 years
    I found wsdl_options didn't work for me but, similar to yourself, addHttpHeader did the trick.
  • R.Anandan
    R.Anandan almost 5 years
    how to set securityMode None ?