SHARE
TWEET

Untitled

a guest Jan 20th, 2020 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
  2. import { Injectable } from '@angular/core';
  3. import { BroadcastService, MsalService } from '@azure/msal-angular';
  4. import { from } from 'rxjs';
  5. import { Observable } from 'rxjs/Observable';
  6.  
  7. // TODO: Remove when MSAL library supports Angular 6, and use MsalInterceptor instead
  8.  
  9. @Injectable()
  10. export class MsalInterceptorTmp implements HttpInterceptor {
  11.  
  12.     constructor(private auth: MsalService, private broadcastService: BroadcastService) { }
  13.  
  14.     intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  15.         const scopes = this.auth.getScopesForEndpoint(req.url);
  16.         this.auth.verbose('Url: ' + req.url + ' maps to scopes: ' + scopes);
  17.         if (scopes === null) {
  18.             return next.handle(req);
  19.         }
  20.         const tokenStored = this.auth.getCachedTokenInternal(scopes);
  21.         if (tokenStored && tokenStored.token) {
  22.             req = req.clone({
  23.                 setHeaders: {
  24.                     Authorization: `Bearer ${tokenStored.token}`,
  25.                 }
  26.             });
  27.             return next.handle(req).do(event => { }, err => {
  28.                 if (err instanceof HttpErrorResponse && err.status === 401) {
  29.                     const scopesl = this.auth.getScopesForEndpoint(req.url);
  30.                     const tokenStoredl = this.auth.getCachedTokenInternal(scopesl);
  31.                     if (tokenStoredl && tokenStoredl.token) {
  32.                         this.auth.clearCacheForScope(tokenStoredl.token);
  33.                     }
  34.                     this.broadcastService.broadcast('msal:notAuthorized', { err, scopesl });
  35.                 }
  36.             });
  37.         } else {
  38.             return from(this.auth.acquireTokenSilent(scopes).then(token => {
  39.                 const JWT = `Bearer ${token}`;
  40.                 return req.clone({
  41.                     setHeaders: {
  42.                         Authorization: JWT,
  43.                     },
  44.                 });
  45.             })).mergeMap(reql => next.handle(reql).do(event => { }, err => {
  46.                 if (err instanceof HttpErrorResponse && err.status === 401) {
  47.                     const scopesl = this.auth.getScopesForEndpoint(reql.url);
  48.                     const tokenStoredl = this.auth.getCachedTokenInternal(scopesl);
  49.                     if (tokenStoredl && tokenStoredl.token) {
  50.                         this.auth.clearCacheForScope(tokenStoredl.token);
  51.                     }
  52.                     this.broadcastService.broadcast('msal:notAuthorized', { err, scopesl });
  53.                 }
  54.             })); // calling next.handle means we are passing control to next interceptor in chain
  55.         }
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top