Browse Source

P4TB-93 try token reload after any token error response

1.1.101
francesco 3 years ago
parent
commit
2284aa7a5a
6 changed files with 193 additions and 46 deletions
  1. +12
    -12
      chrome/content/modules/pEpAdapter.js
  2. +32
    -17
      chrome/content/modules/pEpServer.js
  3. +1
    -1
      package.json
  4. +22
    -15
      tests/boilerplate.js
  5. +118
    -0
      tests/features/token_reload.js
  6. +8
    -1
      tests/mock.js

+ 12
- 12
chrome/content/modules/pEpAdapter.js View File

@ -23,7 +23,6 @@ const ENC_FORMAT_PGP_MIME = 3;
const ENC_FORMAT_PEP = 4;
const PEP_PREFIX = "pEp-";
const PEP_WRONG_SEC_TOKEN_CODE = -32600;
const JRPC_INTERNAL_ERROR = -32603;
const PEP_RESPONSE_OK = 0;
@ -59,6 +58,7 @@ class pEpAdapter {
return response.result.return;
}).catch((err) => {
this.log(err);
return err;
});
}
@ -73,6 +73,7 @@ class pEpAdapter {
return response.result.outParams[0].rating;
}).catch((err) => {
this.log(err);
return err;
});
}
@ -87,6 +88,7 @@ class pEpAdapter {
return response.result.outParams[0];
}).catch((err) => {
this.log(err);
return err;
});
}
@ -101,6 +103,7 @@ class pEpAdapter {
return response.result.outParams[0];
}).catch((err) => {
this.log(err);
return err;
});
}
async get_trustwords(identity1, identity2, language = "en", full = false) {
@ -116,6 +119,7 @@ class pEpAdapter {
return response.result.outParams[1];
}).catch((err) => {
this.log(err);
return err;
});
}
@ -130,6 +134,7 @@ class pEpAdapter {
return response.result.outParams;
}).catch((err) => {
this.log(err);
return err;
});
}
@ -145,6 +150,7 @@ class pEpAdapter {
return response.result.outParams;
}).catch((err) => {
this.log(err);
return err;
});
}
@ -159,6 +165,7 @@ class pEpAdapter {
return response.result.outParams;
}).catch((err) => {
this.log(err);
return err;
});
}
@ -173,6 +180,7 @@ class pEpAdapter {
return response.result.outParams[0];
}).catch((err) => {
this.log(err);
return err;
});
}
@ -188,6 +196,7 @@ class pEpAdapter {
return response.result.outParams[0];
}).catch((err) => {
this.log(err);
return err;
});
}
@ -210,13 +219,6 @@ class pEpAdapter {
if ((typeof (response) === "object") && response.hasOwnProperty("error")) {
switch (response.error.code) {
case PEP_WRONG_SEC_TOKEN_CODE:
this.server.reload();
this.log("Security Token reloaded, try again");
throw new Error({code: "PEP-ERROR", message: response.error.message, response});
break;
case -32603:
throw new Error({code: "PEP-ERROR", message: response.error.message, response});
break;
@ -290,9 +292,6 @@ class pEpAdapter {
if ((typeof (response) === "object") && response.hasOwnProperty("error")) {
switch (response.error.code) {
case PEP_WRONG_SEC_TOKEN_CODE:
throw new Error({code: "PEP-ERROR", message: "Invalid Security Token", response});
break;
case JRPC_INTERNAL_ERROR:
throw new Error({code: "PEP-ERROR", message: response.error.message, response});
break;
@ -386,6 +385,7 @@ class pEpAdapter {
return response.result.outParams[0].rating;
}).catch((err) => {
this.log(err);
return err;
});
}
@ -394,4 +394,4 @@ class pEpAdapter {
// export common.js module to allow one js file for browser and node.js
if (typeof module !== 'undefined' && module.exports) {
module.exports = pEpAdapter;
}
}

+ 32
- 17
chrome/content/modules/pEpServer.js View File

@ -204,23 +204,38 @@ class pEpServer {
*/
callPepAdapter(funcType, functionName, paramsArr) {
this.log("callPepAdapter(" + funcType + ", " + functionName + ")\n");
let that = this;
let securityToken = this.connectionInfo.security_token || "";
let connectionAddress = this.getConnectionAddress();
this.retryCount++;
let functionCallData = {
"security_token": securityToken,
"method": functionName,
"params": paramsArr,
"id": this.retryCount,
"jsonrpc": "2.0"
let server = this;
function getToken() {
return server.connectionInfo.security_token;
};
let url = connectionAddress + funcType;
let data = JSON.stringify(functionCallData);
return that.xhrQueue.executeXhr({url: url, data: data});
function callFactory () {
server.retryCount++;
let connectionAddress = server.getConnectionAddress();
return server.xhrQueue.executeXhr({
url: connectionAddress + funcType,
data: JSON.stringify({
"security_token": getToken() || "",
"method": functionName,
"params": paramsArr,
"id": server.retryCount,
"jsonrpc": "2.0"
})
});
}
return callFactory().then((response) => {
// if we have a wrong token
if(response.error && response.error.code === PEP_WRONG_SEC_TOKEN_CODE) {
// fetch fresh connection information
let token = getToken();
server.reload();
if (token != getToken()) {
// the token changed, try again
return callFactory();
}
}
// otherwise we just return the error
return response;
});
}
getAdapterVersion() {
@ -252,4 +267,4 @@ class pEpServer {
// export common.js module to allow one js file for browser and node.js
if (typeof module !== 'undefined' && module.exports) {
module.exports = pEpServer;
}
}

+ 1
- 1
package.json View File

@ -13,6 +13,6 @@
"xhr2": "^0.1.4"
},
"scripts": {
"test": "./node_modules/.bin/mocha tests/unit/controller.js tests/features/*.js"
"test": "./node_modules/.bin/mocha tests/unit/*.js tests/features/*.js"
}
}

+ 22
- 15
tests/boilerplate.js View File

@ -2,27 +2,34 @@
// because we could combine all these pEp objects into one, in that
// case the `before` part would probably be part of its constructor
// and this file could be deleted
var log = () => {}
if (false) {
log = console.log
}
function getControllerWithDetector(queue, detector) {
pEp = require('../chrome/content/modules/pEp');
let pEpAdapter = require('../chrome/content/modules/pEpAdapter');
let pEpServer = require('../chrome/content/modules/pEpServer');
let fs = require('fs');
let os = require('os');
server = new pEpServer(log, detector, queue);
let adapter = new pEpAdapter(log, server);
pEp.exists = false;
controller = new pEp([], log, adapter, fs, os);
controller.adapter = adapter;
controller.after = () => server.destroyInstance();
return controller;
}
module.exports = {
getController: function (queue) {
pEp = require('../chrome/content/modules/pEp');
let pEpAdapter = require('../chrome/content/modules/pEpAdapter');
let pEpServer = require('../chrome/content/modules/pEpServer');
let fs = require('fs');
let os = require('os');
let connectionInfoDetector = function () {
return getControllerWithDetector(queue, function () {
return {
address: "127.0.0.1",
port: 4223,
path: "/ja/0.1/",
security_token: "0847cqi9WqqE5ZcVtA8_mDIgEmYMv14xlNAvSZW"
};
};
server = new pEpServer(console.log, connectionInfoDetector, queue);
let adapter = new pEpAdapter(console.log, server);
pEp.exists = false;
controller = new pEp([], console.log, adapter, fs, os);
controller.adapter = adapter;
controller.after = () => server.destroyInstance();
return controller;
}
});
},
getControllerWithDetector: getControllerWithDetector
}

+ 118
- 0
tests/features/token_reload.js View File

@ -0,0 +1,118 @@
let {describe, it, before, beforeEach} = require('mocha');
let chai = require('chai').use(require('chai-as-promised'));
let getQueue = require('../mock').getQueue;
let getControllerWithDetector = require('../boilerplate').getControllerWithDetector;
let expect = require('chai').expect
let sinon = require('sinon');
describe('controller call', () => {
let spy, queue, server, controller, result;
let security_token;
let success = {"jsonrpc":"2.0","id":1,"result":{"outParams":[],"return":{"major":0,"minor":17,"patch":0,"api_version":"0.17.0","name":"(40) Eisenach-Ost","package_version":null,"engine_version":"1.1.1","pep_protocol_version":"2.1"},"errorstack":["init()"]}};
let wrongToken = {"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request: Wrong security token.","data":{"security_token":"j27yt2FsbQecAd8wgan_3hDEZzepUhz7iEH1GQd","method":"serverVersion","params":[],"id":13,"jsonrpc":"2.0"}},"id":-1};
function b1() {
security_token = "security_token_value_1";
spy = sinon.spy(function () {
return {
address: "127.0.0.1",
port: 4223,
path: "/ja/0.1/",
security_token: security_token
};
});
queue = getQueue();
controller = getControllerWithDetector(queue, spy);
}
function a1() {
queue.after();
controller.after();
}
describe('after a successful response', () => {
before(() => {
b1();
queue.respondWith(success);
result = controller.getVersion();
});
after(() => {
a1();
});
it('fetches connection data', () => {
expect(spy.calledOnce).to.be.true;
});
it('requests with the known token', () => {
queue.expectSent({"security_token":"security_token_value_1","method":"serverVersion","params":[],"id":1,"jsonrpc":"2.0"});
});
it('returns the result for a successful response', () => {
return expect(result).to.eventually.have
.property('pep_protocol_version', '2.1');
});
});
describe('after a failed response with an unchanged token', () => {
before(() => {
b1();
queue.respondWith(wrongToken);
result = controller.getVersion();
});
after(() => {
a1();
});
it('fetches fresh connection data', () => {
expect(spy.calledTwice).to.be.true;
});
it('gives up without retrying', () => {
return expect(result).to.eventually.be.an.instanceof(TypeError);
});
it('performed one call', () => {
expect(queue.countRequests()).to.equal(1);
});
});
describe('after a failed response with a changed token and a successful reply', () => {
before(() => {
b1();
security_token = "security_token_value_2";
queue.respondWith(wrongToken);
result = controller.getVersion();
queue.respondWith(success);
});
after(() => {
a1();
});
it('fetches fresh connection data', () => {
expect(spy.calledTwice).to.be.true;
});
it('requests with the updated token', () => {
queue.expectSent({"security_token":"security_token_value_2","method":"serverVersion","params":[],"id":2,"jsonrpc":"2.0"}, 1);
});
it('returns the result for a successful response', () => {
return expect(result).to.eventually.have
.property('pep_protocol_version', '2.1');
});
it('performed two calls', () => {
expect(queue.countRequests()).to.equal(2);
});
});
describe('after a failed response with a changed token and a failed reply', () => {
before(() => {
b1();
queue.respondWith(wrongToken);
security_token = "security_token_value_2";
result = controller.getVersion();
queue.respondWith(wrongToken);
});
after(() => {
a1();
});
it('fetches fresh connection data', () => {
expect(spy.calledTwice).to.be.true;
});
it('requests with the updated token', () => {
queue.expectSent({"security_token":"security_token_value_2","method":"serverVersion","params":[],"id":2,"jsonrpc":"2.0"}, 1);
});
it('gives up', () => {
return expect(result).to.eventually.be.an.instanceof(TypeError);
});
it('performed two calls', () => {
expect(queue.countRequests()).to.equal(2);
});
});
});

+ 8
- 1
tests/mock.js View File

@ -6,12 +6,16 @@ let expect = require('chai').expect
let sinon = require('sinon');
// unset to use the actual server
let mocked = true;
var log = () => {}
if (false) {
log = console.log
}
function getQueue () {
let XhrQueue = require('../chrome/content/modules/xhrQueue');
let s = sinon.fakeServer.create();
s.respondImmediately = true;
let queue = new XhrQueue(console.log);
let queue = new XhrQueue(log);
if (mocked) {
queue.XMLHttpRequest = s.xhr;
}
@ -30,6 +34,9 @@ function getQueue () {
expect(body).to.equal(JSON.stringify(expected));
}
};
queue.countRequests = () => {
return s.requests.length;
};
queue.after = () => {
s.restore();
};


Loading…
Cancel
Save