Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/Kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,6 @@ Discarded request: ${JSON.stringify(request)}`));

this._lastTokenExpired = now;

this.auth.authenticationToken = null;
this.realtime.tokenExpired();

this.emit('tokenExpired');
}

Expand Down
10 changes: 8 additions & 2 deletions src/controllers/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class AuthController extends BaseController {
super(kuzzle, 'auth');

this._authenticationToken = null;

this.kuzzle.on('tokenExpired', () => {
this._authenticationToken = null;
});
}

get authenticationToken () {
Expand All @@ -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}`);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/Realtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class RealTimeController extends BaseController {

this.subscriptions = {};
this.subscriptionsOff = {};

this.kuzzle.on('tokenExpired', () => this.tokenExpired());
}

count (roomId, options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/protocols/abstract/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
36 changes: 25 additions & 11 deletions test/controllers/auth.test.js
Original file line number Diff line number Diff line change
@@ -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'};
Expand All @@ -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 = {
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 25 additions & 13 deletions test/controllers/realtime.test.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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}});
Expand Down
2 changes: 1 addition & 1 deletion test/protocol/Base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('Common Protocol', () => {
eventStub = sinon.stub(),
response = {
error: {
message: 'Token expired'
id: 'security.token.invalid'
}
};

Expand Down