@vcd/sdk 15.0.8 → 15.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,8 @@ import { HttpResponse, HttpClient, HttpHeaders, HttpClientModule } from '@angula
3
3
  import * as i0 from '@angular/core';
4
4
  import { Injectable, InjectionToken, Optional, Inject, NgModule } from '@angular/core';
5
5
  import { CommonModule } from '@angular/common';
6
- import { tap, finalize, map, catchError, switchMap, retry, flatMap, skipWhile, share, concatMap, filter, withLatestFrom } from 'rxjs/operators';
7
- import { Observable, throwError, BehaviorSubject, of, ReplaySubject, merge } from 'rxjs';
6
+ import { tap, finalize, map, switchMap, filter, first, catchError, retry, flatMap, skipWhile, share, concatMap, withLatestFrom } from 'rxjs/operators';
7
+ import { of, Observable, throwError, BehaviorSubject, ReplaySubject, merge } from 'rxjs';
8
8
  import { TaskType } from '@vcd/bindings/vcloud/api/rest/schema_v1_5';
9
9
 
10
10
  // tslint:disable:variable-name
@@ -478,13 +478,45 @@ const HTTP_HEADERS = Object.freeze({
478
478
  x_vcloud_authorization: 'x-vcloud-authorization'
479
479
  });
480
480
 
481
+ /*
482
+ * Copyright (c) 2025-2025 Broadcom. All Rights Reserved. Broadcom Confidential. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
483
+ */
484
+ class DecodedJwt {
485
+ constructor(rawToken) {
486
+ this.rawToken = rawToken;
487
+ const parts = rawToken.split(".");
488
+ if (parts.length !== 3) {
489
+ throw new Error(`Invalid JWT format: '${rawToken}'`);
490
+ }
491
+ this.header = this.decodeBase64Url(parts[0]);
492
+ this.payload = this.decodeBase64Url(parts[1]);
493
+ this.signature = parts[2];
494
+ }
495
+ decodeBase64Url(base64Url) {
496
+ const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
497
+ const json = decodeURIComponent(Array.prototype.map.call(atob(base64), escapeMultibyteCharacter).join(""));
498
+ return JSON.parse(json);
499
+ }
500
+ isExpired() {
501
+ const OFFSET_SECONDS = 60;
502
+ const exp = this.payload["exp"] || 0;
503
+ const current = Math.floor(Date.now() / 1000) + OFFSET_SECONDS;
504
+ return current >= exp;
505
+ }
506
+ toJSON() {
507
+ return {
508
+ header: this.header,
509
+ payload: this.payload,
510
+ signature: this.signature,
511
+ };
512
+ }
513
+ }
514
+ function escapeMultibyteCharacter(c) {
515
+ return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
516
+ }
517
+
481
518
  // tslint:disable:variable-name
482
519
  class RequestHeadersInterceptor {
483
- constructor() {
484
- this._enabled = true;
485
- this._version = '';
486
- this._authenticationHeader = HTTP_HEADERS.Authorization;
487
- }
488
520
  set enabled(_enabled) {
489
521
  this._enabled = _enabled;
490
522
  }
@@ -505,6 +537,12 @@ class RequestHeadersInterceptor {
505
537
  this._authenticationHeader = (this._authentication && this._authentication.length > 32) ?
506
538
  HTTP_HEADERS.Authorization : HTTP_HEADERS.x_vcloud_authorization;
507
539
  }
540
+ constructor(authTokenHolderService) {
541
+ this.authTokenHolderService = authTokenHolderService;
542
+ this._enabled = true;
543
+ this._version = '';
544
+ this._authenticationHeader = HTTP_HEADERS.Authorization;
545
+ }
508
546
  intercept(req, next) {
509
547
  let headers = req.headers;
510
548
  if (!headers.has('Accept')) {
@@ -531,7 +569,16 @@ class RequestHeadersInterceptor {
531
569
  const customReq = req.clone({
532
570
  headers
533
571
  });
534
- return next.handle(customReq).pipe(map((res) => {
572
+ return this.updateToken().pipe(map((jwt) => {
573
+ const doesItUseJWT = (this._authentication && this._authentication.length > 32);
574
+ if (doesItUseJWT) {
575
+ this._authentication = `Bearer ${jwt}`;
576
+ }
577
+ return customReq.clone({ setHeaders: { [this._authenticationHeader]: this._authentication } });
578
+ }), switchMap((clonedRequest) => this.handleRequest(next, clonedRequest)));
579
+ }
580
+ handleRequest(next, clonedRequest) {
581
+ return next.handle(clonedRequest).pipe(map((res) => {
535
582
  if (res instanceof HttpResponse) {
536
583
  if (!res.body || !res.headers) {
537
584
  return res;
@@ -565,12 +612,22 @@ class RequestHeadersInterceptor {
565
612
  return headers.set('Content-Type', 'application/*+json');
566
613
  }
567
614
  }
615
+ updateToken() {
616
+ if (!this.authTokenHolderService.jwtAsync) {
617
+ return of(this.authTokenHolderService.jwt);
618
+ }
619
+ return this.authTokenHolderService.jwtAsync.pipe(filter((jwt) => !this.isExpired(jwt)), first());
620
+ }
621
+ isExpired(jwt) {
622
+ const decodedJwt = new DecodedJwt(jwt);
623
+ return decodedJwt.isExpired();
624
+ }
568
625
  }
569
- RequestHeadersInterceptor.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.0", ngImport: i0, type: RequestHeadersInterceptor, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
626
+ RequestHeadersInterceptor.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.0", ngImport: i0, type: RequestHeadersInterceptor, deps: [{ token: AuthTokenHolderService }], target: i0.ɵɵFactoryTarget.Injectable });
570
627
  RequestHeadersInterceptor.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.0", ngImport: i0, type: RequestHeadersInterceptor });
571
628
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.0", ngImport: i0, type: RequestHeadersInterceptor, decorators: [{
572
629
  type: Injectable
573
- }] });
630
+ }], ctorParameters: function () { return [{ type: AuthTokenHolderService }]; } });
574
631
 
575
632
  // tslint:disable:jsdoc-format
576
633
  /**