Skip to content

Commit

Permalink
Another test for forwarding authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
majora2007 committed Jan 5, 2024
1 parent 1e1d50c commit a1f68d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions API/Middleware/CustomAuthHeaderMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using API.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace API.Middleware;

Expand All @@ -15,7 +16,7 @@ public class CustomAuthHeaderMiddleware(RequestDelegate next)
private readonly string[] allowedIpAddresses = { "192.168.1.0/24", "2001:db8::/32", "116.202.233.5", "104.21.81.112" };


public async Task Invoke(HttpContext context, IUnitOfWork unitOfWork)
public async Task Invoke(HttpContext context, IUnitOfWork unitOfWork, ILogger<CustomAuthHeaderMiddleware> logger, ITokenService tokenService)
{
// Extract user information from the custom header
string remoteUser = context.Request.Headers["Remote-User"];
Expand All @@ -27,8 +28,6 @@ public async Task Invoke(HttpContext context, IUnitOfWork unitOfWork)
return;
}



// Validate IP address
if (IsValidIpAddress(context.Connection.RemoteIpAddress))
{
Expand All @@ -42,12 +41,22 @@ public async Task Invoke(HttpContext context, IUnitOfWork unitOfWork)
return;
}
// Check if the RemoteUser has an account on the server
if (!context.Request.Path.Equals("/login", StringComparison.OrdinalIgnoreCase))
{
context.Response.Redirect($"/login?apiKey={user.ApiKey}");
return;
}
// if (!context.Request.Path.Equals("/login", StringComparison.OrdinalIgnoreCase))
// {
// // Attach the Auth header and allow it to pass through
// var token = await tokenService.CreateToken(user);
// context.Request.Headers.Add("Authorization", $"Bearer {token}");
// //context.Response.Redirect($"/login?apiKey={user.ApiKey}");
// return;
// }
// Attach the Auth header and allow it to pass through
var token = await tokenService.CreateToken(user);
context.Request.Headers.Append("Authorization", $"Bearer {token}");
await next(context);
return;
}

context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
await next(context);
}

Expand Down
2 changes: 1 addition & 1 deletion openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"name": "GPL-3.0",
"url": "https://github.com/Kareadita/Kavita/blob/develop/LICENSE"
},
"version": "0.7.11.7"
"version": "0.7.11.10"
},
"servers": [
{
Expand Down

0 comments on commit a1f68d7

Please sign in to comment.