Fixed an authentication quirk where theqBittorrent API returns a 403 instead of a 401 when its unauthorized.

This commit is contained in:
Roger Far 2020-05-16 20:19:42 -06:00
parent 5d73ae140e
commit 7c6458bbb0
3 changed files with 42 additions and 8 deletions

View file

@ -1,14 +1,14 @@
import { Injectable } from '@angular/core';
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpErrorResponse,
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
} from '@angular/common/http';
import { throwError, Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
@ -20,7 +20,7 @@ export class AuthInterceptor implements HttpInterceptor {
): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
catchError((error: HttpErrorResponse) => {
if (error && error.status === 401) {
if (error && (error.status === 401 || error.status === 403)) {
this.router.navigate(['/login']);
}
return throwError(error);

View file

@ -0,0 +1,32 @@
using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace RdtClient.Service.Middleware
{
public class AuthorizeMiddleware
{
readonly RequestDelegate _next;
public AuthorizeMiddleware(RequestDelegate next)
{
_next = next;
}
/// <summary>
/// Return a 403 instead of a 401, it's quirk that QBittorrent has.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public async Task Invoke(HttpContext context)
{
await _next(context);
if (context.Response.StatusCode == (Int32) HttpStatusCode.Unauthorized)
{
context.Response.StatusCode = (Int32) HttpStatusCode.Forbidden;
}
}
}
}

View file

@ -94,6 +94,8 @@ namespace RdtClient.Web
app.ConfigureExceptionHandler();
app.UseMiddleware<AuthorizeMiddleware>();
app.Use(async (context, next) =>
{
await next.Invoke();