简体   繁体   English

AngularJS:通过Websocket发送身份验证头

[英]AngularJS: Sending Authentication Headers over Websocket

I am having difficulty authenticating requests to a WebSocket API. 我难以验证对WebSocket API的请求。

The Site I am working with (www.bitmex.com) provides a REST API and a WebSocket API. 我正在使用的网站(www.bitmex.com)提供了REST API和WebSocket API。

Both of their API's allow authentication with an API Key. 它们的两个API均允许使用API​​密钥进行身份验证。

Authentication Requirements 认证要求

The API provides the following documentation for authentication with API Keys: API提供了以下文档,用于使用API​​密钥进行身份验证:

    Authentication is done by sending the following HTTP headers:

        api-expires: a UNIX timestamp in the future (eg: 5 seconds).
        api-key: Your public API key. This the id param returned when you create an API Key via the API.
        api-signature: A signature of the request you are making. It is calculated as hex(HMAC_SHA256(verb + url + nonce + data)).

REST API REST API

I've created a NodeJS module for sending requests to the REST API, I've defined the following headers 我创建了一个NodeJS模块,用于向REST API发送请求,并定义了以下标头

    headers = {
        "User-Agent": "BitMEX NodeJS API Client",
        "api-expires": expires,
        "api-key": this.api_key,
        "api-signature": this.signMessage(verb, reqUrl, expires, params)
    };

    where the signMessage function looks like: 

    BitMEX.prototype.signMessage = function signMessage(verb, url, nonce, data) {
        if (!data || _.isEmpty(data)) data = '';
        else if(_.isObject(data)) data = formatParameters(data);

        return crypto.createHmac('sha256', this.secret).update(verb + url + nonce + data).digest('hex');
    };

This works great for the REST API and does everything I need it to in the backend of my application. 这对于REST API很有用,并且可以在我的应用程序后端完成我需要的一切。

WebSocket API WebSocket API

I am trying to use WebSocket get realtime data and display it in a browser based interface. 我正在尝试使用WebSocket获取实时数据并将其显示在基于浏览器的界面中。

The documentation on the site states: 该站点上的文档指出:

To use an API Key with websockets, you must sign the initial upgrade request in the same manner you would sign other REST calls. 

I've been implementing this in AngularJS using the ng-websocket module. 我一直在使用ng-websocket模块在AngularJS中实现这一点。

    exchange.dataStream = $websocket('wss://testnet.bitmex.com/realtime');

    exchange.dataStream.onMessage(function incoming (message) {
        console.log("BitMEX: WS MESSAGE RECEIVED: " + message.data);    

        // .. handle data here ... 

    });

    exchange.dataStream.send({"op":"getAccount"});

The problem that I've run into is I can't find anyway to send the headers using ng-websocket that are needed for authentication. 我遇到的问题是,无论如何我都找不到使用身份验证所需的ng-websocket发送标头。

If I am presently logged in to BitMEX from another tab in my browser, this will connect, get the data, and work as expected. 如果目前我是从浏览器的另一个选项卡登录到BitMEX,它将连接,获取数据并按预期工作。

However, if I am not currently logged in to the site, it will throw the following error: 但是,如果我当前尚未登录该站点,它将引发以下错误:

    BitMEX: WS MESSAGE RECEIVED: {"status":401,"error":"Not authenticated.","meta":{},"request":{"op":"getAccount"}}

There is a python example provided here: https://github.com/BitMEX/market-maker/blob/master/test/websocket-apikey-auth-test.py that goes through the Authentication process, but I haven't found a way to accomplish this in AngularJS. 这里提供了一个python示例: https : //github.com/BitMEX/market-maker/blob/master/test/websocket-apikey-auth-test.py经历了身份验证过程,但我没有找到一种在AngularJS中完成此操作的方法。

Summary 摘要

#1) When logged in to BitMEX, and the Websocket is working, is Chrome somehow using the website's cookies to authenticate the websocket requests? #1)登录BitMEX且Websocket正常工作后,Chrome是否以某种方式使用网站的Cookie来验证Websocket请求?

Looking at an overview of websockets here: http://enterprisewebbook.com/ch8_websockets.html The initial handshake upgrades the connection from "HTTP" to the WebSocket protocol, 在此处查看websocket的概述: http : //enterprisewebbook.com/ch8_websockets.html初始握手将连接从“ HTTP”升级为WebSocket协议,

#2) Because this initial connection is over HTTP, is there any way to attach the headers required to this initial HTTP request? #2)由于此初始连接是通过HTTP进行的,是否有任何方法可以将所需的标头附加到此初始HTTP请求?

If you read the Python example, the first thing it sends is {"op": "authKey", "args": [API_KEY, nonce, signature]} then it sends {"op": "getAccount"} 如果您阅读Python示例,则它发送的第一件事是{"op": "authKey", "args": [API_KEY, nonce, signature]}然后发送{"op": "getAccount"}

Python example line #44 and line #51 Python示例第44 第51行

How Chrome does it is another question. Chrome如何做到这是另一个问题。

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

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