node-soap - 如何为每个请求传递证书和基本授权标头?

时间:2017-07-07 07:32:04

标签: node.js ssl soap basic-authentication node-soap

我正在使用node-soap lib来获取SOAP服务并首次使用它。我要求我必须同时传递证书和基本授权标题。

我已经实现了以下代码:

var options = {
    wsdl_options: {
        key: fs.readFileSync(path.resolve("./xxx.key")),
        cert: fs.readFileSync(path.resolve("./xxx.crt")),
        ca: fs.readFileSync(path.resolve("./xxx.pem")),
    },
    wsdl_headers : {    
     Authorization : 'Basic ' + new Buffer(username +':'+ password ).toString('base64')
    },
    "overrideRootElement": {
        "namespace": "con",
    },
    envelopeKey : 'soapenv'
};



soap.createClient(url, options, function(err, client) {
    if(err){
        console.log("Error ::: >",err);
        res.json({message : err});
    }


    if(client){
        console.log(JSON.stringify(client.describe()));
        var data = actualRequestObject  

        client.setSecurity(new soap.ClientSSLSecurity(
            fs.readFileSync(path.resolve("./XXX.key")), 
            fs.readFileSync(path.resolve("./XXX.crt")),
            fs.readFileSync(path.resolve("./XXX.pem"))
        ));

        client.setSecurity(new soap.BasicAuthSecurity(username, password));
        client.IndicativeEnrichment(data, function(err, result){
            console.log("lastRequest :::: >>>>> ",client.lastRequest);
            if(err){
                console.log("ERROR Enrichment :::: >>> ", err);
            }

            if(result){
                console.log("RESULT ::: >>>>", result);
            }
        })
    }
});

当我尝试使用setSecurity()方法设置Basic auth和Certs时。它覆盖了我使用setSecurity()设置的第一件事。如果我没有通过其中任何一个,我会收到未经授权的错误。

请帮我提供解决方案的解决方案。

1 个答案:

答案 0 :(得分:0)

获得客户端证书和基本身份验证的好方法是implement your own node-soap security protocol。您可以从existing node-soap security protocols获得灵感并将它们组合起来,或者编写一个足够通用的链接两个(或更多)现有安全协议。使用解决方案创建拉取请求当然会更好,因此可以考虑将其直接包含在node-soap中。

个人最终将另外optionsconfigured https.Agent(...)一起传递给BasicAuthSecurity构造函数/ class

var https = require('https');

var options = {
  // https://nodejs.org/api/https.html#https_class_https_agent
  agent: new https.Agent({
    key: someKeyBuffer,
    cert: someCertBuffer,
    ca: [
      someCACertBuffer,
    ]
  });
}

client.setSecurity(new soap.BasicAuthSecurity('username', 'password', options));

这种方式还可用于将基本身份验证与.pfx or .p12中使用的ClientSSLSecurityPFX文件相结合