14
14
15
15
import { EventSourcePolyfill } from 'event-source-polyfill' ;
16
16
import * as Environments from './environments/environments.js' ;
17
+ import { AuthMethod } from './environments/environments.js' ;
17
18
import * as Utils from './utils.js' ;
19
+ import { showError } from './utils.js' ;
18
20
19
21
20
22
const config = {
@@ -278,31 +280,69 @@ let authHeaderValue;
278
280
* Activates authorization header for api calls
279
281
* @param {boolean } forDevOps if true, the credentials for the dev ops api will be used.
280
282
*/
281
- export function setAuthHeader ( forDevOps ) {
283
+ export function setAuthHeader ( forDevOps : boolean ) {
284
+ authHeaderValue = undefined ;
285
+ let environment = Environments . current ( ) ;
282
286
if ( forDevOps ) {
283
- if ( Environments . current ( ) . devopsAuth === 'basic' ) {
284
- authHeaderKey = 'Authorization' ;
285
- authHeaderValue = 'Basic ' + window . btoa ( Environments . current ( ) . usernamePasswordDevOps ) ;
286
- } else if ( Environments . current ( ) . devopsAuth === 'bearer' ) {
287
- authHeaderKey = 'Authorization' ;
288
- authHeaderValue = 'Bearer ' + Environments . current ( ) . bearerDevOps ;
287
+ let devopsAuthMethod = environment . authSettings ?. devops ?. method ;
288
+ if ( devopsAuthMethod === AuthMethod . basic ) {
289
+ if ( environment . authSettings . devops . basic . usernamePassword ) {
290
+ authHeaderKey = 'Authorization' ;
291
+ authHeaderValue = 'Basic ' + window . btoa ( environment . authSettings . devops . basic . usernamePassword ) ;
292
+ } else {
293
+ showError ( 'DevOps Username/password missing' )
294
+ }
295
+ } else if ( devopsAuthMethod === AuthMethod . bearer ) {
296
+ if ( environment . authSettings . devops . bearer . bearerToken ) {
297
+ authHeaderKey = 'Authorization' ;
298
+ authHeaderValue = 'Bearer ' + environment . authSettings . devops . bearer . bearerToken ;
299
+ } else {
300
+ showError ( 'DevOps Bearer token missing' )
301
+ }
302
+ } else if ( devopsAuthMethod === AuthMethod . oidc ) {
303
+ if ( environment . authSettings . devops . oidc . bearerToken ) {
304
+ authHeaderKey = 'Authorization' ;
305
+ authHeaderValue = 'Bearer ' + environment . authSettings . devops . oidc . bearerToken ;
306
+ } else {
307
+ showError ( 'DevOps SSO (Bearer) token missing' )
308
+ }
289
309
} else {
290
- authHeaderKey = 'Basic ' ;
291
- authHeaderValue = '' ;
310
+ authHeaderKey = 'Authorization ' ;
311
+ authHeaderValue = 'Basic ' ;
292
312
}
293
313
} else {
294
- if ( Environments . current ( ) . mainAuth === 'basic' ) {
295
- authHeaderKey = 'Authorization' ;
296
- authHeaderValue = 'Basic ' + window . btoa ( Environments . current ( ) . usernamePassword ) ;
297
- } else if ( Environments . current ( ) . mainAuth === 'pre' ) {
298
- authHeaderKey = 'x-ditto-pre-authenticated' ;
299
- authHeaderValue = Environments . current ( ) . dittoPreAuthenticatedUsername ;
300
- } else if ( Environments . current ( ) . mainAuth === 'bearer' ) {
301
- authHeaderKey = 'Authorization' ;
302
- authHeaderValue = 'Bearer ' + Environments . current ( ) . bearer ;
314
+ let mainAuthMethod = environment . authSettings ?. main ?. method ;
315
+ if ( mainAuthMethod === AuthMethod . basic ) {
316
+ if ( environment . authSettings . main . basic . usernamePassword ) {
317
+ authHeaderKey = 'Authorization' ;
318
+ authHeaderValue = 'Basic ' + window . btoa ( environment . authSettings . main . basic . usernamePassword ) ;
319
+ } else {
320
+ showError ( 'Username/password missing' )
321
+ }
322
+ } else if ( mainAuthMethod === AuthMethod . pre ) {
323
+ if ( environment . authSettings . main . pre . dittoPreAuthenticatedUsername ) {
324
+ authHeaderKey = 'x-ditto-pre-authenticated' ;
325
+ authHeaderValue = environment . authSettings . main . pre . dittoPreAuthenticatedUsername ;
326
+ } else {
327
+ showError ( 'Pre-Authenticated username missing' )
328
+ }
329
+ } else if ( mainAuthMethod === AuthMethod . bearer ) {
330
+ if ( environment . authSettings . main . bearer . bearerToken ) {
331
+ authHeaderKey = 'Authorization' ;
332
+ authHeaderValue = 'Bearer ' + environment . authSettings . main . bearer . bearerToken ;
333
+ } else {
334
+ showError ( 'Bearer token missing' )
335
+ }
336
+ } else if ( mainAuthMethod === AuthMethod . oidc ) {
337
+ if ( environment . authSettings . main . oidc . bearerToken ) {
338
+ authHeaderKey = 'Authorization' ;
339
+ authHeaderValue = 'Bearer ' + environment . authSettings . main . oidc . bearerToken ;
340
+ } else {
341
+ showError ( 'SSO (Bearer) token missing' )
342
+ }
303
343
} else {
304
- authHeaderKey = 'Basic ' ;
305
- authHeaderValue = '' ;
344
+ authHeaderKey = 'Authorization ' ;
345
+ authHeaderValue = 'Basic ' ;
306
346
}
307
347
}
308
348
}
@@ -325,78 +365,86 @@ function showDittoError(dittoErr, response) {
325
365
326
366
/**
327
367
* Calls the Ditto api
328
- * @param {String } method 'POST', 'GET', 'DELETE', etc.
329
- * @param {String } path of the Ditto call (e.g. '/things')
368
+ * @param {string } method 'POST', 'GET', 'DELETE', etc.
369
+ * @param {string } path of the Ditto call (e.g. '/things')
330
370
* @param {Object } body payload for the api call
331
371
* @param {Object } additionalHeaders object with additional header fields
332
372
* @param {boolean } returnHeaders request full response instead of json content
333
373
* @param {boolean } devOps default: false. Set true to avoid /api/2 path
334
374
* @param {boolean } returnErrorJson default: false. Set true to return the response of a failed HTTP call as JSON
335
375
* @return {Object } result as json object
336
376
*/
337
- export async function callDittoREST ( method ,
338
- path ,
377
+ export async function callDittoREST ( method : string ,
378
+ path : string ,
339
379
body = null ,
340
380
additionalHeaders = null ,
341
381
returnHeaders = false ,
342
382
devOps = false ,
343
383
returnErrorJson = false ) : Promise < any > {
344
- let response ;
345
- const contentType = method === 'PATCH' ? 'application/merge-patch+json' : 'application/json' ;
346
- try {
347
- response = await fetch ( Environments . current ( ) . api_uri + ( devOps ? '' : '/api/2' ) + path , {
348
- method : method ,
349
- headers : {
350
- 'Content-Type' : contentType ,
351
- [ authHeaderKey ] : authHeaderValue ,
352
- ...additionalHeaders ,
353
- } ,
354
- ...( method !== 'GET' && method !== 'DELETE' && body !== undefined ) && { body : JSON . stringify ( body ) } ,
355
- } ) ;
356
- } catch ( err ) {
357
- Utils . showError ( err ) ;
358
- throw err ;
359
- }
360
- if ( ! response . ok ) {
361
- if ( returnErrorJson ) {
384
+ if ( authHeaderValue ) {
385
+ let response ;
386
+ const contentType = method === 'PATCH' ? 'application/merge-patch+json' : 'application/json' ;
387
+ try {
388
+ response = await fetch ( Environments . current ( ) . api_uri + ( devOps ? '' : '/api/2' ) + path , {
389
+ method : method ,
390
+ headers : {
391
+ 'Content-Type' : contentType ,
392
+ [ authHeaderKey ] : authHeaderValue ,
393
+ ...additionalHeaders ,
394
+ } ,
395
+ ...( method !== 'GET' && method !== 'DELETE' && body !== undefined ) && { body : JSON . stringify ( body ) } ,
396
+ } ) ;
397
+ } catch ( err ) {
398
+ Utils . showError ( err ) ;
399
+ throw err ;
400
+ }
401
+ if ( ! response . ok ) {
402
+ if ( returnErrorJson ) {
403
+ if ( returnHeaders ) {
404
+ return response ;
405
+ } else {
406
+ return response . json ( ) . then ( ( dittoErr ) => {
407
+ showDittoError ( dittoErr , response ) ;
408
+ return dittoErr ;
409
+ } ) ;
410
+ }
411
+ } else {
412
+ response . json ( )
413
+ . then ( ( dittoErr ) => {
414
+ showDittoError ( dittoErr , response ) ;
415
+ } )
416
+ . catch ( ( err ) => {
417
+ Utils . showError ( 'No error details from Ditto' , response . statusText , response . status ) ;
418
+ } ) ;
419
+ throw new Error ( 'An error occurred: ' + response . status ) ;
420
+ }
421
+ }
422
+ if ( response . status !== 204 ) {
362
423
if ( returnHeaders ) {
363
424
return response ;
364
425
} else {
365
- return response . json ( ) . then ( ( dittoErr ) => {
366
- showDittoError ( dittoErr , response ) ;
367
- return dittoErr ;
368
- } ) ;
426
+ return response . json ( ) ;
369
427
}
370
428
} else {
371
- response . json ( )
372
- . then ( ( dittoErr ) => {
373
- showDittoError ( dittoErr , response ) ;
374
- } )
375
- . catch ( ( err ) => {
376
- Utils . showError ( 'No error details from Ditto' , response . statusText , response . status ) ;
377
- } ) ;
378
- throw new Error ( 'An error occurred: ' + response . status ) ;
379
- }
380
- }
381
- if ( response . status !== 204 ) {
382
- if ( returnHeaders ) {
383
- return response ;
384
- } else {
385
- return response . json ( ) ;
429
+ return null ;
386
430
}
387
431
} else {
388
- return null ;
432
+ throw new Error ( "Authentication missing" ) ;
389
433
}
390
434
}
391
435
392
436
export function getEventSource ( thingIds , urlParams ) {
393
- return new EventSourcePolyfill (
437
+ if ( authHeaderValue ) {
438
+ return new EventSourcePolyfill (
394
439
`${ Environments . current ( ) . api_uri } /api/2/things?ids=${ thingIds } ${ urlParams ? '&' + urlParams : '' } ` , {
395
440
headers : {
396
441
[ authHeaderKey ] : authHeaderValue ,
397
442
} ,
398
443
} ,
399
- ) ;
444
+ ) ;
445
+ } else {
446
+ throw new Error ( "Authentication missing" ) ;
447
+ }
400
448
}
401
449
402
450
/**
@@ -409,7 +457,6 @@ export function getEventSource(thingIds, urlParams) {
409
457
* @return {* } promise to the result
410
458
*/
411
459
export async function callConnectionsAPI ( operation , successCallback , connectionId = '' , connectionJson = null , command = null ) {
412
- Utils . assert ( ( env ( ) !== 'things' || Environments . current ( ) . solutionId ) , 'No solutionId configured in environment' ) ;
413
460
const params = config [ env ( ) ] [ operation ] ;
414
461
let response ;
415
462
let body ;
@@ -424,19 +471,23 @@ export async function callConnectionsAPI(operation, successCallback, connectionI
424
471
body = command ;
425
472
}
426
473
427
- try {
428
- response = await fetch ( Environments . current ( ) . api_uri + params . path . replace ( '{{solutionId}}' ,
429
- Environments . current ( ) . solutionId ) . replace ( '{{connectionId}}' , connectionId ) , {
430
- method : params . method ,
431
- headers : {
432
- 'Content-Type' : operation === 'connectionCommand' ? 'text/plain' : 'application/json' ,
433
- [ authHeaderKey ] : authHeaderValue ,
434
- } ,
435
- ...( body ) && { body : body } ,
436
- } ) ;
437
- } catch ( err ) {
438
- Utils . showError ( err ) ;
439
- throw err ;
474
+ if ( authHeaderValue ) {
475
+ try {
476
+ response = await fetch ( Environments . current ( ) . api_uri + params . path
477
+ . replace ( '{{connectionId}}' , connectionId ) , {
478
+ method : params . method ,
479
+ headers : {
480
+ 'Content-Type' : operation === 'connectionCommand' ? 'text/plain' : 'application/json' ,
481
+ [ authHeaderKey ] : authHeaderValue ,
482
+ } ,
483
+ ...( body ) && { body : body } ,
484
+ } ) ;
485
+ } catch ( err ) {
486
+ Utils . showError ( err ) ;
487
+ throw err ;
488
+ }
489
+ } else {
490
+ throw new Error ( "Authentication missing" ) ;
440
491
}
441
492
442
493
if ( ! response . ok ) {
@@ -479,9 +530,7 @@ export async function callConnectionsAPI(operation, successCallback, connectionI
479
530
}
480
531
481
532
export function env ( ) {
482
- if ( Environments . current ( ) . api_uri . startsWith ( 'https://things' ) ) {
483
- return 'things' ;
484
- } else if ( Environments . current ( ) . ditto_version === '2' ) {
533
+ if ( Environments . current ( ) . ditto_version === 2 ) {
485
534
return 'ditto_2' ;
486
535
} else {
487
536
return 'ditto_3' ;
0 commit comments