Skip to content

Commit 9547360

Browse files
author
Aschen
committed
Only re-auth if authenticator is set
1 parent ac15fc7 commit 9547360

File tree

5 files changed

+22
-30
lines changed

5 files changed

+22
-30
lines changed

doc/7/core-classes/kuzzle/authenticate/index.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ Authenticate the SDK by using the function set in the [authenticator](/sdk/js/7/
1515
authenticate();
1616
```
1717

18-
## Resolves
19-
20-
Resolves to the authentication token.
21-
2218
## Usage
2319

2420
<<< ./snippets/authenticate.js

doc/7/core-classes/kuzzle/authenticate/snippets/authenticate.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ kuzzle.authenticator = async () => {
33
};
44

55
try {
6-
const jwt = await kuzzle.authenticate();
7-
console.log(jwt);
8-
/*
9-
'eyJhbGciOiJIUzI1NiIsIkpXVCJ9.eyJfaWQiOiJmb28iLCJpYXQiOjE.wSPmb0z2tErRdYEg'
10-
*/
6+
await kuzzle.authenticate();
7+
118
console.log('Success');
129
} catch (error) {
1310
console.error(error.message);

doc/7/core-classes/kuzzle/properties/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ The `authenticator` property can be set to a function returning a promise.
4444

4545
This function will be called after a successful reconnection if the current authentication token is not valid anymore.
4646

47-
This function has to authenticate the SDK (by setting the `jwt` property). It can be a call to [auth.login](/sdk/js/7/controllers/auth/login) for example.
47+
This function has to authenticate the SDK. It can be a call to [auth.login](/sdk/js/7/controllers/auth/login) for example.
4848

4949
```js
5050
kuzzle.authenticator = async () => {
5151
await kuzzle.auth.login('local', { username: 'user', password: 'pass' });
5252
}
5353
```
5454

55-
If the `authenticator` function fail, then the `reconnected` event is never emitted and a `reconnectionError` event is emitted.
55+
If the `authenticator` function fail to authenticate the SDK, then the `reconnected` event is never emitted and a `reconnectionError` event is emitted.
5656

5757
### offlineQueueLoader
5858

src/Kuzzle.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,9 @@ export class Kuzzle extends KuzzleEventEmitter {
536536
this.stopQueuing();
537537
}
538538

539-
// If the SDK was authenticated, check if the token is still valid and try
539+
// If an authenticator was set, check if the token is still valid and try
540540
// to re-authenticate if needed. Otherwise the SDK is in disconnected state.
541-
if (this.jwt && ! await this.tryReAuthenticate()) {
541+
if (this.authenticator && ! await this.tryReAuthenticate()) {
542542
this.disconnect();
543543

544544
return;
@@ -592,18 +592,18 @@ export class Kuzzle extends KuzzleEventEmitter {
592592
*
593593
* @returns The authentication token
594594
*/
595-
async authenticate (): Promise<string> {
595+
async authenticate (): Promise<void> {
596596
if (typeof this.authenticator !== 'function') {
597597
throw new Error('The "authenticator" property must be a function.');
598598
}
599599

600600
await this.authenticator();
601601

602-
if (! this.jwt) {
603-
throw new Error('The "authenticator" function did not authenticate the SDK. ("jwt" is not set)');
604-
}
602+
const { valid } = await this.auth.checkToken();
605603

606-
return this.jwt;
604+
if (! valid) {
605+
throw new Error('The "authenticator" function failed to authenticate the SDK.');
606+
}
607607
}
608608

609609
/**

test/kuzzle/authenticator.test.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const { Kuzzle } = require('../../src/Kuzzle');
66
describe('Kuzzle authenticator function mecanisms', () => {
77
let kuzzle;
88
let protocol;
9-
let validJwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
109

1110
beforeEach(() => {
1211
protocol = new ProtocolMock('somewhere');
@@ -23,7 +22,7 @@ describe('Kuzzle authenticator function mecanisms', () => {
2322
let promise;
2423

2524
beforeEach(() => {
26-
kuzzle.jwt = validJwt;
25+
kuzzle.authenticator = async () => {};
2726

2827
sinon.stub(kuzzle, 'tryReAuthenticate').resolves(true);
2928
sinon.stub(kuzzle, 'disconnect');
@@ -32,7 +31,7 @@ describe('Kuzzle authenticator function mecanisms', () => {
3231
kuzzle.on('reconnected', reconnectedSpy);
3332
});
3433

35-
it('should try to re-authenticate when reconnecting if a JWT was set', async () => {
34+
it('should try to re-authenticate when reconnecting if an authenticator was set', async () => {
3635
promise = new Promise(_resolve => {
3736
resolve = _resolve;
3837
});
@@ -69,12 +68,12 @@ describe('Kuzzle authenticator function mecanisms', () => {
6968
return promise;
7069
});
7170

72-
it('should not try to authenticate if the SDK was not authenticated', async () => {
71+
it('should not try to authenticate if no authenticator was set', async () => {
72+
kuzzle.authenticator = null;
7373
promise = new Promise(_resolve => {
7474
resolve = _resolve;
7575
});
7676
await kuzzle.connect();
77-
kuzzle.jwt = null;
7877

7978
protocol.emit('reconnect');
8079

@@ -131,18 +130,18 @@ describe('Kuzzle authenticator function mecanisms', () => {
131130

132131
describe('#authenticate', () => {
133132
beforeEach(() => {
134-
kuzzle.authenticator = async () => {
135-
kuzzle.jwt = validJwt;
136-
};
133+
kuzzle.authenticator = async () => {};
134+
135+
sinon.stub(kuzzle.auth, 'checkToken').resolves({ valid: true });
137136

138137
sinon.spy(kuzzle, 'authenticator');
139138
});
140139

141140
it('should execute the "authenticator"', async () => {
142-
const token = await kuzzle.authenticate();
141+
await kuzzle.authenticate();
143142

144143
should(kuzzle.authenticator).be.calledOnce();
145-
should(token).be.eql(validJwt);
144+
should(kuzzle.auth.checkToken).be.calledOnce();
146145
});
147146

148147
it('should throw an error if the "authenticator" is not set', () => {
@@ -151,8 +150,8 @@ describe('Kuzzle authenticator function mecanisms', () => {
151150
should(kuzzle.authenticate()).be.rejected();
152151
});
153152

154-
it('should throw an error if the "authenticator" does not set the JWT', () => {
155-
kuzzle.authenticator = async () => {};
153+
it('should throw an error if the "authenticator" does not authenticate the SDK', () => {
154+
kuzzle.auth.checkToken.resolves({ valid: false });
156155

157156
should(kuzzle.authenticate()).be.rejected();
158157
});

0 commit comments

Comments
 (0)