在节点中存储设备身份验证令牌的最佳做法?

时间:2017-12-05 17:01:58

标签: node.js authentication websocket

我有一个节点应用程序通过websocket连接到某些硬件(下面的代码)。目前,以下是连接和验证设备所采取的步骤:

node application使用一组预定的网络参数打开websocket连接。

然后,设备会将pin number发送回node application

pin number必须通过物理键入设备。

通过pin number对节点应用程序进行身份验证后,会向token发送身份验证node application,这将允许用户在重新连接时绕过引脚身份验证。

每个新的websocket会话都会发生token更改,因此需要在token中更新Node Application

这一步是我的问题所在。

我应该如何/在哪里存储每个新的身份验证令牌?

请注意代码的身份验证令牌部分位于

CloverDeviceConfiguration.prototype.onPairingSuccess = function(authToken) {  
    console.log("Pairing succeeded, authToken is " + authToken);
};

这是完整的代码

//Required libaries via npm
var clover = require('remote-pay-cloud');
var WebSocket = require('ws');
var XMLHttpRequest = require("xmlhttprequest-ssl").XMLHttpRequest;
var xhr = new XMLHttpRequest();
var log = clover.Logger.create();


var endpoint = "ws://192.168.1.65:5678/remote_pay"; // use the change endpoint function to change these paremeters as required. 
var isConnected = false;

/* Custom Functions Section
 *
 * Non predefined functions for communicating with the Clover device.
 *
 */

var changeEndpoint = function(socketType, ipaddress, port) {
    endpoint = socketType // ws or wss
        +
        "//" +
        ipaddress //ip address of the network pay display server (found on clover mini device)
        +
        ":" +
        port //port number of the network pay display (found on clover mini device)
        +
        "/remote_pay";
}

var paymentRequest = function(amount) {
    var saleRequest = new clover.remotepay.SaleRequest();
    saleRequest.setExternalId(clover.CloverID.getNewId());
    saleRequest.setAmount(amount);
    cloverConnector.sale(saleRequest);
}


var webSocketFactory = function() { // function required for connecting to clover device WITHOUT a web browser
    let webSocketOverrides = {
        createWebSocket: function() {
            // To support self-signed certificates you must pass rejectUnauthorized = false.
            // https://github.com/websockets/ws/blob/master/examples/ssl.js
            let sslOptions = {
                rejectUnauthorized: false
            };
            // Use the ws library by default.
            return new WebSocket(endpoint, sslOptions);
        }
    }
    return Object.assign(new clover.CloverWebSocketInterface(endpoint), webSocketOverrides);
};

var toggleConnection = function() {

    if (!isConnected) {
        cloverConnector.initializeConnection();
        var connectorListener = new CloverConnectorListener(cloverConnector);
        cloverConnector.addCloverConnectorListener(connectorListener);
        CloverConnectorListener.prototype = Object.create(clover.remotepay.ICloverConnectorListener.prototype);
        CloverConnectorListener.prototype.constructor = CloverConnectorListener;
    } else {
        cloverConnector.dispose();
        isConnected = false;
    }
}

// End Custom Functions


/* Clover Device Configuration/Connection Section
 *
 *
 */

var CloverDeviceConfiguration = function() {  
    clover.WebSocketPairedCloverDeviceConfiguration.call(this,       "ws://192.168.1.65:5678/remote_pay", // endpoint
              "com.cloverconnector.javascript.simple.sample:1.4", // Application Id
              "Javascript Simple Sample", // posName

              "Register_1", // serialNumber

              null, // authToken

              webSocketFactory, // webSocketFactory

              new clover.ImageUtil()); // Image Util
};

CloverDeviceConfiguration.prototype = Object.create(clover.WebSocketPairedCloverDeviceConfiguration.prototype);
CloverDeviceConfiguration.prototype.constructor = CloverDeviceConfiguration;

CloverDeviceConfiguration.prototype.onPairingCode = function(pairingCode) {  
    console.log("Pairing code is " + pairingCode);
};

CloverDeviceConfiguration.prototype.onPairingSuccess = function(authToken) {  
    console.log("Pairing succeeded, authToken is " + authToken);
};
var networkConfiguration = new CloverDeviceConfiguration();

var builderConfiguration = {};
builderConfiguration[clover.CloverConnectorFactoryBuilder.FACTORY_VERSION] = clover.CloverConnectorFactoryBuilder.VERSION_12;
var cloverConnectorFactory = clover.CloverConnectorFactoryBuilder.createICloverConnectorFactory(builderConfiguration);

var cloverConnector = cloverConnectorFactory.createICloverConnector(networkConfiguration);

var CloverConnectorListener = function(cloverConnector, progressinfoCallback) {
    clover.remotepay.ICloverConnectorListener.call(this);
    this.cloverConnector = cloverConnector;
    this.progressinfoCallback = progressinfoCallback;
    this.testStarted = false;
};
CloverConnectorListener.prototype = Object.create(clover.remotepay.ICloverConnectorListener.prototype);
CloverConnectorListener.prototype.constructor = CloverConnectorListener;

CloverConnectorListener.prototype.onDeviceReady = function(merchantInfo) {
    console.log("Clover device is ready, initiating sale request...");
    paymentRequest(1000); //Uncomment this code to issue a test payment request.
};

CloverConnectorListener.prototype.onDeviceDisconnected = function(request) {
    log.info(request);
};

CloverConnectorListener.prototype.onDeviceConnected = function(request) {
    log.info(request);
    isConnected = true;
};

//End Clover Device Configuration/Connection Section



/*
 *
 * Sale request section
 *
 */
CloverConnectorListener.prototype.onVerifySignatureRequest = function(request) {
    console.log("Requesting signatiure verification");
    log.info(request);
    this.cloverConnector.acceptSignature(request);
};

CloverConnectorListener.prototype.onConfirmPaymentRequest = function(request) {
    cloverConnector.acceptPayment(request.payment);
};

CloverConnectorListener.prototype.onSaleResponse = function(response) {
    log.info(response);
    cloverConnector.dispose();
    if (!response.getIsSale()) {
        console.error("Response is not a sale!");
        console.error(response);
    } else {
        console.log("Sale successful!");
    }
};

/*
 *
 * End sale request section
 *
 */

toggleConnection();

process.on('exit', function() {
    cloverConnector.dispose();
});

//TODO: Emitter function to prevent program from closing after a single transaction.

0 个答案:

没有答案