-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
default-resource.interceptor.ts
50 lines (44 loc) · 1.85 KB
/
default-resource.interceptor.ts
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
import { Inject, Injectable, Optional } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { from, Observable } from 'rxjs';
import { catchError, mergeMap } from 'rxjs/operators';
import { ResourceErrorHandler } from './resource-error-handler';
import { AuthService } from '../services/auth.service';
import { OidcResourceInterceptorConfig } from '../types';
@Injectable()
export class DefaultResourceInterceptor implements HttpInterceptor {
bypass: boolean;
bearerPrefix: string;
authorizationHeaderName: string;
constructor(
private authService: AuthService,
private errorHandler: ResourceErrorHandler,
@Optional() private resourceInterceptorConfig: OidcResourceInterceptorConfig,
) {
this.bypass = !this.authService || !this.resourceInterceptorConfig || !this.resourceInterceptorConfig.allowedUrls;
this.bearerPrefix = resourceInterceptorConfig.bearerPrefix.trim().concat(' ');
this.authorizationHeaderName = resourceInterceptorConfig.authorizationHeaderName;
}
private checkUrl(url: string): boolean {
const found = this.resourceInterceptorConfig.allowedUrls.find(u => url.startsWith(u));
return !!found;
}
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (this.bypass) {
// TODO: do we need this check?
return next.handle(req);
}
if (!this.checkUrl(req.url.toLowerCase())) {
return next.handle(req);
}
return from(this.authService.getToken()).pipe(
mergeMap(accessToken => {
if (accessToken) {
const headers = req.headers.set(this.authorizationHeaderName, this.bearerPrefix + accessToken);
req = req.clone({ headers });
}
return next.handle(req).pipe(catchError(err => this.errorHandler.handleError(err)));
}),
);
}
}