How to sign a SOAP request multiple times using nodejs

73 Views Asked by At

I am new to SOAP and I need to use it here.

I am trying to send a SOAP request to an external endpoint.
For it to work I have to sign the SOAP request multiple times using certificates and encrypt it.
I tried the node-soap package using the WSSecurityCert function giving it the private key, public key and the password but this only lets me sign it once.
Using the function setSecurity twice didn't add two signatures.

How I understand it the soap package lets you configure everything but only creates the XML in the end. That means I can only view the created XML after I have sent it.

My questions are:

  • Is there a way to only sign the XML and get it back and then sign it again and encrypt everything in the end? Doing the process manually.
  • Are there any other active packages or ways to achive this using nodejs?
1

There are 1 best solutions below

1
X3R0 On

try doing this

const soap = require('soap');
const crypto = require('crypto');

// SOAP request payload
const soapRequest = {
  // Your SOAP request content
};

function signSoapEnvelope(soapEnvelope) {
  // Your signing logic here
  const timestamp = new Date().toISOString();
  soapEnvelope.Header = {
    Timestamp: timestamp,
    // Other headers...
  };

  // You may also want to sign the SOAP body or other parts as needed
  // ...

  return soapEnvelope;
}

soap.createClient(url, (err, client) => {
  if (err) {
    console.error(err);
    return;
  }
  let signedSoapRequest = signSoapEnvelope(soapRequest);
  signedSoapRequest = signSoapEnvelope(signedSoapRequest); // you could sign it here again
  client.MySoapOperation(signedSoapRequest, (err, result) => {
    if (err) {
      console.error(err);
      return;
    } 
    console.log(result);
  });
});