All files / src/app/extensions/punchout/identity-provider punchout-identity-provider.ts

95.18% Statements 79/83
84.84% Branches 56/66
89.65% Functions 26/29
95.12% Lines 78/82

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203  2x 2x 2x 2x 2x   2x 2x 2x   2x 2x 2x 2x 2x   2x     2x   16x 16x 16x 16x 16x 16x 16x 16x 16x                       16x             16x   16x 3x             12x               1x 1x     11x 4x   7x           11x         1x                 10x 3x   7x 7x         2x 1x     1x 1x 1x 1x               2x 2x           2x 2x 2x   2x   2x   2x   2x 2x                       3x     1x 1x 1x           1x 1x           7x   7x 7x   6x   1x       7x 1x     6x 2x     2x 2x       4x 1x 1x 1x         3x        
import { HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { Observable, noop, of, race, throwError } from 'rxjs';
import { catchError, concatMap, delay, filter, first, map, switchMap, take, tap } from 'rxjs/operators';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { AppFacade } from 'ish-core/facades/app.facade';
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { IdentityProvider, TriggerReturnType } from 'ish-core/identity-provider/identity-provider.interface';
import { TokenService } from 'ish-core/services/token/token.service';
import { selectQueryParam } from 'ish-core/store/core/router';
import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';
import { CookiesService } from 'ish-core/utils/cookies/cookies.service';
import { whenTruthy } from 'ish-core/utils/operators';
 
import { PunchoutService } from '../services/punchout/punchout.service';
 
@Injectable({ providedIn: 'root' })
export class PunchoutIdentityProvider implements IdentityProvider {
  constructor(
    protected router: Router,
    protected store: Store,
    protected apiTokenService: ApiTokenService,
    private appFacade: AppFacade,
    private accountFacade: AccountFacade,
    private punchoutService: PunchoutService,
    private cookiesService: CookiesService,
    private checkoutFacade: CheckoutFacade,
    private tokenService: TokenService
  ) {}
 
  getCapabilities() {
    return {
      editPassword: true,
      editEmail: true,
      editProfile: true,
    };
  }
 
  init() {
    this.apiTokenService.getCookieVanishes$().subscribe(type => {
      Iif (type === 'user') {
        this.accountFacade.logoutUser({ revokeApiToken: false });
      }
    });
 
    // OAuth Service should be configured before apiToken information are restored and the refresh token mechanism is setup
    this.apiTokenService.restore$(['user', 'order']).subscribe(noop);
 
    this.checkoutFacade.basket$.pipe(whenTruthy(), first()).subscribe(basketView => {
      window.sessionStorage.setItem('basket-id', basketView.id);
    });
  }
 
  triggerLogin(route: ActivatedRouteSnapshot): TriggerReturnType {
    // check for required start parameters before doing anything with the punchout route
    // 'sid', 'access-token' (cXML) or 'HOOK_URL', 'USERNAME', 'PASSWORD' (OCI)
    if (
      !(
        (route.queryParamMap.has('sid') && route.queryParamMap.has('access-token')) ||
        (route.queryParamMap.has('HOOK_URL') &&
          route.queryParamMap.has('USERNAME') &&
          route.queryParamMap.has('PASSWORD'))
      )
    ) {
      this.appFacade.setBusinessError('punchout.error.missing.parameters');
      return false;
    }
 
    if (route.queryParamMap.has('access-token')) {
      this.accountFacade.loginUserWithToken(route.queryParamMap.get('access-token'));
    } else {
      this.accountFacade.loginUser({
        login: route.queryParamMap.get('USERNAME'),
        password: route.queryParamMap.get('PASSWORD'),
      });
    }
 
    return race(
      // throw an error if a user login error occurs
      this.accountFacade.userError$.pipe(
        whenTruthy(),
        take(1),
        concatMap(userError => throwError(() => userError))
      ),
 
      // handle the punchout functions once the punchout user is logged in
      this.accountFacade.isLoggedIn$.pipe(
        whenTruthy(),
        take(1),
        switchMap(() => {
          // handle cXML punchout with sid
          if (route.queryParamMap.get('sid')) {
            return this.handleCxmlPunchoutLogin(route);
            // handle OCI punchout with HOOK_URL
          } else if (route.queryParamMap.get('HOOK_URL')) {
            return this.handleOciPunchoutLogin(route);
          }
        }),
        // punchout error after successful authentication (needs to logout)
        catchError(error =>
          this.accountFacade.userLoading$.pipe(
            first(loading => !loading),
            delay(0),
            switchMap(() => {
              this.accountFacade.logoutUser();
              this.apiTokenService.removeApiToken();
              this.appFacade.setBusinessError(error);
              return of(this.router.parseUrl('/error'));
            })
          )
        )
      )
    ).pipe(
      // general punchout error handling (parameter missing, authentication error)
      catchError(error => {
        this.appFacade.setBusinessError(error);
        return of(this.router.parseUrl('/error'));
      })
    );
  }
 
  triggerLogout(): TriggerReturnType {
    window.sessionStorage.removeItem('basket-id');
    this.accountFacade.logoutUser(); // user will be logged out and related refresh token is revoked on server
    return this.accountFacade.isLoggedIn$.pipe(
      // wait until the user is logged out before you go to homepage to prevent unnecessary REST calls
      filter(loggedIn => !loggedIn),
      take(1),
      tap(() => this.tokenService.logOut()), // remove token from storage when user is logged out
      switchMap(() =>
        this.store.pipe(
          select(selectQueryParam('returnUrl')),
          map(returnUrl => returnUrl || '/home'),
          map(returnUrl => this.router.parseUrl(returnUrl))
        )
      )
    );
  }
 
  intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    return this.apiTokenService.intercept(req, next);
  }
 
  private handleCxmlPunchoutLogin(route: ActivatedRouteSnapshot): Observable<UrlTree> {
    // fetch sid session information (basketId, returnURL, operation, ...)
    return this.punchoutService.getCxmlPunchoutSession(route.queryParamMap.get('sid')).pipe(
      // persist cXML session information (sid, returnURL, basketId) in cookies for later basket transfer
      tap(data => {
        this.cookiesService.put('punchout_SID', route.queryParamMap.get('sid'));
        this.cookiesService.put('punchout_ReturnURL', data.returnURL);
        this.cookiesService.put('punchout_BasketID', data.basketId);
      }),
      // use the basketId basket for the current PWA session (instead of default current basket)
      // TODO: if load basket error (currently no error page) -> logout and do not use default 'current' basket
      // TODO: if loadBasketWithId is faster then the initial loading of the 'current' basket after login the wrong 'current' basket might be used (the additional delay is the current work around)
      delay(500),
      tap(data => this.checkoutFacade.loadBasketWithId(data.basketId)),
      map(() => this.router.parseUrl('/home'))
    );
  }
 
  private handleOciPunchoutLogin(route: ActivatedRouteSnapshot) {
    // save HOOK_URL to cookie for later basket transfer
    this.cookiesService.put('punchout_HookURL', route.queryParamMap.get('HOOK_URL'));
 
    const basketId = window.sessionStorage.getItem('basket-id');
    if (!basketId) {
      // create a new basket for every punchout session to avoid basket conflicts for concurrent punchout sessions
      this.checkoutFacade.createBasket();
    } else {
      this.checkoutFacade.loadBasketWithId(basketId);
    }
 
    // Product Details
    if (route.queryParamMap.get('FUNCTION') === 'DETAIL' && route.queryParamMap.get('PRODUCTID')) {
      return of(this.router.parseUrl(`/product/${route.queryParamMap.get('PRODUCTID')}`));
 
      // Validation of Products
    } else if (route.queryParamMap.get('FUNCTION') === 'VALIDATE' && route.queryParamMap.get('PRODUCTID')) {
      return this.punchoutService
        .getOciPunchoutProductData(route.queryParamMap.get('PRODUCTID'), route.queryParamMap.get('QUANTITY') || '1')
        .pipe(
          tap(data => this.punchoutService.submitOciPunchoutData(data)),
          map(() => false)
        );
 
      // Background Search
    } else if (route.queryParamMap.get('FUNCTION') === 'BACKGROUND_SEARCH' && route.queryParamMap.get('SEARCHSTRING')) {
      return this.punchoutService.getOciPunchoutSearchData(route.queryParamMap.get('SEARCHSTRING')).pipe(
        tap(data => this.punchoutService.submitOciPunchoutData(data, false)),
        map(() => false)
      );
 
      // Login
    } else {
      return of(this.router.parseUrl('/home'));
    }
  }
}