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
14 changes: 12 additions & 2 deletions src/Kuzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ export class Kuzzle extends KuzzleEventEmitter {

set jwt (encodedJwt) {
this.auth.authenticationToken = encodedJwt;

this._loggedIn = encodedJwt ? true : false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be a getter checking the existence of this.auth.authenticationToken instead of a static member synced by another setter?

Copy link
Contributor Author

@Shiranuit Shiranuit Aug 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't because in cookieAuth mode there is no JWT property set but that doesn't mean the SDK is not logged in, that's why I cannot always check if there is a JWT but I can set a property when people do want to put a JWT by hand.

}

get offlineQueue () {
Expand Down Expand Up @@ -599,7 +601,6 @@ export class Kuzzle extends KuzzleEventEmitter {
// If an authenticator was set, check if a user was logged in and if the token is still valid and try
// to re-authenticate if needed. Otherwise the SDK is in disconnected state.
if ( this._loggedIn
&& this.authenticator
&& ! await this.tryReAuthenticate()
) {
this._loggedIn = false;
Expand Down Expand Up @@ -634,6 +635,14 @@ export class Kuzzle extends KuzzleEventEmitter {
return true;
}

/**
* Check if there is an authenticator after verifying if the token is still valid,
* like so API Keys can be used even if there is no authenticator since they will be still valid.
*/
if (! this.authenticator) {
return false;
}

await this.authenticate();

return true;
Expand Down Expand Up @@ -839,7 +848,7 @@ Discarded request: ${JSON.stringify(request)}`));
return;
}

if (this._loggedIn && this.authenticator && await this.tryReAuthenticate()) {
if (this._loggedIn && await this.tryReAuthenticate()) {
this.emit('reAuthenticated');

return;
Expand All @@ -854,6 +863,7 @@ Discarded request: ${JSON.stringify(request)}`));

this._lastTokenExpired = now;

this.jwt = null;
this.emit('tokenExpired');
}

Expand Down
26 changes: 26 additions & 0 deletions test/kuzzle/authenticator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const should = require('should');
const sinon = require('sinon');
const ProtocolMock = require('../mocks/protocol.mock');
const { Kuzzle } = require('../../src/Kuzzle');
const generateJwt = require('../mocks/generateJwt.mock');

describe('Kuzzle authenticator function mecanisms', () => {
let kuzzle;
Expand All @@ -16,6 +17,20 @@ describe('Kuzzle authenticator function mecanisms', () => {
sinon.restore();
});

describe('jwt property', () => {
it('should set the SDK property _loggedIn when setting the JWT property', () => {
kuzzle.jwt = generateJwt();

should(kuzzle._loggedIn).be.true();
});

it('should set the SDK property _loggedIn when setting the JWT property to null or undefined', () => {
kuzzle.jwt = null;

should(kuzzle._loggedIn).be.false();
});
});

describe('connected listener', () => {
let resolve;
let promise;
Expand Down Expand Up @@ -230,6 +245,17 @@ describe('Kuzzle authenticator function mecanisms', () => {
should(reconnectionErrorSpy).not.be.called();
});

it('should returns false if the token is not valid and there is no authenticator', async () => {
kuzzle.auth.checkToken.resolves({ valid: false });
kuzzle.authenticator = null;

const ret = await kuzzle.tryReAuthenticate();

should(ret).be.false();
should(kuzzle.authenticate).not.be.called();
should(reconnectionErrorSpy).not.be.called();
});

it('should call "authenticate" if the token is not valid', async () => {
const ret = await kuzzle.tryReAuthenticate();

Expand Down
10 changes: 9 additions & 1 deletion test/kuzzle/protocol.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,19 @@ describe('Kuzzle protocol methods', () => {

it('should empty the jwt when a "tokenExpired" events is triggered', () => {
kuzzle.jwt = generateJwt();

should(kuzzle._loggedIn).be.true();

kuzzle.connect();
kuzzle.tryReAuthenticate = sinon.stub().resolves(false);

kuzzle.protocol.emit('tokenExpired');

should(kuzzle.jwt).be.null();
setTimeout(() => {
should(kuzzle.tryReAuthenticate).be.calledOnce();
should(kuzzle._loggedIn).be.false();
should(kuzzle.jwt).be.null();
}, 1);
});
});
});