Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Authorization in Node.js SOAP Client

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

like image 920
Kamalakannan J Avatar asked Nov 08 '14 11:11

Kamalakannan J


People also ask

Can I make a SOAP request in Node JS?

I bet I can do this easily in Node. There were several popular SOAP node.js modules to choose from to perform SOAP requests with such as: But I managed to find something wrong with all of them for my particular use case. Each needed either too much code or too much overhead.

How do I create a SOAP request with authorization?

Let us create a sample SOAP request with authorization. It should contain a simple username, a password, and the WSS-TimeToLive property. For this example, preemptive authentication must be enabled. After sending the request, take a look at the Raw request:

How to use node soap with endpoint?

Install the node soap package using npm. This will add the soap module in your package. Now you need the endPoint for the soap package. I am assuming that you would be posting some data using the soap service. You can make necessary changes and would be able to retrieve the data. Let us first check what services are present in the soap API.

How do I add an authorization to an XML request?

To manage authorizations in the request: Open the XML editor for the needed request. Open the Auth panel. In the Auth panel, you configure authentication parameters for your request. To add a new authorization: In the Authorization drop-down list, select Add New Authorization.


3 Answers

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);
});
like image 45
Finnur Eiríksson Avatar answered Sep 21 '22 11:09

Finnur Eiríksson


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.
});
like image 45
Rup Avatar answered Sep 21 '22 11:09

Rup


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)

like image 180
iloo Avatar answered Sep 20 '22 11:09

iloo