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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 15x 15x 15x 15x 15x 15x 15x 15x 15x 19x 19x 19x 15x 15x 1x 1x 15x 5x 1x 2x 2x 4x 4x 5x 5x 5x 4x 3x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 15x 15x 30x 14x 15x 19x 1x 15x 30x 30x 15x 9x 1x 15x 9x 1x 1x 1x 1x 1x 1x 16x 16x 31x 4x 27x 5x 22x 2x 20x 20x 20x 13x 49x 49x 32x 32x 17x | import { HttpErrorResponse, HttpEvent, HttpHandler, HttpParams, HttpRequest, HttpResponse } from '@angular/common/http'; import { ApplicationRef, Injectable } from '@angular/core'; import { Store, select } from '@ngrx/store'; import { CookieOptions } from 'express'; import { isEqual } from 'lodash-es'; import { BehaviorSubject, Observable, OperatorFunction, Subject, combineLatest, iif, interval, noop, of, race, throwError, timer, } from 'rxjs'; import { catchError, concatMap, distinctUntilChanged, filter, first, map, mergeMap, pairwise, shareReplay, startWith, switchMap, take, tap, withLatestFrom, } from 'rxjs/operators'; import { BasketView } from 'ish-core/models/basket/basket.model'; import { User } from 'ish-core/models/user/user.model'; import { ApiService } from 'ish-core/services/api/api.service'; import { getCurrentBasket, getCurrentBasketId, loadBasketByAPIToken } from 'ish-core/store/customer/basket'; import { getOrder, getSelectedOrderId, loadOrderByAPIToken } from 'ish-core/store/customer/orders'; import { fetchAnonymousUserToken, getLoggedInUser, getUserAuthorized, loadUserByAPIToken, } from 'ish-core/store/customer/user'; import { CookiesService } from 'ish-core/utils/cookies/cookies.service'; import { whenTruthy } from 'ish-core/utils/operators'; type ApiTokenCookieType = 'user' | 'order'; export interface ApiTokenCookie { apiToken: string; type: ApiTokenCookieType; isAnonymous?: boolean; orderId?: string; creator?: string; } // If no expiry date is supplied by the token endpoint, this value (in ms) is used const DEFAULT_EXPIRY_TIME = 3600000; @Injectable({ providedIn: 'root' }) export class ApiTokenService { /** * stores current apiToken information, will be used to add authentication header for each request */ private apiToken$: BehaviorSubject<string>; /** * informs subscriber that cookie is unexpectedly removed (e.g. logout from another tab) */ private cookieVanishes$ = new Subject<ApiTokenCookieType>(); /** * cookie options which are used to store apiToken cookie, will be overwritten by setApiToken() method */ private cookieOptions: CookieOptions = {}; /** * informs subscriber (e.g. restore$()) with value from initial apiToken cookie, * is used by tokenCreatedOnAnotherTab() method to trigger restore$() method again */ private initialCookie$: BehaviorSubject<ApiTokenCookie>; /** * informs subscriber whenever an apiToken has been changed */ private cookieChangeEvent$: Observable<[ApiTokenCookie, ApiTokenCookie]>; constructor(private cookiesService: CookiesService, private store: Store, private appRef: ApplicationRef) { // setup initial values const initialCookie = this.parseCookie(); this.initialCookie$ = new BehaviorSubject<ApiTokenCookie>(!SSR ? initialCookie : undefined); this.apiToken$ = new BehaviorSubject<string>(initialCookie?.apiToken); if (!SSR) { // multicast apiTokenCookieChange$ to avoid multiple listeners this.cookieChangeEvent$ = this.apiTokenCookieChange$().pipe(shareReplay(1)); // save internal calculated apiToken as cookie whenever apiToken, basket, user or order information changes this.calculateApiTokenCookieValueOnChanges$().subscribe(apiToken => { const cookieContent = apiToken?.apiToken ? JSON.stringify(apiToken) : undefined; if (cookieContent) { this.cookiesService.put('apiToken', cookieContent, { expires: this.cookieOptions?.expires ?? new Date(Date.now() + DEFAULT_EXPIRY_TIME), secure: this.cookieOptions?.secure, path: '/', }); } }); // remove apiToken cookie on logout // path: '/' is added in order to remove cookie within a multi-site configuration (e.g. configured /en, /fr, /de routes) this.logoutUser$().subscribe(() => this.cookiesService.remove('apiToken', { path: '/' })); // unset apiToken when cookie vanishes/ has been removed unexpectedly and notify public event stream this.tokenVanish$().subscribe(type => { this.removeApiToken(); this.cookieVanishes$.next(type); }); // initialize restore$ mechanism when apiToken cookie is created outside of current PWA context (e.g. login in another tab) this.tokenCreatedOnAnotherTab$().subscribe(noop); } } getApiToken$(): Observable<string> { return this.apiToken$.asObservable(); } /** * removes the current apiToken cookie */ removeApiToken() { this.apiToken$.next(undefined); } /** * sets new apiToken and new apiToken cookie options when available */ setApiToken(apiToken: string, options?: CookieOptions) { Iif (options) { this.cookieOptions = options; } this.apiToken$.next(apiToken); } hasUserApiTokenCookie() { const apiTokenCookie = this.parseCookie(); return apiTokenCookie?.type === 'user' && !apiTokenCookie?.isAnonymous; } /** * trigger actions to restore store information based on initial apiToken cookie */ restore$(types: ApiTokenCookieType[] = ['user', 'order']): Observable<boolean> { Iif (SSR) { return of(true); } return this.initialCookie$.pipe( first(), switchMap(cookie => { if (types.includes(cookie?.type)) { switch (cookie?.type) { case 'user': { if (cookie.isAnonymous) { this.store.dispatch(loadBasketByAPIToken({ apiToken: cookie.apiToken })); // wait until basket infos are loaded return race( this.store.pipe( select(getCurrentBasketId), whenTruthy(), take(1), map(() => true) ), timer(5000).pipe(map(() => false)) ); } else { this.store.dispatch(loadUserByAPIToken()); // wait until user is logged in return race( this.store.pipe(select(getUserAuthorized), whenTruthy(), take(1)), timer(5000).pipe(map(() => false)) ); } } case 'order': { this.store.dispatch(loadOrderByAPIToken({ orderId: cookie.orderId, apiToken: cookie.apiToken })); // wait until order is loaded return race( this.store.pipe( select(getOrder(cookie.orderId)), whenTruthy(), take(1), map(() => true) ), timer(5000).pipe(map(() => false)) ); } } } return of(true); }) ); } getCookieVanishes$(): Observable<ApiTokenCookieType> { return this.cookieVanishes$.asObservable(); } intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { return iif( () => req.url.endsWith('/baskets') && req.method === 'POST', // only on basket creation an anonymous user token can be created this.anonymousUserTokenMechanism(), of(true) ).pipe( switchMap(() => this.appendAuthenticationHeader(req).pipe( concatMap(request => next.handle(request).pipe( map(event => { // remove id_token from /token response // TODO: remove http request body adaptions if correct id_tokens are returned Iif ( event instanceof HttpResponse && event.url.endsWith('token') && request.body instanceof HttpParams ) { return event.clone({ body: { ...event.body, id_token: undefined, }, }); } return event; }), catchError(err => { Iif (this.isAuthTokenError(err)) { this.invalidateApiToken(); // retry request without auth token const retryRequest = request.clone({ headers: request.headers.delete(ApiService.TOKEN_HEADER_KEY), }); // timer introduced for testability return timer(500).pipe(switchMap(() => next.handle(retryRequest))); } return throwError(() => err); }) ) ) ) ) ); } /** * will remove apiToken and inform cookieVanishes$ listeners that cookie in not working as expected */ private invalidateApiToken() { const cookie = this.parseCookie(); this.removeApiToken(); Iif (cookie) { this.cookieVanishes$.next(cookie?.type); } } private isAuthTokenError(err: unknown) { return ( err instanceof HttpErrorResponse && typeof err.error === 'string' && (err.error.includes('AuthenticationToken') || err.error.includes('Unable to decode token')) // possible auth token error codes ); } /** * append authentication header to current request in case an apiToken is available and specific header was not set before */ private appendAuthenticationHeader(req: HttpRequest<unknown>): Observable<HttpRequest<unknown>> { return this.apiToken$.pipe( map(apiToken => apiToken && !req.headers?.has(ApiService.TOKEN_HEADER_KEY) ? req.clone({ headers: req.headers.set(ApiService.TOKEN_HEADER_KEY, apiToken) }) : req ), first() ); } /** * process to trigger action to fetch anonymous user token and wait until apiToken is set in service */ private anonymousUserTokenMechanism(): Observable<unknown> { return this.apiToken$.pipe( switchMap( apiToken => iif(() => !apiToken, of(false).pipe(tap(() => this.store.dispatch(fetchAnonymousUserToken()))), of(true)) // fetch anonymous user token only when api token is not available ), whenTruthy(), first() ); } /** * Observable which emits with the last two apiToken cookie data only, when these have changed within 1000ms interval */ private apiTokenCookieChange$(): Observable<[ApiTokenCookie, ApiTokenCookie]> { return this.appRef.isStable.pipe( whenTruthy(), first(), mergeMap(() => interval(1000).pipe( map(() => this.parseCookie()), pairwise(), distinctUntilChanged((prev, curr) => isEqual(prev, curr)) ) ) ); } /** * notify subscriber when user has been logged out */ private logoutUser$(): Observable<boolean> { return this.store.pipe( select(getLoggedInUser), startWith(undefined), pairwise(), filter(([previous, current]) => !!previous && !current), map(() => true) ); } /** * @returns calculate current apiToken cookie value based on the internal store$ state */ private calculateApiTokenCookieValueOnChanges$(): Observable<ApiTokenCookie> { return combineLatest([ this.store.pipe(select(getLoggedInUser)), this.store.pipe(select(getCurrentBasket)), this.store.pipe(select(getSelectedOrderId)), this.apiToken$, ]).pipe( switchMap(value => of(value)), this.mapToApiTokenCookie(), filter(apiToken => !!apiToken?.apiToken), distinctUntilChanged<ApiTokenCookie>(isEqual) ); } /** * @returns previous apiToken cookie type when the cookie vanishes */ private tokenVanish$(): Observable<ApiTokenCookieType> { // access token vanishes routine return this.cookieChangeEvent$.pipe( withLatestFrom(this.apiToken$), filter(([[previous, current], apiToken]) => !!previous && !current && !!apiToken), map(([[previous]]) => previous.type) ); } /** * initialize restore$ mechanism when apiToken cookie is created outside of current PWA context * * Steps: * * (1): stream got notified when 'apiToken' cookie changes * * (2): check that a user token cookie was added in comparison to previous cookie value * * (3): calculate a current apiToken cookie value of current pwa context * * (4): filter stream values when calculated apiToken cookie value is not truthy --> cookie was added from another tab * * (5): set new apiToken and initialCookie$ values with information from current apiToken cookie in order to fetch data via restore$ * */ private tokenCreatedOnAnotherTab$(): Observable<boolean> { // cookie created routine when user is logged in in an another tab return this.cookieChangeEvent$.pipe( filter(([previous, current]) => !previous && current?.type === 'user' && !!current?.apiToken), // first time after a login cookie appears withLatestFrom( this.store.pipe(select(getLoggedInUser)), this.store.pipe(select(getCurrentBasket)), this.store.pipe(select(getSelectedOrderId)), this.apiToken$ ), switchMap(([[, current], ...ctx]) => of(ctx).pipe( this.mapToApiTokenCookie(), filter(calculated => !calculated), // application calculated no api token cookie although an user cookie is stored map(() => current) ) ), tap(cookieValue => { this.setApiToken(cookieValue.apiToken); this.initialCookie$.next(cookieValue); }), switchMap(() => this.restore$()) ); } private mapToApiTokenCookie(): OperatorFunction<[User, BasketView, string, string], ApiTokenCookie> { return (source$: Observable<[User, BasketView, string, string]>) => source$.pipe( map(([user, basket, orderId, apiToken]): ApiTokenCookie => { if (user) { return { apiToken, type: 'user', isAnonymous: false, creator: 'pwa' }; } else if (basket) { return { apiToken, type: 'user', isAnonymous: true, creator: 'pwa' }; } else if (orderId) { return { apiToken, type: 'order', orderId, creator: 'pwa' }; } const apiTokenCookieString = this.cookiesService.get('apiToken'); const apiTokenCookie: ApiTokenCookie = apiTokenCookieString ? JSON.parse(apiTokenCookieString) : undefined; if (apiToken && apiTokenCookie) { return { ...apiTokenCookie, apiToken }; // overwrite existing cookie information with new apiToken } }) ); } private parseCookie(): ApiTokenCookie { const cookieContent = this.cookiesService.get('apiToken'); if (cookieContent) { try { return JSON.parse(cookieContent); } catch (err) { // ignore } } return; } } |