mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
use django authentication instead of auth tokens.
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { AuthGuardService } from './auth-guard.service';
|
||||
|
||||
describe('AuthGuardService', () => {
|
||||
let service: AuthGuardService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(AuthGuardService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
@@ -1,20 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthGuardService {
|
||||
|
||||
constructor(public auth: AuthService, public router: Router) { }
|
||||
|
||||
canActivate(): boolean {
|
||||
if (!this.auth.isAuthenticated()) {
|
||||
this.router.navigate(['login']);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { AuthInterceptor } from './auth.interceptor';
|
||||
|
||||
describe('AuthInterceptor', () => {
|
||||
beforeEach(() => TestBed.configureTestingModule({
|
||||
providers: [
|
||||
AuthInterceptor
|
||||
]
|
||||
}));
|
||||
|
||||
it('should be created', () => {
|
||||
const interceptor: AuthInterceptor = TestBed.inject(AuthInterceptor);
|
||||
expect(interceptor).toBeTruthy();
|
||||
});
|
||||
});
|
@@ -1,37 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
HttpRequest,
|
||||
HttpHandler,
|
||||
HttpEvent,
|
||||
HttpInterceptor,
|
||||
HttpErrorResponse
|
||||
} from '@angular/common/http';
|
||||
import { Observable, throwError } from 'rxjs';
|
||||
import { AuthService } from './auth.service';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
import { Toast, ToastService } from './toast.service';
|
||||
|
||||
@Injectable()
|
||||
export class AuthInterceptor implements HttpInterceptor {
|
||||
|
||||
constructor(private authService: AuthService, private toastService: ToastService) {}
|
||||
|
||||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||
if (this.authService.isAuthenticated()) {
|
||||
request = request.clone({
|
||||
setHeaders: {
|
||||
Authorization: 'Token ' + this.authService.getToken()
|
||||
}
|
||||
});
|
||||
}
|
||||
return next.handle(request).pipe(
|
||||
catchError((error: HttpErrorResponse) => {
|
||||
if (error.status == 401 && this.authService.isAuthenticated()) {
|
||||
this.authService.logout()
|
||||
this.toastService.showToast(Toast.makeError("Your session has expired. Please log in again."))
|
||||
}
|
||||
return throwError(error)
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
describe('AuthService', () => {
|
||||
let service: AuthService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(AuthService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
@@ -1,72 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
import { map } from 'rxjs/operators';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Router } from '@angular/router';
|
||||
import { environment } from 'src/environments/environment';
|
||||
|
||||
interface TokenResponse {
|
||||
token: string
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthService {
|
||||
|
||||
private currentUsername: string
|
||||
|
||||
private token: string
|
||||
|
||||
constructor(private http: HttpClient, private router: Router) {
|
||||
this.token = localStorage.getItem('auth-service:token')
|
||||
if (this.token == null) {
|
||||
this.token = sessionStorage.getItem('auth-service:token')
|
||||
}
|
||||
this.currentUsername = localStorage.getItem('auth-service:currentUsername')
|
||||
if (this.currentUsername == null) {
|
||||
this.currentUsername = sessionStorage.getItem('auth-service:currentUsername')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private requestToken(username: string, password: string): Observable<TokenResponse> {
|
||||
return this.http.post<TokenResponse>(`${environment.apiBaseUrl}token/`, {"username": username, "password": password})
|
||||
}
|
||||
|
||||
isAuthenticated(): boolean {
|
||||
return this.currentUsername != null
|
||||
}
|
||||
|
||||
logout() {
|
||||
this.currentUsername = null
|
||||
this.token = null
|
||||
localStorage.removeItem('auth-service:token')
|
||||
localStorage.removeItem('auth-service:currentUsername')
|
||||
sessionStorage.removeItem('auth-service:token')
|
||||
sessionStorage.removeItem('auth-service:currentUsername')
|
||||
this.router.navigate(['login'])
|
||||
}
|
||||
|
||||
login(username: string, password: string, rememberMe: boolean): Observable<boolean> {
|
||||
return this.requestToken(username,password).pipe(
|
||||
map(tokenResponse => {
|
||||
this.currentUsername = username
|
||||
this.token = tokenResponse.token
|
||||
let storage = rememberMe ? localStorage : sessionStorage
|
||||
storage.setItem('auth-service:token', this.token)
|
||||
storage.setItem('auth-service:currentUsername', this.currentUsername)
|
||||
return true
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
getToken(): string {
|
||||
return this.token
|
||||
}
|
||||
|
||||
getCurrentUsername(): string {
|
||||
return this.currentUsername
|
||||
}
|
||||
}
|
@@ -2,7 +2,6 @@ import { Injectable } from '@angular/core';
|
||||
import { PaperlessDocument } from 'src/app/data/paperless-document';
|
||||
import { AbstractPaperlessService } from './abstract-paperless-service';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { AuthService } from '../auth.service';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Results } from 'src/app/data/results';
|
||||
import { FilterRule } from 'src/app/data/filter-rule';
|
||||
@@ -27,7 +26,7 @@ export const SORT_DIRECTION_DESCENDING = "des"
|
||||
})
|
||||
export class DocumentService extends AbstractPaperlessService<PaperlessDocument> {
|
||||
|
||||
constructor(http: HttpClient, private auth: AuthService) {
|
||||
constructor(http: HttpClient) {
|
||||
super(http, 'documents')
|
||||
}
|
||||
|
||||
@@ -52,15 +51,15 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
|
||||
}
|
||||
|
||||
getPreviewUrl(id: number): string {
|
||||
return this.getResourceUrl(id, 'preview') + `?auth_token=${this.auth.getToken()}`
|
||||
return this.getResourceUrl(id, 'preview')
|
||||
}
|
||||
|
||||
getThumbUrl(id: number): string {
|
||||
return this.getResourceUrl(id, 'thumb') + `?auth_token=${this.auth.getToken()}`
|
||||
return this.getResourceUrl(id, 'thumb')
|
||||
}
|
||||
|
||||
getDownloadUrl(id: number): string {
|
||||
return this.getResourceUrl(id, 'download') + `?auth_token=${this.auth.getToken()}`
|
||||
return this.getResourceUrl(id, 'download')
|
||||
}
|
||||
|
||||
uploadDocument(formData) {
|
||||
|
Reference in New Issue
Block a user