-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added setup of CSRF, and interceptor that adds/gets relevant token he…
…ader. WithCredentials is needed to send/receive cookies.
- Loading branch information
1 parent
ec9810c
commit 1323682
Showing
3 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const CsrfCookieName = "token-cookie-name"; | ||
export const CsrfHeaderName = "x-csrf-token"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from "@angular/common/http"; | ||
import { Injectable } from "@angular/core"; | ||
import { environment } from "@environments/environment"; | ||
import { CsrfHeaderName } from "@shared/constants/csrf-constants"; | ||
import { Observable, of } from "rxjs"; | ||
import { catchError, switchMap, tap } from "rxjs/operators"; | ||
|
||
@Injectable() | ||
export class CsrfInterceptor implements HttpInterceptor { | ||
private baseUrl = environment.baseUrl; | ||
private tokenUrl = "csrf/token"; | ||
private csrfToken: string; | ||
constructor(private httpClient: HttpClient) {} | ||
|
||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
if (this.csrfToken || req.url.endsWith(this.tokenUrl)) { | ||
const cloned = req.clone({ | ||
headers: req.headers.set(CsrfHeaderName, this?.csrfToken ?? ""), | ||
withCredentials: req.withCredentials || ["POST", "PUT", "DELETE"].includes(req.method), | ||
}); | ||
|
||
return next.handle(cloned); | ||
} | ||
|
||
// If no CSRF token, fetch it first | ||
return this.fetchCsrfToken().pipe( | ||
switchMap(() => { | ||
return next.handle(req); | ||
}), | ||
catchError(err => { | ||
console.error("CSRF token fetch failed", err); | ||
return next.handle(req); | ||
}) | ||
); | ||
} | ||
|
||
private fetchCsrfToken(): Observable<string> { | ||
return this.httpClient.get<{ token: string }>(this.baseUrl + this.tokenUrl, { withCredentials: true }).pipe( | ||
tap(response => { | ||
this.csrfToken = response.token; | ||
}), | ||
switchMap(response => of(response.token)) | ||
); | ||
} | ||
} |