Skip to content

Commit 65a7d42

Browse files
author
Shiranuit
authored
verify that the token informations retrieve from checkToken doesnt belong to the anonymous user (#683)
This PR adds a verification when using checkToken to verify that the informations about the token validity are not from the anonymous to
1 parent 5ccdc84 commit 65a7d42

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/Kuzzle.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ export class Kuzzle extends KuzzleEventEmitter {
353353
* In case of login failure we need to be sure that the stored token is still valid
354354
*/
355355
try {
356-
const response = await this.auth.checkToken();
357-
this._loggedIn = response.valid;
356+
this._loggedIn = await this.isAuthenticated();
358357
} catch {
359358
this._loggedIn = false;
360359
}
@@ -374,8 +373,7 @@ export class Kuzzle extends KuzzleEventEmitter {
374373
*/
375374
this.on('connected', async () => {
376375
try {
377-
const { valid } = await this.auth.checkToken();
378-
this._loggedIn = valid;
376+
this._loggedIn = await this.isAuthenticated();
379377
} catch {
380378
this._loggedIn = false;
381379
}
@@ -654,7 +652,7 @@ export class Kuzzle extends KuzzleEventEmitter {
654652
private async tryReAuthenticate (): Promise<boolean> {
655653
this._reconnectInProgress = true;
656654
try {
657-
const { valid } = await this.auth.checkToken();
655+
const valid = await this.isAuthenticated();
658656

659657
if (valid) {
660658
return true;
@@ -698,7 +696,7 @@ export class Kuzzle extends KuzzleEventEmitter {
698696

699697
await this.authenticator();
700698

701-
const { valid } = await this.auth.checkToken();
699+
const valid = await this.isAuthenticated();
702700

703701
this._loggedIn = valid;
704702

@@ -707,6 +705,17 @@ export class Kuzzle extends KuzzleEventEmitter {
707705
}
708706
}
709707

708+
/**
709+
* Check wether the user is authenticated or not
710+
* by verifiying if a token is present and still valid
711+
* and if the token doesn't belong to the anonymous user.
712+
*/
713+
async isAuthenticated() {
714+
const { valid, kuid } = await this.auth.checkToken();
715+
716+
return valid && kuid !== '-1';
717+
}
718+
710719
/**
711720
* Adds a listener to a Kuzzle global event. When an event is fired, listeners are called in the order of their
712721
* insertion.

src/controllers/Auth.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ export class AuthController extends BaseController {
206206
/**
207207
* Token expiration timestamp
208208
*/
209-
expiresAt: number
209+
expiresAt: number,
210+
/**
211+
* KUID of the user that the token belongs to
212+
*/
213+
kuid: string
210214
}> {
211215
let cookieAuth = false;
212216
if (token === undefined) {

0 commit comments

Comments
 (0)