From 05289f61d7bd0650ef511cc92a8a657c493dce30 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 31 Dec 2023 16:38:20 +0100 Subject: Clean up swagger, clean up auth code --- ModAS.Server/Services/AuthenticationService.cs | 76 -------------------------- ModAS.Server/Services/PingTask.cs | 9 --- ModAS.Server/Services/UserProviderService.cs | 1 - 3 files changed, 86 deletions(-) delete mode 100644 ModAS.Server/Services/AuthenticationService.cs delete mode 100644 ModAS.Server/Services/PingTask.cs (limited to 'ModAS.Server/Services') diff --git a/ModAS.Server/Services/AuthenticationService.cs b/ModAS.Server/Services/AuthenticationService.cs deleted file mode 100644 index 8efc08c..0000000 --- a/ModAS.Server/Services/AuthenticationService.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System.Net.Http.Headers; -using LibMatrix; -using LibMatrix.Services; -using MxApiExtensions.Extensions; -using MxApiExtensions.Services; - -namespace ModAS.Server.Services; - -public class AuthenticationService( - ILogger logger, - ModASConfiguration config, - IHttpContextAccessor request, - HomeserverProviderService homeserverProviderService) { - private readonly HttpRequest _request = request.HttpContext!.Request; - - private static Dictionary _tokenMap = new(); - - internal string? GetToken(bool fail = true) { - //_request.GetTypedHeaders().Get("Authorization")?.Parameter != asr.HomeserverToken - - string? token = null; - if (_request.GetTypedHeaders().TryGet("Authorization", out var authHeader) && !string.IsNullOrWhiteSpace(authHeader?.Parameter)) { - token = authHeader.Parameter; - } - else if (_request.Query.ContainsKey("access_token")) { - token = _request.Query["access_token"]; - } - - if (string.IsNullOrWhiteSpace(token) && fail) { - throw new MatrixException() { - ErrorCode = "M_MISSING_TOKEN", - Error = "Missing access token" - }; - } - - return token; - } - - public async Task GetMxidFromToken(string? token = null, bool fail = true) { - token ??= GetToken(fail); - if (string.IsNullOrWhiteSpace(token)) { - if (fail) { - throw new MatrixException() { - ErrorCode = "M_MISSING_TOKEN", - Error = "Missing access token" - }; - } - - return "@anonymous:*"; - } - - if (_tokenMap is not { Count: > 0 } && File.Exists("token_map")) { - _tokenMap = (await File.ReadAllLinesAsync("token_map")) - .Select(l => l.Split('\t')) - .ToDictionary(l => l[0], l => l[1]); - } - - if (_tokenMap.TryGetValue(token, out var mxid)) return mxid; - - logger.LogInformation("Looking up mxid for token {}", token); - var hs = await homeserverProviderService.GetAuthenticatedWithToken(config.ServerName, token, config.HomeserverUrl); - try { - var res = hs.WhoAmI.UserId; - logger.LogInformation("Got mxid {} for token {}", res, token); - - return res; - } - catch (MatrixException e) { - if (e.ErrorCode == "M_UNKNOWN_TOKEN") { - return null; - } - - throw; - } - } -} \ No newline at end of file diff --git a/ModAS.Server/Services/PingTask.cs b/ModAS.Server/Services/PingTask.cs deleted file mode 100644 index 99a8f40..0000000 --- a/ModAS.Server/Services/PingTask.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace ModAS.Server.Services; - -public class PingTask : IHostedService, IDisposable { - public Task StartAsync(CancellationToken cancellationToken) => throw new NotImplementedException(); - - public Task StopAsync(CancellationToken cancellationToken) => throw new NotImplementedException(); - - public void Dispose() => throw new NotImplementedException(); -} \ No newline at end of file diff --git a/ModAS.Server/Services/UserProviderService.cs b/ModAS.Server/Services/UserProviderService.cs index bb281c4..7e4065c 100644 --- a/ModAS.Server/Services/UserProviderService.cs +++ b/ModAS.Server/Services/UserProviderService.cs @@ -11,7 +11,6 @@ using MxApiExtensions.Services; namespace ModAS.Server.Services; public class UserProviderService( - AuthenticationService authenticationService, HomeserverProviderService homeserverProviderService, IHttpContextAccessor request, ModASConfiguration config, -- cgit 1.5.1