about summary refs log tree commit diff
path: root/MatrixMediaGate/Services
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-02-26 17:10:46 +0100
committerRory& <root@rory.gay>2024-02-26 17:10:46 +0100
commit9371a2eb9d10d9492a168fbb735ba0b0e4d76671 (patch)
treebca4c1ba38370e46265251f00945e9ef1f71f438 /MatrixMediaGate/Services
parentNix: add mainProgram (diff)
downloadMatrixMediaGate-9371a2eb9d10d9492a168fbb735ba0b0e4d76671.tar.xz
Fix auth code to be excluded on federation.
Diffstat (limited to 'MatrixMediaGate/Services')
-rw-r--r--MatrixMediaGate/Services/AuthValidator.cs27
1 files changed, 10 insertions, 17 deletions
diff --git a/MatrixMediaGate/Services/AuthValidator.cs b/MatrixMediaGate/Services/AuthValidator.cs

index 08ccd14..6f2b0c1 100644 --- a/MatrixMediaGate/Services/AuthValidator.cs +++ b/MatrixMediaGate/Services/AuthValidator.cs
@@ -6,43 +6,36 @@ namespace MatrixMediaGate.Services; public class AuthValidator(ILogger<AuthValidator> logger, ProxyConfiguration cfg, IHttpContextAccessor ctx) { private static Dictionary<string, DateTime> _authCache = new(); - public async Task<bool> UpdateAuth() { - if (ctx.HttpContext is null) return false; - if (ctx.HttpContext.Connection.RemoteIpAddress is null) return false; + public async Task UpdateAuth() { + if (ctx.HttpContext?.Connection.RemoteIpAddress is null) return; var remote = ctx.HttpContext.Connection.RemoteIpAddress.ToString(); - - + if (_authCache.TryGetValue(remote, out var value)) { if (value > DateTime.Now.AddSeconds(30)) { - return true; + return; } _authCache.Remove(remote); } string? token = getToken(); - if (token is null) return false; + 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}"); - req.Headers.Host = cfg.Host; var response = await hc.SendAsync(req); - if (response.Content.Headers.ContentType?.MediaType != "application/json") return false; + if (response.Content.Headers.ContentType?.MediaType != "application/json") return; var content = await response.Content.ReadAsStringAsync(); var json = JsonDocument.Parse(content); if (json.RootElement.TryGetProperty("user_id", out var userId)) { _authCache[remote] = DateTime.Now.AddMinutes(5); logger.LogInformation("Authenticated {userId} on {remote}, expiring at {time}", userId, remote, _authCache[remote]); - return true; } - - return false; } public bool ValidateAuth() { - if (ctx.HttpContext is null) return false; - if (ctx.HttpContext.Connection.RemoteIpAddress is null) return false; + if (ctx.HttpContext?.Connection.RemoteIpAddress is null) return false; var remote = ctx.HttpContext.Connection.RemoteIpAddress.ToString(); if (_authCache.ContainsKey(remote)) { @@ -57,9 +50,9 @@ public class AuthValidator(ILogger<AuthValidator> logger, ProxyConfiguration cfg } private string? getToken() { - if (ctx is null) return null; - if (ctx.HttpContext.Request.Headers.ContainsKey("Authorization")) { - return ctx.HttpContext.Request.Headers["Authorization"].ToString().Split(' ', 2)[1]; + if (ctx.HttpContext is null) return null; + if (ctx.HttpContext.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"]!;