|
@@ -1,8 +1,9 @@
|
|
|
import { catchError, switchMap } from 'rxjs/operators';
|
|
import { catchError, switchMap } from 'rxjs/operators';
|
|
|
import { throwError } from "rxjs";
|
|
import { throwError } from "rxjs";
|
|
|
import { Injectable } from '@angular/core';
|
|
import { Injectable } from '@angular/core';
|
|
|
-import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpErrorResponse } from '@angular/common/http';
|
|
|
|
|
|
|
+import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpErrorResponse, HttpHeaders, HttpXsrfTokenExtractor } from '@angular/common/http';
|
|
|
import { Observable } from 'rxjs';
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
+
|
|
|
import { AccountService } from './account.service';
|
|
import { AccountService } from './account.service';
|
|
|
|
|
|
|
|
@Injectable()
|
|
@Injectable()
|
|
@@ -14,13 +15,11 @@ export class AuthInterceptor implements HttpInterceptor
|
|
|
|
|
|
|
|
private isRefreshing = false;
|
|
private isRefreshing = false;
|
|
|
|
|
|
|
|
- constructor(private accountService: AccountService) { }
|
|
|
|
|
|
|
+ constructor(private csrfTokenExtrator: HttpXsrfTokenExtractor, private accountService: AccountService) { }
|
|
|
|
|
|
|
|
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>>
|
|
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>>
|
|
|
{
|
|
{
|
|
|
- const authToken = this.accountService.getAccessToken();
|
|
|
|
|
-
|
|
|
|
|
- const modified = request.clone({ headers: request.headers.append('Authorization', "Bearer " + authToken) });
|
|
|
|
|
|
|
+ const modified = request.clone({ headers: this.addExtraHeaders(request.headers, (request.method == "POST" || request.method == "PUT" || request.method == "DELETE")) });
|
|
|
|
|
|
|
|
return next.handle(modified).pipe(
|
|
return next.handle(modified).pipe(
|
|
|
catchError((error) => { if (error instanceof HttpErrorResponse && !request.url.includes('/sign/in') && error.status === 403) { return this.handle401Error(request, next); } return throwError(() => error); })
|
|
catchError((error) => { if (error instanceof HttpErrorResponse && !request.url.includes('/sign/in') && error.status === 403) { return this.handle401Error(request, next); } return throwError(() => error); })
|
|
@@ -38,8 +37,7 @@ export class AuthInterceptor implements HttpInterceptor
|
|
|
return this.accountService.updateToken().pipe(
|
|
return this.accountService.updateToken().pipe(
|
|
|
switchMap(() => {
|
|
switchMap(() => {
|
|
|
this.isRefreshing = false;
|
|
this.isRefreshing = false;
|
|
|
- const authToken = this.accountService.getAccessToken();
|
|
|
|
|
- const modified = request.clone({ headers: request.headers.append('Authorization', "Bearer " + authToken) });
|
|
|
|
|
|
|
+ const modified = request.clone({ headers: this.addExtraHeaders(request.headers, (request.method == "POST" || request.method == "PUT" || request.method == "DELETE")) });
|
|
|
return next.handle(modified);
|
|
return next.handle(modified);
|
|
|
}),
|
|
}),
|
|
|
catchError((error) => {
|
|
catchError((error) => {
|
|
@@ -54,4 +52,21 @@ export class AuthInterceptor implements HttpInterceptor
|
|
|
return next.handle(request);
|
|
return next.handle(request);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private addExtraHeaders(headers: HttpHeaders, postput: boolean): HttpHeaders
|
|
|
|
|
+ {
|
|
|
|
|
+ const authToken = this.accountService.getAccessToken();
|
|
|
|
|
+
|
|
|
|
|
+ headers = headers.append('Authorization', "Bearer " + authToken);
|
|
|
|
|
+
|
|
|
|
|
+ const csrfToken = this.csrfTokenExtrator.getToken() as string;
|
|
|
|
|
+
|
|
|
|
|
+ console.log("csrfToken = " + csrfToken); // TODO
|
|
|
|
|
+
|
|
|
|
|
+ if (postput && (csrfToken != null))
|
|
|
|
|
+ {
|
|
|
|
|
+ headers = headers.append('X-XSRF-TOKEN', csrfToken);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return headers;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|