6

I'm trying to build a SOAP client with Node, I'm using "soap" package (https://www.npmjs.org/package/soap) trying to consume a user/password protected WSDL.

I can't find how to pass those credentials before creating the client by "soap.createClient", and of course, I can't retrieve the WSDL if I don't provide the right credentials.

I've tried doing:

soap.security.WSSecurity('user', 'pass');

and then calling "createClient" but to no avail.

Also, I've tried to do it with the node-soap-client, with this client I (apparently) can connect to the WSDL, but after that, I've no idea where to go (how to invoke methods).

What am I doing wrong?

Thanks for all your help!

Laerion
  • 805
  • 2
  • 13
  • 18

4 Answers4

5

Username and password credentials can be passed 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)

iloo
  • 926
  • 12
  • 26
1

If its password protected you also need to check the correct security mechanism. I spend a day trying to figure out that the service used NTLM security(it was a clients project and I only got username and password to access the wsdl). In that case, you would need to pass the correct wsdl_options object

var wsdl_options = {
  ntlm: true,
  username: "your username",
  password: "your password",
  domain: "domain",
  workstation: "workstation"
}

soap.createClient(data.credentials[data.type], {
    wsdl_options
  },
  function(err, client) {
    console.log(client.describe());
  });

Also, you would need to setSecurity on the client before using any service. the link to complete explanation: https://codecalls.com/2020/05/17/using-soap-with-node-js/

Nitin Khare
  • 147
  • 2
  • 10
0

when I added the auth to the headers I still had a problem. After reading the code and a number of articles I found this to work.

// or use local wsdl if security required
let url = 'http://service.asmx?wsdl' 
let wsdl = 'wsdl.wsdl';
let soap = require('soap');
let util = require('util')

soap.createClient(wsdl, function(err, client) {

    //don't forget to double slash the string or else the base64 will be incorrect
    client.setSecurity(new soap.BasicAuthSecurity('admin\\userName', 'password'));

    client.MethodFromWSDL(args, function (err, result) {
        console.log(util.inspect(result,{depth: null}))

    });
});
Ali
  • 99
  • 2
  • 12
  • You didn't need auth to get your WSDL, you needed auth to call the service then. The question and the answer by @iloo are about that. It's entirely possible that someone would need both though. – Dan Field Nov 30 '17 at 17:09
0

This worked for me, the API required the auth parameters as

<UserDetails xmlns="http://url/">';
  <userName>{$Username}</userName>
  <password>{$Password}</password>
  <program>{$program}</program>
</UserDetails>

After lots of trial and error - this ended working

const soapHeader = {
  UserDetails: {
   userName: process.env.userName,
   password: process.env.password,
   program: process.env.program
  }
}

...

soap.createClient(path, function (err, client) {
if (err) {
  console.log('Error creating SOAP client: ' + err);
}

client.addSoapHeader(soapHeader, "", "tns", process.env.URN);

client[process.env.FUNCTION](sargs, function (err, result, rawResponse, soapHeader, rawRequest) {
  if (err) {
    console.log('Error call SOAP function ' +  process.env.FUNCTION + ': ', err);
  }
  else {

    console.log(result);
  }

  ...
nanusdad
  • 446
  • 4
  • 5