|  | 
| 10 | 10 |   SecurityController = require('./controllers/security'), | 
| 11 | 11 |   MemoryStorageController = require('./controllers/memoryStorage'), | 
| 12 | 12 |   BaseController = require('./controllers/base'), | 
| 13 |  | -  uuidv4 = require('./uuidv4'); | 
|  | 13 | +  uuidv4 = require('./uuidv4'), | 
|  | 14 | +  proxify = require('./proxify'); | 
| 14 | 15 | 
 | 
| 15 | 16 | const | 
| 16 | 17 |   events = [ | 
| @@ -87,6 +88,14 @@ class Kuzzle extends KuzzleEventEmitter { | 
| 87 | 88 |     } | 
| 88 | 89 |     this.queuing = false; | 
| 89 | 90 |     this.replayInterval = 10; | 
|  | 91 | + | 
|  | 92 | +    this._jwt = undefined; | 
|  | 93 | + | 
|  | 94 | +    return proxify(this, { | 
|  | 95 | +      seal: true, | 
|  | 96 | +      name: 'kuzzle', | 
|  | 97 | +      exposeApi: true | 
|  | 98 | +    }); | 
| 90 | 99 |   } | 
| 91 | 100 | 
 | 
| 92 | 101 |   get autoQueue () { | 
| @@ -116,28 +125,6 @@ class Kuzzle extends KuzzleEventEmitter { | 
| 116 | 125 |     this._autoReplay = value; | 
| 117 | 126 |   } | 
| 118 | 127 | 
 | 
| 119 |  | -  get jwt () { | 
| 120 |  | -    return this._jwt; | 
| 121 |  | -  } | 
| 122 |  | - | 
| 123 |  | -  set jwt (token) { | 
| 124 |  | -    if (token === undefined || token === null) { | 
| 125 |  | -      this._jwt = undefined; | 
| 126 |  | -    } | 
| 127 |  | -    else if (typeof token === 'string') { | 
| 128 |  | -      this._jwt = token; | 
| 129 |  | -    } | 
| 130 |  | -    else if (typeof token === 'object' | 
| 131 |  | -      && token.result | 
| 132 |  | -      && token.result.jwt | 
| 133 |  | -      && typeof token.result.jwt === 'string' | 
| 134 |  | -    ) { | 
| 135 |  | -      this._jwt = token.result.jwt; | 
| 136 |  | -    } else { | 
| 137 |  | -      throw new Error(`Invalid token argument: ${token}`); | 
| 138 |  | -    } | 
| 139 |  | -  } | 
| 140 |  | - | 
| 141 | 128 |   get host () { | 
| 142 | 129 |     return this.protocol.host; | 
| 143 | 130 |   } | 
| @@ -203,6 +190,26 @@ class Kuzzle extends KuzzleEventEmitter { | 
| 203 | 190 |     return this.protocol.sslConnection; | 
| 204 | 191 |   } | 
| 205 | 192 | 
 | 
|  | 193 | +  get authenticated () { | 
|  | 194 | +    return this.auth.authenticationToken && !this.auth.authenticationToken.expired; | 
|  | 195 | +  } | 
|  | 196 | + | 
|  | 197 | +  get jwt () { | 
|  | 198 | +    if (!this.auth.authenticationToken) { | 
|  | 199 | +      return null; | 
|  | 200 | +    } | 
|  | 201 | + | 
|  | 202 | +    return this.auth.authenticationToken.encodedJwt; | 
|  | 203 | +  } | 
|  | 204 | + | 
|  | 205 | +  set jwt (encodedJwt) { | 
|  | 206 | +    this.auth.authenticationToken = encodedJwt; | 
|  | 207 | +  } | 
|  | 208 | +   | 
|  | 209 | +  get connected () { | 
|  | 210 | +    return this.protocol.connected; | 
|  | 211 | +  } | 
|  | 212 | + | 
| 206 | 213 |   /** | 
| 207 | 214 |   * Emit an event to all registered listeners | 
| 208 | 215 |   * An event cannot be emitted multiple times before a timeout has been reached. | 
| @@ -238,7 +245,7 @@ class Kuzzle extends KuzzleEventEmitter { | 
| 238 | 245 |     this.protocol.addListener('queryError', (err, query) => this.emit('queryError', err, query)); | 
| 239 | 246 | 
 | 
| 240 | 247 |     this.protocol.addListener('tokenExpired', () => { | 
| 241 |  | -      this.jwt = undefined; | 
|  | 248 | +      this.auth.authenticationToken = null; | 
| 242 | 249 |       this.emit('tokenExpired'); | 
| 243 | 250 |     }); | 
| 244 | 251 | 
 | 
| @@ -274,16 +281,17 @@ class Kuzzle extends KuzzleEventEmitter { | 
| 274 | 281 |         this.playQueue(); | 
| 275 | 282 |       } | 
| 276 | 283 | 
 | 
| 277 |  | -      if (this.jwt) { | 
| 278 |  | -        return this.auth.checkToken(this.jwt) | 
|  | 284 | +      if (this.auth.authenticationToken) { | 
|  | 285 | +        return this.auth.checkToken() | 
| 279 | 286 |           .then(res => { | 
|  | 287 | + | 
| 280 | 288 |             // shouldn't obtain an error but let's invalidate the token anyway | 
| 281 | 289 |             if (!res.valid) { | 
| 282 |  | -              this.jwt = undefined; | 
|  | 290 | +              this.auth.authenticationToken = null; | 
| 283 | 291 |             } | 
| 284 | 292 |           }) | 
| 285 | 293 |           .catch(() => { | 
| 286 |  | -            this.jwt = undefined; | 
|  | 294 | +            this.auth.authenticationToken = null; | 
| 287 | 295 |           }) | 
| 288 | 296 |           .then(() => this.emit('reconnected')); | 
| 289 | 297 |       } | 
| @@ -371,16 +379,7 @@ class Kuzzle extends KuzzleEventEmitter { | 
| 371 | 379 |     request.volatile.sdkInstanceId = this.protocol.id; | 
| 372 | 380 |     request.volatile.sdkVersion = this.sdkVersion; | 
| 373 | 381 | 
 | 
| 374 |  | -    /* | 
| 375 |  | -     * Do not add the token for the checkToken route, to avoid getting a token error when | 
| 376 |  | -     * a developer simply wish to verify his token | 
| 377 |  | -     */ | 
| 378 |  | -    if (this.jwt !== undefined | 
| 379 |  | -      && !(request.controller === 'auth' | 
| 380 |  | -      && (request.action === 'checkToken' || request.action === 'login')) | 
| 381 |  | -    ) { | 
| 382 |  | -      request.jwt = this.jwt; | 
| 383 |  | -    } | 
|  | 382 | +    this.auth.authenticateRequest(request); | 
| 384 | 383 | 
 | 
| 385 | 384 |     let queuable = true; | 
| 386 | 385 |     if (options && options.queuable === false) { | 
| @@ -456,20 +455,23 @@ Discarded request: ${JSON.stringify(request)}`)); | 
| 456 | 455 |       throw new Error('You must provide a valid accessor.'); | 
| 457 | 456 |     } | 
| 458 | 457 | 
 | 
| 459 |  | -    if (this[accessor]) { | 
|  | 458 | +    if (this.__proxy__ ? this.__proxy__.hasProp(accessor) : this[accessor]) { | 
| 460 | 459 |       throw new Error(`There is already a controller with the accessor '${accessor}'. Please use another one.`); | 
| 461 | 460 |     } | 
| 462 | 461 | 
 | 
| 463 | 462 |     const controller = new ControllerClass(this); | 
| 464 |  | - | 
|  | 463 | +     | 
| 465 | 464 |     if (!(controller.name && controller.name.length > 0)) { | 
| 466 | 465 |       throw new Error('Controllers must have a name.'); | 
| 467 | 466 |     } | 
| 468 |  | - | 
|  | 467 | +     | 
| 469 | 468 |     if (controller.kuzzle !== this) { | 
| 470 | 469 |       throw new Error('You must pass the Kuzzle SDK instance to the parent constructor.'); | 
| 471 | 470 |     } | 
| 472 |  | - | 
|  | 471 | +     | 
|  | 472 | +    if (this.__proxy__) { | 
|  | 473 | +      this.__proxy__.registerProp(accessor); | 
|  | 474 | +    } | 
| 473 | 475 |     this[accessor] = controller; | 
| 474 | 476 | 
 | 
| 475 | 477 |     return this; | 
|  | 
0 commit comments