X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fshared-main%2Fauth%2Fauth-interceptor.service.ts;h=93b3a93d6d4255def5a42c0f606353c05fb98f96;hb=d0fbc9fd0a29c37f3ff9b99030351e90b276fe7d;hp=68a4acdb5242466766ad566a5005e04e532bcd36;hpb=67ed6552b831df66713bac9e672738796128d33f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/shared-main/auth/auth-interceptor.service.ts b/client/src/app/shared/shared-main/auth/auth-interceptor.service.ts index 68a4acdb5..93b3a93d6 100644 --- a/client/src/app/shared/shared-main/auth/auth-interceptor.service.ts +++ b/client/src/app/shared/shared-main/auth/auth-interceptor.service.ts @@ -1,15 +1,18 @@ -import { Observable, throwError as observableThrowError } from 'rxjs' +import { Observable, of, throwError as observableThrowError } from 'rxjs' import { catchError, switchMap } from 'rxjs/operators' -import { HTTP_INTERCEPTORS, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http' +import { HTTP_INTERCEPTORS, HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http' import { Injectable, Injector } from '@angular/core' +import { Router } from '@angular/router' import { AuthService } from '@app/core/auth/auth.service' +import { HttpStatusCode } from '@shared/models' +import { OAuth2ErrorCode, PeerTubeProblemDocument } from '@shared/models/server' @Injectable() export class AuthInterceptor implements HttpInterceptor { private authService: AuthService // https://github.com/angular/angular/issues/18224#issuecomment-316957213 - constructor (private injector: Injector) {} + constructor (private injector: Injector, private router: Router) {} intercept (req: HttpRequest, next: HttpHandler): Observable> { if (this.authService === undefined) { @@ -22,12 +25,21 @@ export class AuthInterceptor implements HttpInterceptor { // Catch 401 errors (refresh token expired) return next.handle(authReq) .pipe( - catchError(err => { - if (err.status === 401 && err.error && err.error.code === 'invalid_token') { - return this.handleTokenExpired(req, next) + catchError((err: HttpErrorResponse) => { + const error = err.error as PeerTubeProblemDocument + const isOTPMissingError = this.authService.isOTPMissingError(err) + + if (!isOTPMissingError) { + if (err.status === HttpStatusCode.UNAUTHORIZED_401 && error && error.code === OAuth2ErrorCode.INVALID_TOKEN) { + return this.handleTokenExpired(req, next) + } + + if (err.status === HttpStatusCode.UNAUTHORIZED_401) { + return this.handleNotAuthenticated(err) + } } - return observableThrowError(err) + return observableThrowError(() => err) }) ) } @@ -51,6 +63,11 @@ export class AuthInterceptor implements HttpInterceptor { // Clone the request to add the new header return req.clone({ headers: req.headers.set('Authorization', authHeaderValue) }) } + + private handleNotAuthenticated (err: HttpErrorResponse): Observable { + this.router.navigate([ '/401' ], { state: { obj: err }, skipLocationChange: true }) + return of(err.message) + } } export const AUTH_INTERCEPTOR_PROVIDER = {