I'm totally against modifying the source of any 3th party dependencies. It usually leads to future problems ( updates, compatibility, unsuspected bugs etc.)
Here is my attempt of stacking securities.
import { IHeaders, ISecurity } from "soap";
export class SecurityStack implements ISecurity {
private stack: ISecurity[];
public constructor(...security: ISecurity[]) {
this.stack = security;
const hasPostProcessMethod = this.stack.some(s => s["postProcess"]);
const hasToXmlMethod = this.stack.some(s => s["toXML"]);
if(hasPostProcessMethod && hasToXmlMethod)
throw new Error("Security with `postProcess` and those with `toXml` methods cannot be used together");
if(!hasPostProcessMethod)
this.postProcess = undefined;
}
public addOptions(options: any): void {
this.stack.forEach(security => {
if(security["addOptions"])
security.addOptions(options);
});
}
public toXML(): string {
let result = "";
this.stack.forEach(security => {
if(security["toXML"])
result += security.toXML();
});
return result;
}
public addHeaders(headers: IHeaders): void {
this.stack.forEach(security => {
if(security["addHeaders"])
security.addHeaders(headers);
});
}
public postProcess?(xml: any, envelopeKey: any): string {
let result = xml;
this.stack.forEach(security => {
if(security["postProcess"])
result = security.postProcess(xml, envelopeKey);
});
return result;
}
}
And then usage:
const client = await soap.createClientAsync(wsdl);
const sslSecurity = new soap.ClientSSLSecurity(xxx, xxx);
const wsSecurity = new soap.WSSecurity("xxx","xxx");
const securityStack = new SecurityStack(sslSecurity, wsSecurity);
client.setSecurity(securityStack);
Keep in mind that not every security methods can be combined.