diff --git a/MatrixMediaGate/Services/AuthValidator.cs b/MatrixMediaGate/Services/AuthValidator.cs
index 6f2b0c1..cf53cef 100644
--- a/MatrixMediaGate/Services/AuthValidator.cs
+++ b/MatrixMediaGate/Services/AuthValidator.cs
@@ -3,12 +3,18 @@ using System.Text.Json;
namespace MatrixMediaGate.Services;
-public class AuthValidator(ILogger<AuthValidator> logger, ProxyConfiguration cfg, IHttpContextAccessor ctx) {
- private static Dictionary<string, DateTime> _authCache = new();
+public class AuthValidator(ILogger<AuthValidator> logger, ProxyConfiguration cfg) {
+ private readonly Dictionary<string, DateTime> _authCache = new();
+ private readonly HttpClient _hc = new() {
+ BaseAddress = new Uri(cfg.Upstream),
+ DefaultRequestHeaders = {
+ Host = cfg.Host
+ }
+ };
- public async Task UpdateAuth() {
- if (ctx.HttpContext?.Connection.RemoteIpAddress is null) return;
- var remote = ctx.HttpContext.Connection.RemoteIpAddress.ToString();
+ public async Task UpdateAuth(HttpContext ctx) {
+ if (ctx.Connection.RemoteIpAddress is null) return;
+ var remote = ctx.Connection.RemoteIpAddress.ToString();
if (_authCache.TryGetValue(remote, out var value)) {
if (value > DateTime.Now.AddSeconds(30)) {
@@ -18,12 +24,10 @@ public class AuthValidator(ILogger<AuthValidator> logger, ProxyConfiguration cfg
_authCache.Remove(remote);
}
- string? token = getToken();
+ string? token = getToken(ctx);
if (token is null) return;
-
- using var hc = new HttpClient();
using var req = new HttpRequestMessage(HttpMethod.Get, $"{cfg.Upstream}/_matrix/client/v3/account/whoami?access_token={token}");
- var response = await hc.SendAsync(req);
+ var response = await _hc.SendAsync(req);
if (response.Content.Headers.ContentType?.MediaType != "application/json") return;
var content = await response.Content.ReadAsStringAsync();
@@ -34,9 +38,9 @@ public class AuthValidator(ILogger<AuthValidator> logger, ProxyConfiguration cfg
}
}
- public bool ValidateAuth() {
- if (ctx.HttpContext?.Connection.RemoteIpAddress is null) return false;
- var remote = ctx.HttpContext.Connection.RemoteIpAddress.ToString();
+ public bool ValidateAuth(HttpContext ctx) {
+ if (ctx.Connection.RemoteIpAddress is null) return false;
+ var remote = ctx.Connection.RemoteIpAddress.ToString();
if (_authCache.ContainsKey(remote)) {
if (_authCache[remote] > DateTime.Now) {
@@ -49,13 +53,12 @@ public class AuthValidator(ILogger<AuthValidator> logger, ProxyConfiguration cfg
return false;
}
- private string? getToken() {
- if (ctx.HttpContext is null) return null;
- if (ctx.HttpContext.Request.Headers.TryGetValue("Authorization", out var header)) {
+ private string? getToken(HttpContext ctx) {
+ if (ctx.Request.Headers.TryGetValue("Authorization", out var header)) {
return header.ToString().Split(' ', 2)[1];
}
- else if (ctx.HttpContext.Request.Query.ContainsKey("access_token")) {
- return ctx.HttpContext.Request.Query["access_token"]!;
+ else if (ctx.Request.Query.ContainsKey("access_token")) {
+ return ctx.Request.Query["access_token"]!;
}
else {
return null;
|