mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	Frontend: CSRF support
This commit is contained in:
		| @@ -39,6 +39,8 @@ import { InfiniteScrollModule } from 'ngx-infinite-scroll'; | ||||
| import { DateTimeComponent } from './components/common/input/date-time/date-time.component'; | ||||
| import { TagsComponent } from './components/common/input/tags/tags.component'; | ||||
| import { SortableDirective } from './directives/sortable.directive'; | ||||
| import { CookieService } from 'ngx-cookie-service'; | ||||
| import { CsrfInterceptor } from './interceptors/csrf.interceptor'; | ||||
|  | ||||
| @NgModule({ | ||||
|   declarations: [ | ||||
| @@ -85,7 +87,12 @@ import { SortableDirective } from './directives/sortable.directive'; | ||||
|     InfiniteScrollModule | ||||
|   ], | ||||
|   providers: [ | ||||
|     DatePipe | ||||
|     DatePipe, | ||||
|     CookieService, { | ||||
|       provide: HTTP_INTERCEPTORS, | ||||
|       useClass: CsrfInterceptor, | ||||
|       multi: true | ||||
|     } | ||||
|   ], | ||||
|   bootstrap: [AppComponent] | ||||
| }) | ||||
|   | ||||
							
								
								
									
										16
									
								
								src-ui/src/app/interceptors/csrf.interceptor.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src-ui/src/app/interceptors/csrf.interceptor.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| import { TestBed } from '@angular/core/testing'; | ||||
|  | ||||
| import { CsrfInterceptor } from './csrf.interceptor'; | ||||
|  | ||||
| describe('CsrfInterceptor', () => { | ||||
|   beforeEach(() => TestBed.configureTestingModule({ | ||||
|     providers: [ | ||||
|       CsrfInterceptor | ||||
|       ] | ||||
|   })); | ||||
|  | ||||
|   it('should be created', () => { | ||||
|     const interceptor: CsrfInterceptor = TestBed.inject(CsrfInterceptor); | ||||
|     expect(interceptor).toBeTruthy(); | ||||
|   }); | ||||
| }); | ||||
							
								
								
									
										30
									
								
								src-ui/src/app/interceptors/csrf.interceptor.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src-ui/src/app/interceptors/csrf.interceptor.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| import { Injectable } from '@angular/core'; | ||||
| import { | ||||
|   HttpRequest, | ||||
|   HttpHandler, | ||||
|   HttpEvent, | ||||
|   HttpInterceptor | ||||
| } from '@angular/common/http'; | ||||
| import { Observable } from 'rxjs'; | ||||
| import { CookieService } from 'ngx-cookie-service'; | ||||
|  | ||||
| @Injectable() | ||||
| export class CsrfInterceptor implements HttpInterceptor { | ||||
|  | ||||
|   constructor(private cookieService: CookieService) { | ||||
|  | ||||
|   } | ||||
|  | ||||
|   intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { | ||||
|     let csrfToken = this.cookieService.get('csrftoken') | ||||
|     if (csrfToken) { | ||||
|      request = request.clone({ | ||||
|         setHeaders: { | ||||
|           'X-CSRFToken': csrfToken | ||||
|         } | ||||
|       }) | ||||
|     } | ||||
|  | ||||
|     return next.handle(request); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Jonas Winkler
					Jonas Winkler