简体   繁体   English

如何:JavaScript中的wsse soap请求(节点)

[英]how to: wsse soap request in javascript (node)

I need to communicate with a soap:xml API from a node server on the Wix.com platform. 我需要从Wix.com平台上的节点服务器与soap:xml API通信。 The API requires Soap WSSE authentication. 该API需要Soap WSSE身份验证。

I can send an authenticated request to the endpoint in SoapUI, however haven't been able successfully do this on the Wix node platform. 我可以将经过身份验证的请求发送到SoapUI中的终结点,但是尚未在Wix节点平台上成功完成此操作。

Wix only have a subset of node packages available for install and XMLHttpRequest is not available in their environment. Wix只有一部分节点程序包可用于安装,而XMLHttpRequest在其环境中不可用。

I have tried node-soap but receive errors which indicate the package might be buggy on the Wix node platform. 我已经尝试过节点肥皂,但是收到错误消息,该错误指示该软件包在Wix节点平台上可能有错误。

I've found myself using the node "request" ( https://www.npmjs.com/package/request ) package and trying to roll my own solution to work around missing node packages and environment restrictions. 我发现自己使用了节点“ request”( https://www.npmjs.com/package/request )软件包,并尝试推出自己的解决方案来解决缺少的节点软件包和环境限制的问题。

Currently I can send a request to the end point however I receive the following response; 目前,我可以将请求发送到终点,但是会收到以下响应;

<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Client</faultcode><faultstring>Access denied</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>\n

This suggests to me i'm not authenticating correctly. 这向我表明我没有正确进行身份验证。

As I mentioned, I've been able to successfully send requests and receive expected responses via SoapUI. 如前所述,我已经能够通过SoapUI成功发送请求并接收预期的响应。 So the API is functioning, and I suspect it's my implementation that is at fault. 因此,API正在运行,我怀疑这是我的实现存在问题。 I'll be honest, I've worked with REST/JSON API's in the past, and it has been a long time since i've worked with a SOAP API, and I remember even back then having a whole lot of pain! 老实说,我过去使用过REST / JSON API,距离我使用SOAP API已有很长时间了,而且我还记得当时很痛苦!

my request code 我的要求代码

import request from 'request';
import {wsseHeaderAssoc} from 'backend/wsse';

export function getLocationID() {

    let apiUsername = "username";
    let apiPassword = "password";
    let apiURL = "https://api.serviceprovider.com/wsdl";

    // WSSE authentication header vars
    let wsse = wsseHeaderAssoc(apiUsername, apiPassword);
    let wsseUsername = wsse["Username"];
    let wssePasswordDigest = wsse["PasswordDigest"];
    let wsseCreated = wsse["Created"];
    let wsseNonce = wsse["Nonce"];

    let xml =
    `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:masked:api">`+
        `<soapenv:Header>`+
            `<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">`+
                `<wsse:UsernameToken wsu:Id="UsernameToken-19834957983507345987345987345">`+
                    `<wsse:Username>${wsseUsername}</wsse:Username>`+
                    `<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">${wssePasswordDigest}</wsse:Password>`+
                    `<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">${wsseNonce}</wsse:Nonce>`+
                    `<wsu:Created>${wsseCreated}</wsu:Created>`+
                `</wsse:UsernameToken>`+
            `</wsse:Security>`+
        `</soapenv:Header>`+
        `<soapenv:Body>`+
        ...
        `</soapenv:Body>`+
    `</soapenv:Envelope>`

    var options = {
    url: apiURL,
    method: 'POST',
    body: xml,
    headers: {
        'Content-Type':'text/xml;charset=utf-8',
        'Accept-Encoding': 'gzip,deflate',
        'Content-Length':xml.length,
        'SOAPAction':"https://api.serviceprovider.com/wsdl/service",
        'User-Agent':"Apache-HttpClient/4.1.1 (java 1.5)",
        'Connection':"Keep-Alive"
    }
    };

    let callback = (error, response, body) => {
    if (!error && response.statusCode == 200) {
        console.log('Raw result ', response);

        // If you ever get this working, do some mad magic here
    };
    console.log('Error ', response);  
    };

}

I'm using wsse-js ( https://github.com/vrruiz/wsse-js/blob/master/wsse.js ) to generate the PasswordDigest, Created datetime stamp and Nonce as the node wsse package ( https://www.npmjs.com/package/wsse ) isn't available on Wix. 我正在使用wsse-js( https://github.com/vrruiz/wsse-js/blob/master/wsse.js )生成PasswordDigest,Created datetime stamp和Nonce作为节点wsse包( https:// Wix上不提供www.npmjs.com/package/wsse )。 I've read over the code and based on what i've read elsewhere this looks like a good implementation. 我已经阅读了代码,并根据我在其他地方阅读的内容,这看起来是一个不错的实现。

I made one small addition to return the generated details in an assoc array; 我做了一些小的添加,以将生成的详细信息返回到一个assoc数组中;

export function wsseHeaderAssoc(Username, Password) {
    var w = wsse(Password);
    var wsseAssoc = [];
    wsseAssoc["Username"] = Username;
    wsseAssoc["PasswordDigest"] = w[2];
    wsseAssoc["Created"] = w[1];
    wsseAssoc["Nonce"] = w[0];
    return wsseAssoc;
}

As stated earlier i'm receiving a response of; 如前所述,我收到了的回复;

<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Client</faultcode><faultstring>Access denied</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>\n

And i'm expecting a valid SOAP XML response. 我期待一个有效的SOAP XML响应。

I've used the raw xml structure and headers from SoapUI to construct this, everything looks fine, i really have no idea where i'm going wrong. 我使用了SoapUI的原始xml结构和标头来构造它,一切看起来都很好,我真的不知道我要去哪里。

I would love any pointers anyone could throw my way - I've lost 2 days trying to brute force this, I need help. 我很乐意任何人都可以抛弃我的指针-我花了2天的时间试图强行破解,我需要帮助。

You can use the WSSecurity method from the soap package. 您可以使用soap包中的WSSecurity方法。 An example from their README: 他们的自述文件中的一个示例:

  var options = {
    hasNonce: true,
    actor: 'actor'
  };
  var wsSecurity = new soap.WSSecurity('username', 'password', options)
  client.setSecurity(wsSecurity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM