@@ -179,13 +179,13 @@ class ImpCentralApiHelper {
179
179
login ( user , password ) {
180
180
const auth = this . _impCentralApi . auth ;
181
181
return this . _processImpCentralApiRequest (
182
- null , null , auth . login . bind ( auth ) , user , password ) ;
182
+ false , null , null , auth . login . bind ( auth ) , user , password ) ;
183
183
}
184
184
185
185
getAccessToken ( loginKey ) {
186
186
const auth = this . _impCentralApi . auth ;
187
187
return this . _processImpCentralApiRequest (
188
- Identifier . ENTITY_TYPE . TYPE_LOGIN_KEY , loginKey , auth . getAccessToken . bind ( auth ) , loginKey ) ;
188
+ false , Identifier . ENTITY_TYPE . TYPE_LOGIN_KEY , loginKey , auth . getAccessToken . bind ( auth ) , loginKey ) ;
189
189
}
190
190
191
191
// Checks and refreshes access token if needed
@@ -195,23 +195,7 @@ class ImpCentralApiHelper {
195
195
let expDate = new Date ( this . _authConfig . expiresAt ) ;
196
196
let currDate = new Date ( ) ;
197
197
if ( expDate - currDate < ACCESS_TOKEN_RENEW_BEFORE_EXPIRY_MS ) {
198
- if ( ! this . _authConfig . refreshToken && ! this . _authConfig . loginKey ) {
199
- return Promise . reject ( new Errors . AccessTokenExpiredError ( this . _authConfig ) ) ;
200
- }
201
- else {
202
- return this . _renewAccessToken ( )
203
- . then ( result => {
204
- this . _authConfig . setAccessToken ( result . access_token , result . expires_at ) ;
205
- this . _impCentralApi . auth . accessToken = this . _authConfig . accessToken ;
206
- return this . _authConfig . save ( ) ;
207
- } ) . catch ( error => {
208
- if ( error instanceof Errors . EntityNotFoundError ||
209
- this . _isEntityNotFoundError ( error ) ) {
210
- return Promise . reject ( new Errors . RefreshTokenError ( this . _authConfig ) ) ;
211
- }
212
- return Promise . reject ( error ) ;
213
- } ) ;
214
- }
198
+ return this . _refreshAccessToken ( ) ;
215
199
}
216
200
else {
217
201
this . _impCentralApi . auth . accessToken = this . _authConfig . accessToken ;
@@ -220,11 +204,33 @@ class ImpCentralApiHelper {
220
204
} ) ;
221
205
}
222
206
207
+ _refreshAccessToken ( ) {
208
+ if ( ! this . _authConfig . refreshToken && ! this . _authConfig . loginKey ) {
209
+ // --temp option was specified for impt auth login,
210
+ // there is no information required to refresh access token.
211
+ return Promise . reject ( new Errors . AccessTokenExpiredError ( this . _authConfig ) ) ;
212
+ }
213
+ else {
214
+ return this . _renewAccessToken ( )
215
+ . then ( result => {
216
+ this . _authConfig . setAccessToken ( result . access_token , result . expires_at ) ;
217
+ this . _impCentralApi . auth . accessToken = this . _authConfig . accessToken ;
218
+ return this . _authConfig . save ( ) ;
219
+ } ) . catch ( error => {
220
+ if ( error instanceof Errors . EntityNotFoundError ||
221
+ this . _isEntityNotFoundError ( error ) ) {
222
+ return Promise . reject ( new Errors . RefreshTokenError ( this . _authConfig ) ) ;
223
+ }
224
+ return Promise . reject ( error ) ;
225
+ } ) ;
226
+ }
227
+ }
228
+
223
229
_renewAccessToken ( ) {
224
230
const auth = this . _impCentralApi . auth ;
225
231
if ( this . _authConfig . refreshToken ) {
226
232
return this . _processImpCentralApiRequest (
227
- null , null , auth . refreshAccessToken . bind ( auth ) , this . _authConfig . refreshToken ) ;
233
+ false , null , null , auth . refreshAccessToken . bind ( auth ) , this . _authConfig . refreshToken ) ;
228
234
}
229
235
else if ( this . _authConfig . loginKey ) {
230
236
return this . getAccessToken ( this . _authConfig . loginKey ) ;
@@ -237,7 +243,7 @@ class ImpCentralApiHelper {
237
243
[ pageNumber , pageSize ] :
238
244
[ filters , pageNumber , pageSize ] ;
239
245
240
- return this . _processImpCentralApiRequest ( null , null , entityApi . list . bind ( entityApi ) , ...listArgs ) .
246
+ return this . _processImpCentralApiRequest ( true , null , null , entityApi . list . bind ( entityApi ) , ...listArgs ) .
241
247
then ( result => {
242
248
let data = result . data ;
243
249
if ( 'next' in result . links ) {
@@ -281,7 +287,7 @@ class ImpCentralApiHelper {
281
287
const entityApi = this . _getEntityApi ( entityType ) ;
282
288
return this . _checkAccessToken ( ) .
283
289
then ( ( ) => this . _entityStorage . clearList ( entityType ) ) .
284
- then ( ( ) => this . _processImpCentralApiRequest ( null , null , entityApi . create . bind ( entityApi ) , ...args ) ) .
290
+ then ( ( ) => this . _processImpCentralApiRequest ( true , null , null , entityApi . create . bind ( entityApi ) , ...args ) ) .
285
291
then ( result => result . data ) ;
286
292
}
287
293
@@ -292,7 +298,7 @@ class ImpCentralApiHelper {
292
298
this . _entityStorage . clearList ( entityType ) ;
293
299
this . _entityStorage . clearEntity ( id ) ;
294
300
} ) .
295
- then ( ( ) => this . _processImpCentralApiRequest ( entityType , id , entityApi . update . bind ( entityApi ) , id , ...args ) ) .
301
+ then ( ( ) => this . _processImpCentralApiRequest ( true , entityType , id , entityApi . update . bind ( entityApi ) , id , ...args ) ) .
296
302
then ( result => result . data ) ;
297
303
}
298
304
@@ -304,7 +310,7 @@ class ImpCentralApiHelper {
304
310
if ( entity ) {
305
311
return entity ;
306
312
}
307
- const entityRequest = this . _processImpCentralApiRequest ( entityType , id , entityApi . get . bind ( entityApi ) , id ) .
313
+ const entityRequest = this . _processImpCentralApiRequest ( true , entityType , id , entityApi . get . bind ( entityApi ) , id ) .
308
314
then ( result => result . data ) ;
309
315
this . _entityStorage . setEntity ( id , entityRequest ) ;
310
316
return entityRequest .
@@ -322,7 +328,7 @@ class ImpCentralApiHelper {
322
328
this . _entityStorage . clearList ( entityType ) ;
323
329
this . _entityStorage . clearEntity ( id ) ;
324
330
} ) .
325
- then ( ( ) => this . _processImpCentralApiRequest ( entityType , id , entityApi . delete . bind ( entityApi ) , id , ...args ) ) ;
331
+ then ( ( ) => this . _processImpCentralApiRequest ( true , entityType , id , entityApi . delete . bind ( entityApi ) , id , ...args ) ) ;
326
332
}
327
333
328
334
restartDevices ( devGroupId , conditional = false ) {
@@ -333,7 +339,7 @@ class ImpCentralApiHelper {
333
339
entityApi . conditionalRestartDevices :
334
340
entityApi . restartDevices ;
335
341
return this . _processImpCentralApiRequest (
336
- Identifier . ENTITY_TYPE . TYPE_DEVICE_GROUP , devGroupId , method . bind ( entityApi ) , devGroupId ) ;
342
+ true , Identifier . ENTITY_TYPE . TYPE_DEVICE_GROUP , devGroupId , method . bind ( entityApi ) , devGroupId ) ;
337
343
} ) ;
338
344
}
339
345
@@ -345,6 +351,7 @@ class ImpCentralApiHelper {
345
351
this . _entityStorage . clearEntity ( devGroupId ) ;
346
352
} ) .
347
353
then ( ( ) => this . _processImpCentralApiRequest (
354
+ true ,
348
355
Identifier . ENTITY_TYPE . TYPE_DEVICE_GROUP ,
349
356
devGroupId ,
350
357
entityApi . updateMinSupportedDeployment . bind ( entityApi ) ,
@@ -360,7 +367,7 @@ class ImpCentralApiHelper {
360
367
entityApi . conditionalRestart :
361
368
entityApi . restart ;
362
369
return this . _processImpCentralApiRequest (
363
- Identifier . ENTITY_TYPE . TYPE_DEVICE , deviceId , method . bind ( entityApi ) , deviceId ) ;
370
+ true , Identifier . ENTITY_TYPE . TYPE_DEVICE , deviceId , method . bind ( entityApi ) , deviceId ) ;
364
371
} ) ;
365
372
}
366
373
@@ -374,7 +381,12 @@ class ImpCentralApiHelper {
374
381
}
375
382
} ) .
376
383
then ( ( ) => this . _processImpCentralApiRequest (
377
- Identifier . ENTITY_TYPE . TYPE_DEVICE_GROUP , deviceGroupId , entityApi . addDevices . bind ( entityApi ) , deviceGroupId , ...deviceIds ) ) ;
384
+ true ,
385
+ Identifier . ENTITY_TYPE . TYPE_DEVICE_GROUP ,
386
+ deviceGroupId ,
387
+ entityApi . addDevices . bind ( entityApi ) ,
388
+ deviceGroupId ,
389
+ ...deviceIds ) ) ;
378
390
}
379
391
380
392
unassignDevices ( deviceGroupId , unbondKey , ...deviceIds ) {
@@ -387,6 +399,7 @@ class ImpCentralApiHelper {
387
399
}
388
400
} ) .
389
401
then ( ( ) => this . _processImpCentralApiRequest (
402
+ true ,
390
403
Identifier . ENTITY_TYPE . TYPE_DEVICE_GROUP ,
391
404
deviceGroupId ,
392
405
entityApi . removeDevices . bind ( entityApi ) ,
@@ -395,30 +408,43 @@ class ImpCentralApiHelper {
395
408
...deviceIds ) ) ;
396
409
}
397
410
398
- logStream ( deviceIds , messageHandler , stateChangeHandler = null ) {
411
+ logStream ( deviceIds , messageHandler , stateChangeHandler = null , errorHandler = null ) {
399
412
let logStreamId ;
400
413
const entityApi = this . _impCentralApi . logStreams ;
401
414
return this . _checkAccessToken ( ) .
402
415
then ( ( ) => this . _processImpCentralApiRequest (
403
- null , null , entityApi . create . bind ( entityApi ) , messageHandler , stateChangeHandler , null , LogStreams . FORMAT_JSON ) ) .
416
+ true ,
417
+ null ,
418
+ null ,
419
+ entityApi . create . bind ( entityApi ) ,
420
+ messageHandler ,
421
+ stateChangeHandler ,
422
+ errorHandler ,
423
+ LogStreams . FORMAT_JSON ) ) .
404
424
then ( ( logStream ) => {
405
425
logStreamId = logStream . data . id ;
406
426
return this . makeConcurrentOperations ( deviceIds , ( deviceId ) =>
407
- this . _processImpCentralApiRequest ( null , null , entityApi . addDevice . bind ( entityApi ) , logStreamId , deviceId ) ) ;
427
+ this . _processImpCentralApiRequest ( true , null , null , entityApi . addDevice . bind ( entityApi ) , logStreamId , deviceId ) ) ;
408
428
} ) .
409
429
then ( ( ) => logStreamId ) ;
410
430
}
411
431
412
432
closeLogStream ( logStreamId ) {
413
433
const entityApi = this . _impCentralApi . logStreams ;
414
- return this . _processImpCentralApiRequest ( null , null , entityApi . close . bind ( entityApi ) , logStreamId ) ;
434
+ return this . _processImpCentralApiRequest ( true , null , null , entityApi . close . bind ( entityApi ) , logStreamId ) ;
415
435
}
416
436
417
437
logs ( deviceId , pageNumber = null , pageSize = null ) {
418
438
const entityApi = this . _impCentralApi . devices ;
419
439
return this . _checkAccessToken ( ) .
420
440
then ( ( ) => this . _processImpCentralApiRequest (
421
- Identifier . ENTITY_TYPE . TYPE_DEVICE , deviceId , entityApi . getLogs . bind ( entityApi ) , deviceId , pageNumber , pageSize ) ) .
441
+ true ,
442
+ Identifier . ENTITY_TYPE . TYPE_DEVICE ,
443
+ deviceId ,
444
+ entityApi . getLogs . bind ( entityApi ) ,
445
+ deviceId ,
446
+ pageNumber ,
447
+ pageSize ) ) .
422
448
then ( result => result . data ) ;
423
449
}
424
450
@@ -428,15 +454,24 @@ class ImpCentralApiHelper {
428
454
return result ;
429
455
}
430
456
431
- _processImpCentralApiRequest ( entityType , id , impCentralApiMethod , ...args ) {
457
+ _processImpCentralApiRequest ( refreshAccessTokenOnAuthError , entityType , id , impCentralApiMethod , ...args ) {
432
458
UserInteractor . spinnerStart ( ) ;
433
459
return impCentralApiMethod ( ...args ) .
434
460
then ( result => this . _resolveImpCentralApiResponse ( result ) ) .
435
461
catch ( error => {
436
- if ( this . _isRateLimitError ( error ) ) {
462
+ if ( this . _isAuthenticationError ( error ) ) {
463
+ if ( refreshAccessTokenOnAuthError ) {
464
+ // try to refresh access token in case of authentication error and then restart the original request
465
+ return this . _refreshAccessToken ( ) .
466
+ then ( ( ) => this . _processImpCentralApiRequest (
467
+ refreshAccessTokenOnAuthError , entityType , id , impCentralApiMethod , ...args ) ) ;
468
+ }
469
+ }
470
+ else if ( this . _isRateLimitError ( error ) ) {
437
471
return new Promise ( ( resolve , reject ) => {
438
472
setTimeout ( ( ) => resolve ( ) , 1000 )
439
- } ) . then ( ( ) => this . _processImpCentralApiRequest ( entityType , id , impCentralApiMethod , ...args ) ) ;
473
+ } ) . then ( ( ) => this . _processImpCentralApiRequest (
474
+ refreshAccessTokenOnAuthError , entityType , id , impCentralApiMethod , ...args ) ) ;
440
475
}
441
476
else if ( this . _isEntityNotFoundError ( error ) && entityType && id ) {
442
477
throw new Errors . EntityNotFoundError ( entityType , id ) ;
@@ -454,6 +489,11 @@ class ImpCentralApiHelper {
454
489
return error instanceof ImpCentralApi . Errors . ImpCentralApiError &&
455
490
error . _statusCode === 429 ;
456
491
}
492
+
493
+ _isAuthenticationError ( error ) {
494
+ return error instanceof ImpCentralApi . Errors . ImpCentralApiError &&
495
+ error . _statusCode === 401 ;
496
+ }
457
497
}
458
498
459
499
module . exports = ImpCentralApiHelper ;
0 commit comments