From 46e4379202d53bfb35449d0eb3e8b285c468e058 Mon Sep 17 00:00:00 2001 From: Aschen Date: Mon, 27 Apr 2020 12:43:15 +0200 Subject: [PATCH] fix token expired event --- src/Kuzzle.js | 3 --- src/controllers/Auth.js | 10 ++++++-- src/controllers/Realtime.js | 2 ++ src/protocols/abstract/Base.js | 2 +- test/controllers/auth.test.js | 36 ++++++++++++++++++++--------- test/controllers/realtime.test.js | 38 ++++++++++++++++++++----------- test/protocol/Base.test.js | 2 +- 7 files changed, 62 insertions(+), 31 deletions(-) diff --git a/src/Kuzzle.js b/src/Kuzzle.js index b59124aa7..24e0b2b8b 100644 --- a/src/Kuzzle.js +++ b/src/Kuzzle.js @@ -501,9 +501,6 @@ Discarded request: ${JSON.stringify(request)}`)); this._lastTokenExpired = now; - this.auth.authenticationToken = null; - this.realtime.tokenExpired(); - this.emit('tokenExpired'); } diff --git a/src/controllers/Auth.js b/src/controllers/Auth.js index 49cdacbc7..0dcb9a0e5 100644 --- a/src/controllers/Auth.js +++ b/src/controllers/Auth.js @@ -18,6 +18,10 @@ class AuthController extends BaseController { super(kuzzle, 'auth'); this._authenticationToken = null; + + this.kuzzle.on('tokenExpired', () => { + this._authenticationToken = null; + }); } get authenticationToken () { @@ -27,9 +31,11 @@ class AuthController extends BaseController { set authenticationToken (encodedJwt) { if (encodedJwt === undefined || encodedJwt === null) { this._authenticationToken = null; - } else if (typeof encodedJwt === 'string') { + } + else if (typeof encodedJwt === 'string') { this._authenticationToken = new Jwt(encodedJwt); - } else { + } + else { throw new Error(`Invalid token argument: ${encodedJwt}`); } } diff --git a/src/controllers/Realtime.js b/src/controllers/Realtime.js index ad1959a90..aca778320 100644 --- a/src/controllers/Realtime.js +++ b/src/controllers/Realtime.js @@ -11,6 +11,8 @@ class RealTimeController extends BaseController { this.subscriptions = {}; this.subscriptionsOff = {}; + + this.kuzzle.on('tokenExpired', () => this.tokenExpired()); } count (roomId, options = {}) { diff --git a/src/protocols/abstract/Base.js b/src/protocols/abstract/Base.js index d8aa9319a..81d6a752d 100644 --- a/src/protocols/abstract/Base.js +++ b/src/protocols/abstract/Base.js @@ -98,7 +98,7 @@ Discarded request: ${JSON.stringify(request)}`)); this.emit('queryError', error, request); - if (request.action !== 'logout' && error.message === 'Token expired') { + if (request.action !== 'logout' && error.id === 'security.token.invalid') { this.emit('tokenExpired'); } diff --git a/test/controllers/auth.test.js b/test/controllers/auth.test.js index 93e2f3f2d..a3e774792 100644 --- a/test/controllers/auth.test.js +++ b/test/controllers/auth.test.js @@ -1,10 +1,11 @@ -const - Jwt = require('../../src/core/Jwt'), - AuthController = require('../../src/controllers/Auth'), - User = require('../../src/core/security/User'), - generateJwt = require('../mocks/generateJwt.mock'), - sinon = require('sinon'), - should = require('should'); +const sinon = require('sinon'); +const should = require('should'); + +const KuzzleEventEmitter = require('../../src/core/KuzzleEventEmitter'); +const Jwt = require('../../src/core/Jwt'); +const AuthController = require('../../src/controllers/Auth'); +const User = require('../../src/core/security/User'); +const generateJwt = require('../mocks/generateJwt.mock'); describe('Auth Controller', () => { const options = {opt: 'in'}; @@ -13,13 +14,24 @@ describe('Auth Controller', () => { kuzzle; beforeEach(() => { - kuzzle = { - emit: sinon.stub(), - query: sinon.stub() - }; + kuzzle = new KuzzleEventEmitter(); + kuzzle.query = sinon.stub(); + kuzzle.auth = new AuthController(kuzzle); }); + describe('on: tokenExpired', () => { + it('should set the authenticationToken to null', () => { + kuzzle.auth.authenticationToken = generateJwt(); + + kuzzle.emit('tokenExpired'); + + process.nextTick(() => { + should(kuzzle.auth.authenticationToken).be.null(); + }); + }); + }); + describe('createApiKey', () => { it('should send request to Kuzzle API', async () => { const apiResult = { @@ -309,6 +321,8 @@ describe('Auth Controller', () => { }); it('should trigger a "loginAttempt" event once the user is logged in', () => { + kuzzle.emit = sinon.stub(); + return kuzzle.auth.login('strategy', credentials, 'expiresIn') .then(() => { should(kuzzle.emit) diff --git a/test/controllers/realtime.test.js b/test/controllers/realtime.test.js index fd56f3bd9..5aa4ebcb8 100644 --- a/test/controllers/realtime.test.js +++ b/test/controllers/realtime.test.js @@ -1,22 +1,21 @@ -const - AuthController = require('../../src/controllers/Auth'), - RealtimeController = require('../../src/controllers/Realtime'), - generateJwt = require('../mocks/generateJwt.mock'), - mockrequire = require('mock-require'), - sinon = require('sinon'), - should = require('should'), - uuidv4 = require('../../src/utils/uuidv4'); +const mockrequire = require('mock-require'); +const sinon = require('sinon'); +const should = require('should'); + +const KuzzleEventEmitter = require('../../src/core/KuzzleEventEmitter'); +const AuthController = require('../../src/controllers/Auth'); +const RealtimeController = require('../../src/controllers/Realtime'); +const generateJwt = require('../mocks/generateJwt.mock'); +const uuidv4 = require('../../src/utils/uuidv4'); describe('Realtime Controller', () => { const options = {opt: 'in'}; let kuzzle; beforeEach(() => { - kuzzle = { - addListener: sinon.stub(), - query: sinon.stub().resolves(), - emit: sinon.stub() - }; + kuzzle = new KuzzleEventEmitter(); + kuzzle.query = sinon.stub(); + kuzzle.realtime = new RealtimeController(kuzzle); kuzzle.auth = new AuthController(kuzzle); kuzzle.auth.authenticationToken = generateJwt(); @@ -26,6 +25,19 @@ describe('Realtime Controller', () => { mockrequire.stopAll(); }); + describe('on: tokenExpired', () => { + it('should call tokenExpired() method', () => { + kuzzle.realtime.tokenExpired = sinon.stub(); + + kuzzle.emit('tokenExpired'); + + process.nextTick(() => { + should(kuzzle.realtime.tokenExpired).be.called(); + }); + }); + }); + + describe('#count', () => { it('should call realtime/count query with the roomId and return a Promise which resolves a number', () => { kuzzle.query.resolves({result: {roomId: 'roomId', count: 1234}}); diff --git a/test/protocol/Base.test.js b/test/protocol/Base.test.js index ca4314651..524aa786b 100644 --- a/test/protocol/Base.test.js +++ b/test/protocol/Base.test.js @@ -119,7 +119,7 @@ describe('Common Protocol', () => { eventStub = sinon.stub(), response = { error: { - message: 'Token expired' + id: 'security.token.invalid' } };